-
Notifications
You must be signed in to change notification settings - Fork 1
/
htestd.js
executable file
·252 lines (213 loc) · 6.56 KB
/
htestd.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
#! /usr/bin/env node
// htestd - HTTP Test Daemon
//
// A janky little HTTP server for testing peculiar things.
'use strict';
var http = require ('http');
var url = require ('url');
var console = require ('console');
var util = require ('util');
// Various utility shorthands ...
var info = console.info;
var fmt = util.format;
var port = 60000;
function send_response(response, status, body) {
if (!body) {
body = http.STATUS_CODES[status]
}
response.writeHead(status, {
'Content-Length': body.length,
'Content-Type': 'text/plain'
});
response.write(body);
response.end();
}
// Close the socket without sending a response (with an optional timeout).
//
// Syntax:
//
// [UNITS/TIME]
//
// UNITS:
// 's', 'sec' -> seconds
// 'ms', 'msec' -> milliseconds
function send_no_response(request, response, path) {
var ms = 0;
var now = new Date().getTime();
if (path.length == 2) {
switch (path[0]) {
case 's':
case 'sec':
ms = path[1] * 1000;
break;
case 'ms':
case 'msec':
ms = path[1];
break;
default:
return send_response(response, 400, fmt('unsupported time unit \'%s\'\n', path[0]));
}
}
return setTimeout(function() {
var elapsed = new Date().getTime() - now;
request.socket.destroy();
}, ms);
}
// Send a cacheable response
//
// Examples:
// /cacheable
// /cacheable/anything/you/like
function send_cacheable_response(request, response, path) {
var status = 200;
var body = http.STATUS_CODES[status] + "\n";
response.writeHead(status, {
//'Content-Length' : body.length,
'Content-Type': 'text/plain',
'Cache-Control': 'public,max-age=6000'
});
response.write(body);
response.end();
}
// Respond 200 OK after a timeout.
//
// Syntax:
//
// UNITS/TIME[/(head|body)]
//
// UNITS:
// 's', 'sec' -> seconds
// 'ms', 'msec' -> milliseconds
// (head|body):
// Optional flag to specify whether the timeout occurs
// before sending the headers or while we are sending
// the body. 'head' is the default.
//
// Examples:
// /timeout/s/10
// /timeout/msec/500/body
function send_timeout_response(request, response, path) {
var ms;
var how;
var now = new Date().getTime();
if (!path || path.length < 2 || path.length > 3) {
return send_response(response, 400);
}
how = (path.length == 3) ? path[2] : 'head';
switch (path[0]) {
case 's':
case 'sec':
ms = path[1] * 1000;
break;
case 'ms':
case 'msec':
ms = path[1];
break;
default:
return send_response(response, 400, fmt('unsupported time unit \'%s\'\n', path[0]));
}
switch (how) {
case 'head':
// Wait for the timeout, then send the head and body.
return setTimeout(function() {
var elapsed = new Date().getTime() - now;
var body = fmt('%sms elapsed\n', elapsed);
send_response(response, 200, body);
}, ms);
case 'body':
// Send the head and partial body, wait the timeout, then finish.
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.write('body pending ...\n');
return setTimeout(function() {
var elapsed = new Date().getTime() - now;
var body = fmt('%sms elapsed\n', elapsed);
response.write(body);
response.end();
}, ms);
default:
return send_response(response, 400, fmt('invalid timeout type \'%s\'\n', how));
}
}
// Respond with the a truncated HTTP response body.
function send_truncated_response(request, response, path) {
response.chunkedEncoding = false;
response.shouldKeepAlive = false;
response.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': '100'
});
response.write('body pending ...\n');
return setTimeout(function() {
response.write('or not\n');
response.end();
}, 1000);
}
// Respond with the requested HTTP status.
function send_status_response(response, path) {
var status;
if (!path || path.length != 1) {
return send_response(response, 400);
}
status = parseInt(path[0]);
if (isNaN(status)) {
return send_response(response, 400);
}
return send_response(response, status, fmt('you asked for a %d response\n', status));
}
// Respond with a JSON body containing the request headers.
//
// Examples:
// /headers
// /headers/anything you like
function send_headers_response(request, response, path) {
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.write(JSON.stringify(request.headers));
response.write('\n');
response.end();
}
var routes = {
'timeout': send_timeout_response,
'truncate': send_truncated_response,
'status': send_status_response,
'cacheable': send_cacheable_response,
'headers': send_headers_response,
'abort': send_no_response,
};
var server = http.createServer(function (request, response) {
var u = url.parse(request.url);
if (request.method != 'GET') {
return send_response(response, 400, 'Only GET is supported');
}
if (!u.pathname) {
// No path /.. bad request ...
return send_response(response, 400, 'You had better send a path');
}
console.info('attempting to route path %s', u.pathname);
var current = routes;
var components = u.pathname.split("/");
for (var i = 0; i < components.length; ++i) {
if (components[i] === '') {
continue;
}
if (components[i] in current) {
current = current[components[i]];
} else if (typeof(current) === typeof(Function)) {
// We have a handler for the partial path, but there's more of the
// path to consume. For example, the timeout handler take care or
// URLs like /timeout/ms/100
return current(request, response, components.slice(i, components.length));
} else {
return send_response(response, 404);
}
}
if (typeof(current) === typeof(Function)) {
return current(request, response, u.pathname.split("/"));
}
return send_response(response, 404);
});
server.listen(port, null, null, function() { console.info('listening on port %d', port); });
// vim: ts=4 sw=4 et :