Skip to content

Commit feb324b

Browse files
committed
[test] Add core test-http-proxy test
Modifications: * make client connect to `PROXY_PORT` instead of `PORT`
1 parent 275109b commit feb324b

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

test/core/simple/test-http-proxy.js

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
var url = require('url');
26+
27+
var PROXY_PORT = common.PORT;
28+
var BACKEND_PORT = common.PORT + 1;
29+
30+
var cookies = [
31+
'session_token=; path=/; expires=Sun, 15-Sep-2030 13:48:52 GMT',
32+
'prefers_open_id=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT'
33+
];
34+
35+
var headers = {'content-type': 'text/plain',
36+
'set-cookie': cookies,
37+
'hello': 'world' };
38+
39+
var backend = http.createServer(function(req, res) {
40+
common.debug('backend request');
41+
res.writeHead(200, headers);
42+
res.write('hello world\n');
43+
res.end();
44+
});
45+
46+
var proxy = http.createServer(function(req, res) {
47+
common.debug('proxy req headers: ' + JSON.stringify(req.headers));
48+
var proxy_req = http.get({
49+
port: BACKEND_PORT,
50+
path: url.parse(req.url).pathname
51+
}, function(proxy_res) {
52+
53+
common.debug('proxy res headers: ' + JSON.stringify(proxy_res.headers));
54+
55+
assert.equal('world', proxy_res.headers['hello']);
56+
assert.equal('text/plain', proxy_res.headers['content-type']);
57+
assert.deepEqual(cookies, proxy_res.headers['set-cookie']);
58+
59+
res.writeHead(proxy_res.statusCode, proxy_res.headers);
60+
61+
proxy_res.on('data', function(chunk) {
62+
res.write(chunk);
63+
});
64+
65+
proxy_res.on('end', function() {
66+
res.end();
67+
common.debug('proxy res');
68+
});
69+
});
70+
});
71+
72+
var body = '';
73+
74+
var nlistening = 0;
75+
function startReq() {
76+
nlistening++;
77+
if (nlistening < 2) return;
78+
79+
var client = http.get({
80+
port: common.PROXY_PORT,
81+
path: '/test'
82+
}, function(res) {
83+
common.debug('got res');
84+
assert.equal(200, res.statusCode);
85+
86+
assert.equal('world', res.headers['hello']);
87+
assert.equal('text/plain', res.headers['content-type']);
88+
assert.deepEqual(cookies, res.headers['set-cookie']);
89+
90+
res.setEncoding('utf8');
91+
res.on('data', function(chunk) { body += chunk; });
92+
res.on('end', function() {
93+
proxy.close();
94+
backend.close();
95+
common.debug('closed both');
96+
});
97+
});
98+
common.debug('client req');
99+
}
100+
101+
common.debug('listen proxy');
102+
proxy.listen(PROXY_PORT, startReq);
103+
104+
common.debug('listen backend');
105+
backend.listen(BACKEND_PORT, startReq);
106+
107+
process.on('exit', function() {
108+
assert.equal(body, 'hello world\n');
109+
});

0 commit comments

Comments
 (0)