-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
278 lines (248 loc) · 10.6 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"use strict";
var comb = require("comb"),
isString = comb.isString,
merge = comb.merge,
http = require("http"),
https = require("https"),
methods = require("methods"),
util = require('util'),
request = require("request");
var port = 3234, Test;
var getCookie = function (name, jar, url) {
return jar["_jar"].getCookiesSync(url).filter(function (cookie) {
return cookie.key === name;
})[0];
};
var promiseInstance = {
constructor: function (test) {
this._super(arguments);
this._test = test;
}
};
methods.forEach(function (method) {
var name = 'delete' === method ? 'del' : method;
method = method.toUpperCase();
promiseInstance[name] = function (url) {
var test = this._test;
return new Test(test.app, method, url).wait(this).jar(test._jar);
};
});
var TestPromise = comb.define(comb.Promise, {
instance: promiseInstance
});
Test = comb.define(null, {
instance: {
_wait: null,
constructor: function (app, method, path) {
this.method = method;
var self = this._static;
this.app = app;
if (isString(app)) {
this._baseUrl = app;
} else {
var addr = app.address();
var portno = addr ? addr.port : self.PORT++;
if (!addr) {
app.listen(portno);
}
var protocol = app instanceof https.Server ? 'https' : 'http';
this._baseUrl = protocol + '://127.0.0.1:' + portno;
}
this._url = this._baseUrl + path;
this._expects = [];
this._options = {};
this._jar = request.jar();
self.REQUEST_OPTIONS.forEach(function (opt) {
if (!(opt in this)) {
this[opt] = function (value) {
this._options[opt] = value;
return this;
};
}
}, this);
},
jar: function (jar) {
this._jar = jar;
return this;
},
upload: function (file) {
this._upload = merge({}, file);
return this;
},
wait: function (promise) {
this._wait = promise;
return this;
},
expect: function (field, val, cb) {
var self = this;
var dummyObject = {};
var v8Handler = Error.prepareStackTrace;
Error.prepareStackTrace = function (dummyObject, v8StackTrace) {
return v8StackTrace;
};
Error.captureStackTrace(dummyObject, self.expect);
var stack = dummyObject.stack;
Error.prepareStackTrace = v8Handler;
var stack = "at (" + stack[0].getFileName() + ":" + stack[0].getLineNumber() + ":" + stack[0].getColumnNumber() + ")";
var args = comb.argsToArray(arguments);
cb = comb.isFunction(args[args.length - 1]) ? args.pop() : null;
if (field === cb) {
cb = null;
args.push(cb);
}
if (args.length === 2 && ["cookie", "!cookie"].indexOf(field) === -1) {
if (comb.isNumber(field)) {
this._expects.push(["status", field, stack]);
this._expects.push(["body", val, stack]);
} else {
this._expects.push(["header", field, val, stack]);
}
} else if (args.length === 1) {
this._expects.push([comb.isNumber(field) ? "status" : "body", field, stack]);
} else {
args.push(stack);
this._expects.push(args);
}
if (cb) {
return this.end(cb);
}
return this;
},
_parseExpect: function (res, body, promise) {
var headers = res.headers;
comb.async.array(this._expects).forEach(function (expect) {
switch (expect[0]) {
case "cookie":
//testing for cookies
var key = expect[1], val = expect[2], cookie = getCookie(key, this._jar, this._baseUrl);
if (comb.isUndefined(cookie)) {
throw new Error("expected cookie " + key + " to be present");
}
if (val) {
if (comb.isHash(val) && !comb.deepEqual(cookie, val)) {
throw new Error("expected cookie " + key + " to equal " + JSON.stringify(val) + "\n" + expect[3]);
} else if (cookie.value === val) {
throw new Error("expected cookie " + key + " value to equal " + val);
}
}
break;
case "!cookie":
//testing for cookies
key = expect[1];
if (!comb.isUndefinedOrNull(getCookie(key, this._jar, this._baseUrl))) {
throw new Error("expected cookie " + key + " to be null or undefined " + "\n" + expect[2]);
}
break;
case "header":
//it is a header test
var header = expect[1].toLowerCase();
val = expect[2];
if (header in headers) {
if (comb.isRexExp(val)) {
if (!val.test(headers[header])) {
throw new Error('Expected "' + expect[1] + '" matching ' + val + ', got "' + headers[header] + '"' + "\n" + expect[3]);
}
} else {
if (header === "location") {
var actual = headers[header];
if (!comb.deepEqual(actual, val)
&& !comb.deepEqual(actual, this._baseUrl + val)
&& !comb.deepEqual(actual, this._baseUrl.replace(/^http[s]?:/, "") + val)) {
throw new Error([
'Expected "', expect[1], '" of "' + val, '", got "', headers[header], '"'
].join("") + "\n" + expect[3]);
}
} else if (!comb.deepEqual(headers[header.toLowerCase()], val)) {
throw new Error([
'Expected "', expect[1], '" of "' + val, '", got "', headers[header], '"'
].join("") + "\n" + expect[3]);
}
}
} else {
throw new Error('Expected "' + expect[1] + '" header field' + "\n" + expect[3]);
}
break;
case "body":
val = expect[1];
if (comb.isHash(val)) {
//the body should be json
var json;
try {
json = comb.isString(body) ? JSON.parse(body.replace(/\\n/g, "")) : body;
} catch (e) {
throw new Error("Unable to parse " + body + " to json");
}
if (!comb.deepEqual(json, val)) {
throw new Error(['Expected', util.inspect(val), 'response body, got', util.inspect(json)].join(" ") + "\n" + expect[2]);
}
} else if (comb.isFunction(val)) {
return val(body);
} else if (comb.isRegExp(val)) {
if (!val.test(body)) {
throw new Error(['Expected body', util.inspect(body), 'to match', util.inspect(val)].join(" ") + "\n" + expect[2]);
}
} else if (!comb.deepEqual(body, val)) {
//just assert the body
throw new Error(['Expected', util.inspect(val), 'response body, got', util.inspect(body)].join(" ") + "\n" + expect[2]);
}
break;
case "status":
if (res.statusCode !== expect[1])
throw new Error(["Expected response status code to be", expect[1], "got", res.statusCode].join(" ") + "\n" + expect[2]);
break;
}
}, this, 1).then(function () {
promise.callback(res, body);
}, promise);
},
end: function (cb) {
var ret = new TestPromise(this).classic(cb);
var opts = comb.deepMerge({}, {jar: this._jar, method: this.method, url: this._url}, this._options);
comb.when(this._wait).chain(function () {
// call any functions that have been passed to an option
// to get the calculated values (object, int, string, etc)
comb.hash.forEach(opts, function(val, key) {
if (comb.isFunction(val)) {
opts[key] = val();
}
}, this);
var r = request(opts, function (err, res, body) {
if (err) {
return ret.errback(err);
} else {
return this._parseExpect(res, body, ret);
}
}.bind(this));
r.setMaxListeners(0);
var upload = this._upload;
if (upload && !comb.isEmpty(upload)) {
var form = r.form();
comb(upload).forEach(function (value, key) {
form.append(key, value);
});
}
}.bind(this), ret.errback.bind(ret));
return ret;
}
},
"static": {
PORT: 3234,
REQUEST_OPTIONS: ["uri", "url", "qs", "headers", "body", "form", "json", "multipart", "followRedirect",
"followAllRedirects", "maxRedirects", "encoding", "pool", "timeout", "oauth", "strictSSL", "jar", "aws",
"auth", "rejectUnauthorized", "httpSignature"]
}
});
module.exports = function (app) {
if (comb.isFunction(app)) {
app = http.createServer(app);
}
var ret = {};
methods.forEach(function (method) {
var name = 'delete' === method ? 'del' : method;
method = method.toUpperCase();
ret[name] = function (url) {
return new Test(app, method, url);
};
});
return ret;
};