/
testosterone.js
198 lines (172 loc) · 5.84 KB
/
testosterone.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
module.exports = function (config) {
config = config || {};
var client = require('http').createClient(
config.port || 80,
config.host || 'localhost'
),
sys = require('sys'),
testosterone = {},
count_responses = 0,
done_responses = 0,
count_asserts = 0,
passed_asserts = 0,
colorize = function (str) {
var colors = { bold: 1, red: 31, green: 32, yellow: 33, grey: 30 };
return str.replace(/\[(\w+)\]\{([^]*?)\}/g, function (_, color, str) {
return '\x1B[' + colors[color] + 'm' + str + '\x1B[0m';
});
},
test = function () {
if (count_responses === done_responses) {
sys.print(colorize('\n[grey]{» ' + done_responses + ' responses, ' + passed_asserts + ' asserts}\n'));
process.exit();
}
},
// extend assert
assert = (function () {
var assert = require('assert'),
functions = Object.keys(assert),
a = {};
functions.forEach(function (fn) {
if (typeof require('assert')[fn] === 'function') {
a[fn] = function (_) {
count_asserts += 1;
try {
require('assert')[fn].apply(this, Array.prototype.slice.call(arguments, 0));
} catch (exc) {
sys.print(colorize('\n[red]{✗ => ' + exc.stack + '}\n'));
process.exit();
}
passed_asserts += 1;
sys.print(colorize('[green]{✓} '));
};
}
});
return a;
}()),
/**
* Do an http call with
* the given `req` object and `res` assertions object.
*
* @param {Object} req
* @param {Object|Function} res
*/
call = function (req, res, callback) {
// Callback as third or fourth arg
if (typeof res === 'function') {
callback = res;
} else {
if (typeof callback !== 'function') {
callback = function () {};
}
}
// Issue request
var timer,
method = req.method || 'GET',
status = res.status || res.statusCode,
data = req.data || req.body,
requestTimeout = req.timeout || 0,
headers = req.headers || {},
request;
if (data) {
if (typeof data === 'object') {
data = require('querystring').stringify(data);
if (req.method === 'POST') {
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
}
}
}
request = client.request(method, req.url, headers);
if (data) {
request.write(data);
}
// Timeout
if (requestTimeout) {
timer = setTimeout(function () {
delete req.timeout;
assert.fail('Request timed out after ' + requestTimeout + 'ms.');
}, requestTimeout);
}
request.addListener('response', function (response) {
response.body = '';
response.setEncoding('utf8');
response.addListener('data', function (chunk) {
response.body += chunk;
});
response.addListener('end', function () {
if (timer) {
clearTimeout(timer);
}
// Assert response body
if (res.body !== undefined) {
var eql = res.body instanceof RegExp
? res.body.test(response.body)
: res.body === response.body;
assert.ok(
eql,
'Invalid response body.\n' +
' Expected: ' + sys.inspect(res.body) + '\n' +
' Got: ' + sys.inspect(response.body)
);
}
// Assert response status
if (typeof status === 'number') {
assert.equal(
response.statusCode,
status,
colorize(
'Invalid response status code.\n' +
' Expected: [green]{' + status + '}\n' +
' Got: [red]{' + response.statusCode + '}'
));
}
// Assert response headers
if (res.headers) {
(function () {
var keys = Object.keys(res.headers),
len = keys.length,
i, name, actual, expected, eql;
for (i = 0; i < len; ++i) {
name = keys[i];
actual = response.headers[name.toLowerCase()];
expected = res.headers[name];
eql = expected instanceof RegExp ? expected.test(actual) : expected === actual;
assert.ok(
eql,
colorize(
'Invalid response header [bold]{' + name + '}.\n' +
' Expected: [green]{' + expected + '}\n' +
' Got: [red]{' + actual + '}'
)
);
}
}());
}
// Callback
done_responses += 1;
test();
callback(response);
});
});
request.end();
};
['get', 'post', 'head', 'put', 'delete', 'trace', 'options', 'connect'].forEach(function (method) {
// refactor this a little bit
testosterone[method] = function (route, req, res, callback) {
count_responses += 1;
test();
if (typeof req === 'function') {
callback = req;
res = {};
req = {};
}
req.method = method.toUpperCase();
req.url = req.url || route;
call(req, res, callback);
return testosterone;
};
});
testosterone.assert = assert;
sys.print(colorize('[yellow]{✿ Testosterone}\n'));
return testosterone;
};