1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var common = require('../common');
23 var assert = require('assert');
24 var http = require('http');
25 var url = require('url');
28 common.error(common.inspect(x));
31 var responses_sent = 0;
32 var responses_recvd = 0;
36 var server = http.Server(function (req, res) {
37 if (responses_sent == 0) {
38 assert.equal('GET', req.method);
39 assert.equal('/hello', url.parse(req.url).pathname);
41 console.dir(req.headers);
42 assert.equal(true, 'accept' in req.headers);
43 assert.equal('*/*', req.headers['accept']);
45 assert.equal(true, 'foo' in req.headers);
46 assert.equal('bar', req.headers['foo']);
49 if (responses_sent == 1) {
50 assert.equal('POST', req.method);
51 assert.equal('/world', url.parse(req.url).pathname);
55 req.on('end', function () {
56 res.writeHead(200, {'Content-Type': 'text/plain'});
57 res.write('The path was ' + url.parse(req.url).pathname);
62 //assert.equal('127.0.0.1', res.connection.remoteAddress);
64 server.listen(common.PORT);
66 server.on('listening', function () {
67 var agent = new http.Agent({ port: common.PROXY_PORT, maxSockets: 1 });
69 port: common.PROXY_PORT,
71 headers: {'Accept': '*/*', 'Foo': 'bar'},
74 assert.equal(200, res.statusCode);
76 res.setEncoding('utf8');
77 res.on('data', function (chunk) { body0 += chunk; });
78 common.debug('Got /hello response');
81 setTimeout(function () {
82 var req = http.request({
83 port: common.PROXY_PORT,
88 assert.equal(200, res.statusCode);
90 res.setEncoding('utf8');
91 res.on('data', function (chunk) { body1 += chunk; });
92 common.debug('Got /world response');
98 process.on('exit', function () {
99 common.debug('responses_recvd: ' + responses_recvd);
100 assert.equal(2, responses_recvd);
102 common.debug('responses_sent: ' + responses_sent);
103 assert.equal(2, responses_sent);
105 assert.equal('The path was /hello', body0);
106 assert.equal('The path was /world', body1);