This repository was archived by the owner on Sep 12, 2018. It is now read-only.
forked from jupyterhub/configurable-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutil.js
138 lines (126 loc) · 3.9 KB
/
testutil.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 http = require('http');
var URL = require('url');
var extend = require('util')._extend;
var WebSocketServer = require('ws').Server;
var querystring = require('querystring');
var configproxy = require('../lib/configproxy');
var servers = [];
var add_target = exports.add_target = function (proxy, path, port, websocket, target_path) {
var target = 'http://127.0.0.1:' + port;
if (target_path) {
target = target + target_path;
}
var server;
var data = {
target: target,
path: path,
};
server = http.createServer(function (req, res) {
var reply = extend({}, data);
reply.url = req.url;
reply.headers = req.headers;
res.write(JSON.stringify(reply));
res.end();
});
if (websocket) {
var wss = new WebSocketServer({
server: server
});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
var reply = extend({}, data);
reply.message = message;
ws.send(JSON.stringify(reply));
});
ws.send('connected');
});
}
server.listen(port);
servers.push(server);
proxy.add_route(path, {target: target});
};
var add_target_redirecting = exports.add_target_redirecting = function (proxy, path, port, target_path, redirect_to) {
// Like the above, but the server returns a redirect response with a Location header.
// Cannot use default arguments as they are apparently not supported.
var target = 'http://127.0.0.1:' + port;
if (target_path) {
target = target + target_path;
}
var server;
server = http.createServer(function (req, res) {
res.setHeader("Location", redirect_to);
res.statusCode = 301;
res.write('');
res.end();
});
server.listen(port);
servers.push(server);
proxy.add_route(path, {target: target});
};
exports.setup_proxy = function (port, callback, options, paths) {
options = options || {};
options.auth_token = 'secret';
var proxy = new configproxy.ConfigurableProxy(options);
// add default route
var p = port + 1;
paths = paths || ['/'];
paths.map(function (path) {
p = p + 1;
add_target(proxy, path, p, true);
});
var ip = '127.0.0.1';
var countdown = 2;
var onlisten = function () {
countdown = countdown - 1;
if (countdown === 0) {
if (callback) {
callback(proxy);
}
}
};
if (options.error_target) {
countdown = countdown + 1;
var error_server = http.createServer(function (req, res) {
var parsed = URL.parse(req.url);
var query = querystring.parse(parsed.query);
res.setHeader('Content-Type', 'text/plain');
res.write(query.url);
req.on('data', function () {});
req.on('end', function () {
res.end();
});
});
error_server.on('listening', onlisten);
error_server.listen(URL.parse(options.error_target).port, ip);
servers.push(error_server);
}
proxy.proxy_server.listen(port, ip);
proxy.api_server.listen(port + 1, ip);
servers.push(proxy.proxy_server);
servers.push(proxy.api_server);
proxy.api_server.on('listening', onlisten);
proxy.proxy_server.on('listening', onlisten);
return proxy;
};
exports.teardown_servers = function (callback) {
var count = 0;
var onclose = function () {
count = count + 1;
if (count === servers.length) {
servers = [];
callback();
}
};
for (var i = servers.length - 1; i >= 0; i--) {
servers[i].close(onclose);
}
};
var onfinish = exports.onfinish = function (res, callback) {
res.body = '';
res.on('data', function (chunk) {
res.body += chunk;
});
res.on('end', function () {
callback(res);
});
};