-
Notifications
You must be signed in to change notification settings - Fork 62
/
host.js
366 lines (310 loc) · 10 KB
/
host.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
var test = require("tap").test;
var assert = require('assert');
var http = require('http');
var httpProxy = require('http-proxy');
var connect = require('connect');
var zlib = require('zlib');
// Create an array of selects that harmon will process.
var actions = [];
// Create a simple action
var simpleaction = {};
// Select a node by its class name. You can also select by tag e.g. 'div'
simpleaction.query = '.b';
// Create an function that is executed when that node is selected. Here we just replace '& frames' with '+trumpet'
simpleaction.func = function (node) {
node.createWriteStream({ outer: true })
.end('<div>+ Trumpet</div>');
}
// Add the action to the action array
actions.push(simpleaction);
var action2 = {};
action2.query = '.a';
// Create an function that is executed when that node is selected. Here we just replace '& frames' with '+trumpet'
action2.func = function (node) {
test("Request Test", function (t) {
t.plan(1);
t.ok(true, "Request Selector Has Been Called");
t.end();
});
node.createWriteStream({outer : true }).end('<div>Harmon Middleware</div>');
}
//Turning this off for now
//var reqactions = [];
actions.push(action2);
var con1 = connect();
con1.use(require('../')([], actions));
con1.use(function (req, res) {
proxy1.web(req, res);
})
var consvr1 = http.createServer(con1).listen(8000);
var proxy1 = httpProxy.createProxyServer({
target: 'http://localhost:9000'
})
// Create a simple web server for the proxy to send requests to and manipulate the data from
var server1 = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<html><head></head><body><div class="a">Nodejitsu Http Proxy</div><div class="b">& Frames</div></body></html>');
res.end();
}).listen(9000);
var options = {
host: 'localhost',
port: 8000,
path: '/',
method: 'POST'
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var out = "";
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
out+= chunk;
});
res.on('end', function(){
assert.equal('<html><head></head><body><div>Harmon Middleware</div><div>+ Trumpet</div></body></html>', out);
console.log("# Content Returned Correct");
consvr1.close();
server1.close();
proxy1 = null;
});
res.on('close', function(){
console.log("CLOSE");
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.on('close', function(){
console.log("END");
});
// write data to request body
req.write('<html><head></head><body><div class="a">Nodejitsu Http Proxy</div><div class="b">& Frames</div></body></html>');
req.end();
test('Streams can change the response size', function (t) {
t.plan(1);
var server2 = http.createServer(function (req, res) {
var s = '<body><p>hi</p></body>';
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-length', '' + s.length); // All ASCII today
res.end(s);
}).listen(9001);
var sizeChanger = {} ;
sizeChanger.query = 'p';
sizeChanger.func = function (elem) {
ws = elem.createWriteStream({outer: true})
ws.end('<p>A larger paragraph</p>');
}
var con2 = connect();
con2.use(require('../')([], [sizeChanger]));
con2.use(function (req, res) {
proxy.web(req, res);
}
)
var consvr2 = http.createServer(con2).listen(8001);
var proxy = httpProxy.createProxyServer({
target: 'http://localhost:9001'
})
/*
var proxy2 = httpProxy.createServer(
require('../')(null, [sizeChanger]),
9001, 'localhost'
).listen(8001);
*/
http.get('http://localhost:8001', function (res) {
var str = ''; // yeah well it's all ASCII today.
res.on('data', function (data) {
console.log("'data'", data + '');
str += data;
});
res.on('end', function () {
t.equal(str, '<body><p>A larger paragraph</p></body>');
server2.close();
consvr2.close();
t.end();
});
});
});
test('Only text/html should be altered.', function (t) {
t.plan(1);
var server2 = http.createServer(function (req, res) {
var s = '<body><p>hi</p></body>';
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-length', '' + s.length); // All ASCII today
res.end(s);
}).listen(9001);
var ignoredSelector = {} ;
ignoredSelector.query = 'p';
ignoredSelector.func = function (elem) {
ws = elem.createWriteStream({outer: true})
ws.end('<p>A larger paragraph</p>');
}
var con2 = connect();
con2.use(require('../')([], [ignoredSelector], true));
con2.use(function (req, res) {
proxy2.web(req, res);
}
)
var consvr2 = http.createServer(con2).listen(8001);
var proxy2 = httpProxy.createProxyServer({
target: 'http://localhost:9001'
})
http.get('http://localhost:8001', function (res) {
var str = ''; // yeah well it's all ASCII today.
res.on('data', function (data) {
console.log("'data'", data + '');
str += data;
});
res.on('end', function () {
// Should not be modified since server indicated that content type is not HTML
t.equal(str, '<body><p>hi</p></body>');
server2.close();
consvr2.close();
t.end();
});
});
});
test('Gzipped content should be affected.', function (t) {
t.plan(2);
var initialString = '<html><head></head><body><div class="b3">Gzipped is NOT affected</div></body></html>';
var expectedString = '<html><head></head><body><div class="b3">Gzipped IS affected</div></body></html>';
// Server that returns test gziped response
var server3 = http.createServer(function (req, res) {
var gzip = zlib.createGzip();
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-Encoding', 'gzip');
gzip.on('data', function (buf) {
res.write(buf);
});
gzip.on('end', function () {
res.end();
});
gzip.write(initialString);
gzip.end();
}).listen(9001);
// Proxy-Server
var proxy3 = httpProxy.createProxyServer({
target: 'http://localhost:9001'
})
var selector3 = {
query: '.b3',
func: function (node) {
node.createWriteStream().end('Gzipped IS affected');
}
};
var con3 = connect();
con3.use(require('../')([], [selector3]));
con3.use(function (req, res) {
proxy3.web(req, res);
})
// Proxied Server
var consvr3 = http.createServer(con3).listen(8001);
// Check proxied response
var testProxied = function(){
http.get('http://localhost:8001', function (res) {
var str = '';
res.on('data', function (data) {
str += data;
});
res.on('end', function () {
// response string should be equals to expected
t.equals(str, expectedString);
t.end();
consvr3.close();
server3.close();
proxy3 = null;
});
});
}
// Check direct response
var testDirect = function(callback){
http.get('http://localhost:9001', function (res) {
var str = '';
res.on('data', function (data) {
str += data;
});
res.on('end', function () {
// initial string should be modified by gzip
t.notEquals(initialString, str);
callback();
});
});
}
// Run tests
testDirect(testProxied);
});
test('Gzipped content should be proxied when content is not html and htmlonly is true.', function (t) {
t.plan(3);
var initialString = JSON.stringify({hello:'world'});
var expectedString = JSON.stringify({hello:'world'});
var gunzip = zlib.Gunzip();
// Server that returns test gziped response
var server3 = http.createServer(function (req, res) {
var gzip = zlib.createGzip();
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Encoding', 'gzip');
gzip.on('data', function (buf) {
res.write(buf);
});
gzip.on('end', function () {
res.end();
});
gzip.write(initialString);
gzip.end();
}).listen(9001);
// Proxy-Server
var proxy3 = httpProxy.createProxyServer({
target: 'http://localhost:9001'
})
var selector3 = {
query: '.b3',
func: function (node) {
node.createWriteStream().end('Gzipped IS affected');
}
};
var con3 = connect();
con3.use(require('../')([], [selector3], true));
con3.use(function (req, res) {
proxy3.web(req, res);
})
// Proxied Server
var consvr3 = http.createServer(con3).listen(8001);
// Check proxied response
var testProxied = function(){
http.get('http://localhost:8001', function (res) {
var str = '';
res.on('data', function (data) {
gunzip.write(data);
});
res.on('end', function () {
gunzip.end();
});
gunzip.on('data', function(data){
str += data.toString();
})
gunzip.on('end', function(){
// response string should be equals to expected
t.equals(str, expectedString);
t.equals(res.headers['content-encoding'], 'gzip');
t.end();
consvr3.close();
server3.close();
proxy3 = null;
})
});
}
// Check direct response
var testDirect = function(callback){
http.get('http://localhost:9001', function (res) {
var str = '';
res.on('data', function (data) {
str += data;
});
res.on('end', function () {
// initial string should be modified by gzip
t.notEquals(initialString, str);
callback();
});
});
}
// Run tests
testDirect(testProxied);
});