This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (122 loc) · 3.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var restify = require('restify'),
colors = require('colors'),
assert = require('assert'),
querystring = require('querystring');
var cupidon = require('./cupidon');
var Histoire = function(opts) {
opts.matcher = cupidon.match;
this.opts = opts;
this.chapters = [];
this.responses = {};
this.opts.path = this.opts.path || '';
this.client = restify.createJsonClient({
url: opts.url,
version: '*'
});
};
function evaluate(data, context) {
if (typeof data == 'string' || typeof data == 'number' || typeof data == 'boolean') {
return data;
} else if (typeof data == 'function') {
return data(context);
} else if (data instanceof Array) {
return data.map(function(value) {
return evaluate(value, context);
});
} else {
Object.getOwnPropertyNames(data).forEach(function(key) {
data[key] = evaluate(data[key], context);
});
return data;
}
};
Histoire.prototype = {
chapter: function(chapter) {
chapter.withStatus = chapter.withStatus || 200;
this.chapters.push(chapter);
},
metsTesLunettesEtLisNousTout: function() {
var self = this;
var next = function(i) {
if (i >= self.chapters.length) return;
var chapter = self.chapters[i];
var callback = function(err, req, res, data) {
// if (err) {
// console.log('ERROR');
// console.log(JSON.stringify(err, undefined, 2));
// throw(err);
// }
if (chapter.id !== undefined) {
self.responses[chapter.id] = data;
}
var goodStatus = (chapter.withStatus == res.statusCode);
if (data !== undefined) {
console.log('->');
console.log(('' + res.statusCode)[goodStatus ? 'green' : 'red']);
console.log(JSON.stringify(data, undefined, 2));
}
console.log();
assert.ok(goodStatus);
if (chapter.expect !== undefined) {
assert.ok(self.opts.matcher(data, chapter.expect, self.responses));
}
if (chapter.andWait !== undefined) {
setTimeout(function() { next(i + 1); }, chapter.andWait);
} else {
next(i + 1);
}
};
if (chapter.topic !== undefined) {
console.log(chapter.topic.yellow);
}
if (chapter.type == 'GET' || chapter.type == 'DELETE') {
var path = evaluate(chapter.path, self.responses);
console.log(chapter.type + ' ' + path);
var params = chapter.params;
if (params) console.log(JSON.stringify(params, undefined, 2));
var query = self.opts.path + path + (
(!params)
? ''
: ('?' + querystring.stringify(params))
);
var opts = {
path: query,
headers: chapter.headers
};
if (chapter.type == 'GET') {
self.client.get(opts, callback);
} else {
self.client.del({
path: query,
headers: {
'Content-Length': 0
}
}, callback);
}
} else if (chapter.type == 'POST' || chapter.type == 'PUT') {
var path = evaluate(chapter.path, self.responses);
console.log(chapter.type + ' ' + path);
var opts = {
path: self.opts.path + path
};
if (chapter.headers !== undefined) {
console.log(JSON.stringify(chapter.headers, undefined, 2).grey);
opts.headers = evaluate(chapter.headers, self.responses);
}
var data = evaluate(chapter.data || {}, self.responses);
console.log(JSON.stringify(data, undefined, 2));
self.client[chapter.type == 'POST' ? 'post' : 'put'](opts, data, callback);
} else if (chapter.type == 'WAIT') {
console.log('WAIT ' + chapter.duration + 'ms');
setTimeout(function() { next(i + 1); }, chapter.duration);
}
};
next(0);
}
};
module.exports = {
raconteNousUneHistoire: function(opts) {
return new Histoire(opts);
},
any: cupidon.any
};