koichik | 4cf931d | 2011-08-14 07:56:41 | [diff] [blame^] | 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.createServer(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 | |
| 65 | server.listen(common.PORT, function() { |
| 66 | var client = http.createClient(common.PORT); |
| 67 | var req = client.request('/hello', {'Accept': '*/*', 'Foo': 'bar'}); |
| 68 | setTimeout(function() { |
| 69 | req.end(); |
| 70 | }, 100); |
| 71 | req.on('response', function(res) { |
| 72 | assert.equal(200, res.statusCode); |
| 73 | responses_recvd += 1; |
| 74 | res.setEncoding('utf8'); |
| 75 | res.on('data', function(chunk) { body0 += chunk; }); |
| 76 | common.debug('Got /hello response'); |
| 77 | }); |
| 78 | |
| 79 | setTimeout(function() { |
| 80 | var req = client.request('POST', '/world'); |
| 81 | req.end(); |
| 82 | req.on('response', function(res) { |
| 83 | assert.equal(200, res.statusCode); |
| 84 | responses_recvd += 1; |
| 85 | res.setEncoding('utf8'); |
| 86 | res.on('data', function(chunk) { body1 += chunk; }); |
| 87 | common.debug('Got /world response'); |
| 88 | }); |
| 89 | }, 1); |
| 90 | }); |
| 91 | |
| 92 | process.on('exit', function() { |
| 93 | common.debug('responses_recvd: ' + responses_recvd); |
| 94 | assert.equal(2, responses_recvd); |
| 95 | |
| 96 | common.debug('responses_sent: ' + responses_sent); |
| 97 | assert.equal(2, responses_sent); |
| 98 | |
| 99 | assert.equal('The path was /hello', body0); |
| 100 | assert.equal('The path was /world', body1); |
| 101 | }); |