|
| 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 | +function p(x) { |
| 28 | + common.error(common.inspect(x)); |
| 29 | +} |
| 30 | + |
| 31 | +var responses_sent = 0; |
| 32 | +var responses_recvd = 0; |
| 33 | +var body0 = ''; |
| 34 | +var body1 = ''; |
| 35 | + |
| 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); |
| 40 | + |
| 41 | + console.dir(req.headers); |
| 42 | + assert.equal(true, 'accept' in req.headers); |
| 43 | + assert.equal('*/*', req.headers['accept']); |
| 44 | + |
| 45 | + assert.equal(true, 'foo' in req.headers); |
| 46 | + assert.equal('bar', req.headers['foo']); |
| 47 | + } |
| 48 | + |
| 49 | + if (responses_sent == 1) { |
| 50 | + assert.equal('POST', req.method); |
| 51 | + assert.equal('/world', url.parse(req.url).pathname); |
| 52 | + this.close(); |
| 53 | + } |
| 54 | + |
| 55 | + req.on('end', function() { |
| 56 | + res.writeHead(200, {'Content-Type': 'text/plain'}); |
| 57 | + res.write('The path was ' + url.parse(req.url).pathname); |
| 58 | + res.end(); |
| 59 | + responses_sent += 1; |
| 60 | + }); |
| 61 | + |
| 62 | + //assert.equal('127.0.0.1', res.connection.remoteAddress); |
| 63 | +}); |
| 64 | +server.listen(common.PORT); |
| 65 | + |
| 66 | +server.on('listening', function() { |
| 67 | + var agent = new http.Agent({ port: common.PROXY_PORT, maxSockets: 1 }); |
| 68 | + http.get({ |
| 69 | + port: common.PROXY_PORT, |
| 70 | + path: '/hello', |
| 71 | + headers: {'Accept': '*/*', 'Foo': 'bar'}, |
| 72 | + agent: agent |
| 73 | + }, function(res) { |
| 74 | + assert.equal(200, res.statusCode); |
| 75 | + responses_recvd += 1; |
| 76 | + res.setEncoding('utf8'); |
| 77 | + res.on('data', function(chunk) { body0 += chunk; }); |
| 78 | + common.debug('Got /hello response'); |
| 79 | + }); |
| 80 | + |
| 81 | + setTimeout(function() { |
| 82 | + var req = http.request({ |
| 83 | + port: common.PROXY_PORT, |
| 84 | + method: 'POST', |
| 85 | + path: '/world', |
| 86 | + agent: agent |
| 87 | + }, function(res) { |
| 88 | + assert.equal(200, res.statusCode); |
| 89 | + responses_recvd += 1; |
| 90 | + res.setEncoding('utf8'); |
| 91 | + res.on('data', function(chunk) { body1 += chunk; }); |
| 92 | + common.debug('Got /world response'); |
| 93 | + }); |
| 94 | + req.end(); |
| 95 | + }, 1); |
| 96 | +}); |
| 97 | + |
| 98 | +process.on('exit', function() { |
| 99 | + common.debug('responses_recvd: ' + responses_recvd); |
| 100 | + assert.equal(2, responses_recvd); |
| 101 | + |
| 102 | + common.debug('responses_sent: ' + responses_sent); |
| 103 | + assert.equal(2, responses_sent); |
| 104 | + |
| 105 | + assert.equal('The path was /hello', body0); |
| 106 | + assert.equal('The path was /world', body1); |
| 107 | +}); |
| 108 | + |
0 commit comments