/
test.js
195 lines (165 loc) 路 4.1 KB
/
test.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
/**
* Module dependencies.
*/
var request = require('superagent')
, util = require('util')
, http = require('http')
, https = require('https')
, assert = require('assert')
, Request = request.Request;
/**
* Starting port.
*/
var port = 3456;
/**
* Expose `Test`.
*/
module.exports = Test;
/**
* Initialize a new `Test` with the given `app`,
* request `method` and `path`.
*
* @param {Server} app
* @param {String} method
* @param {String} path
* @api public
*/
function Test(app, method, path) {
Request.call(this, method, path);
this.redirects(0);
this.app = app;
this._fields = {};
this.url = 'string' === typeof app
? app + path
: this.serverAddress(app, path);
}
/**
* Inherits from `Request.prototype`.
*/
Test.prototype.__proto__ = Request.prototype;
/**
* Returns a URL, extracted from a server.
*
* @param {Server} app
* @param {String} path
* @returns {String} URL address
* @api private
*/
Test.prototype.serverAddress = function(app, path){
var addr = app.address();
var portno = addr ? addr.port : port++;
if (!addr) app.listen(portno);
var protocol = app instanceof https.Server ? 'https' : 'http';
return protocol + '://127.0.0.1:' + portno + path;
};
/**
* Expectations:
*
* .expect(200)
* .expect(200, fn)
* .expect(200, body)
* .expect('Some body')
* .expect('Some body', fn)
* .expect('Content-Type', 'application/json')
* .expect('Content-Type', 'application/json', fn)
*
* @return {Test}
* @api public
*/
Test.prototype.expect = function(a, b, c){
var self = this;
// callback
if ('function' == typeof b) this.end(b);
if ('function' == typeof c) this.end(c);
// status
if ('number' == typeof a) {
this._status = a;
// body
if ('function' != typeof b) this._body = b;
return this;
}
// header field
if ('string' == typeof b || b instanceof RegExp) {
this._fields[a] = b;
return this;
}
// body
this._body = a;
return this;
};
/**
* Defer invoking superagent's `.end()` until
* the server is listening.
*
* @param {Function} fn
* @api public
*/
Test.prototype.end = function(fn){
var self = this;
var end = Request.prototype.end;
end.call(this, function(res){
self.assert(res, fn);
});
return this;
};
/**
* Perform assertions and invoke `fn(err)`.
*
* @param {Response} res
* @param {Function} fn
* @api private
*/
Test.prototype.assert = function(res, fn){
var status = this._status
, fields = this._fields
, body = this._body
, isregexp = body instanceof RegExp
, expected
, actual
, re;
// status
if (status && res.status !== status) {
var a = http.STATUS_CODES[status];
var b = http.STATUS_CODES[res.status];
return fn(new Error('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"'), res);
}
// body
if (null != body) {
// parsed
if ('object' == typeof body && !isregexp) {
try {
assert.deepEqual(body, res.body);
} catch (err) {
var a = util.inspect(body);
var b = util.inspect(res.body);
return fn(new Error('expected ' + a + ' response body, got ' + b));
}
} else {
// string
if (body !== res.text) {
var a = util.inspect(body);
var b = util.inspect(res.text);
// regexp
if (isregexp) {
if (!body.test(res.text)) {
return fn(new Error('expected body ' + b + ' to match ' + body));
}
} else {
return fn(new Error('expected ' + a + ' response body, got ' + b));
}
}
}
}
// fields
for (var field in fields) {
expected = fields[field];
actual = res.header[field.toLowerCase()];
if (null == actual) return fn(new Error('expected "' + field + '" header field'));
if (expected == actual) continue;
if (expected instanceof RegExp) re = expected;
if (re && re.test(actual)) continue;
if (re) return fn(new Error('expected "' + field + '" matching ' + expected + ', got "' + actual + '"'));
return fn(new Error('expected "' + field + '" of "' + expected + '", got "' + actual + '"'));
}
fn(null, res);
};