|
1 | 1 | 'use strict';
|
2 |
| -require('../common'); |
| 2 | +const common = require('../common'); |
3 | 3 | const assert = require('assert');
|
4 | 4 | const http = require('http');
|
5 | 5 | const url = require('url');
|
6 | 6 |
|
7 |
| -let responses_sent = 0; |
8 |
| -let responses_recvd = 0; |
9 |
| -let body0 = ''; |
10 |
| -let body1 = ''; |
| 7 | +const expectedRequests = ['/hello', '/there', '/world']; |
11 | 8 |
|
12 |
| -const server = http.Server(function(req, res) { |
13 |
| - if (responses_sent === 0) { |
14 |
| - assert.strictEqual('GET', req.method); |
15 |
| - assert.strictEqual('/hello', url.parse(req.url).pathname); |
| 9 | +const server = http.Server(common.mustCall(function(req, res) { |
| 10 | + assert.strictEqual(expectedRequests.shift(), req.url); |
16 | 11 |
|
17 |
| - console.dir(req.headers); |
18 |
| - assert.strictEqual(true, 'accept' in req.headers); |
19 |
| - assert.strictEqual('*/*', req.headers['accept']); |
20 |
| - |
21 |
| - assert.strictEqual(true, 'foo' in req.headers); |
22 |
| - assert.strictEqual('bar', req.headers['foo']); |
| 12 | + switch (req.url) { |
| 13 | + case '/hello': |
| 14 | + assert.strictEqual(req.method, 'GET'); |
| 15 | + assert.strictEqual(req.headers['accept'], '*/*'); |
| 16 | + assert.strictEqual(req.headers['foo'], 'bar'); |
| 17 | + assert.strictEqual(req.headers.cookie, 'foo=bar; bar=baz; baz=quux'); |
| 18 | + break; |
| 19 | + case '/there': |
| 20 | + assert.strictEqual(req.method, 'PUT'); |
| 21 | + assert.strictEqual(req.headers.cookie, 'node=awesome; ta=da'); |
| 22 | + break; |
| 23 | + case '/world': |
| 24 | + assert.strictEqual(req.method, 'POST'); |
| 25 | + assert.deepStrictEqual(req.headers.cookie, 'abc=123; def=456; ghi=789'); |
| 26 | + break; |
| 27 | + default: |
| 28 | + assert(false, `Unexpected request for ${req.url}`); |
23 | 29 | }
|
24 | 30 |
|
25 |
| - if (responses_sent === 1) { |
26 |
| - assert.strictEqual('POST', req.method); |
27 |
| - assert.strictEqual('/world', url.parse(req.url).pathname); |
| 31 | + if (expectedRequests.length === 0) |
28 | 32 | this.close();
|
29 |
| - } |
30 | 33 |
|
31 | 34 | req.on('end', function() {
|
32 | 35 | res.writeHead(200, {'Content-Type': 'text/plain'});
|
33 | 36 | res.write('The path was ' + url.parse(req.url).pathname);
|
34 | 37 | res.end();
|
35 |
| - responses_sent += 1; |
36 | 38 | });
|
37 | 39 | req.resume();
|
38 |
| - |
39 |
| - //assert.strictEqual('127.0.0.1', res.connection.remoteAddress); |
40 |
| -}); |
| 40 | +}, 3)); |
41 | 41 | server.listen(0);
|
42 | 42 |
|
43 | 43 | server.on('listening', function() {
|
44 | 44 | const agent = new http.Agent({ port: this.address().port, maxSockets: 1 });
|
45 |
| - http.get({ |
| 45 | + const req = http.get({ |
46 | 46 | port: this.address().port,
|
47 | 47 | path: '/hello',
|
48 |
| - headers: {'Accept': '*/*', 'Foo': 'bar'}, |
| 48 | + headers: { |
| 49 | + Accept: '*/*', |
| 50 | + Foo: 'bar', |
| 51 | + Cookie: [ 'foo=bar', 'bar=baz', 'baz=quux' ] |
| 52 | + }, |
49 | 53 | agent: agent
|
50 |
| - }, function(res) { |
51 |
| - assert.strictEqual(200, res.statusCode); |
52 |
| - responses_recvd += 1; |
| 54 | + }, common.mustCall(function(res) { |
| 55 | + const cookieHeaders = req._header.match(/^Cookie: .+$/img); |
| 56 | + assert.deepStrictEqual(cookieHeaders, |
| 57 | + ['Cookie: foo=bar; bar=baz; baz=quux']); |
| 58 | + assert.strictEqual(res.statusCode, 200); |
| 59 | + let body = ''; |
53 | 60 | res.setEncoding('utf8');
|
54 |
| - res.on('data', function(chunk) { body0 += chunk; }); |
55 |
| - console.error('Got /hello response'); |
56 |
| - }); |
| 61 | + res.on('data', function(chunk) { body += chunk; }); |
| 62 | + res.on('end', common.mustCall(function() { |
| 63 | + assert.strictEqual(body, 'The path was /hello'); |
| 64 | + })); |
| 65 | + })); |
57 | 66 |
|
58 |
| - setTimeout(function() { |
| 67 | + setTimeout(common.mustCall(function() { |
| 68 | + const req = http.request({ |
| 69 | + port: server.address().port, |
| 70 | + method: 'PUT', |
| 71 | + path: '/there', |
| 72 | + agent: agent |
| 73 | + }, common.mustCall(function(res) { |
| 74 | + const cookieHeaders = req._header.match(/^Cookie: .+$/img); |
| 75 | + assert.deepStrictEqual(cookieHeaders, ['Cookie: node=awesome; ta=da']); |
| 76 | + assert.strictEqual(res.statusCode, 200); |
| 77 | + let body = ''; |
| 78 | + res.setEncoding('utf8'); |
| 79 | + res.on('data', function(chunk) { body += chunk; }); |
| 80 | + res.on('end', common.mustCall(function() { |
| 81 | + assert.strictEqual(body, 'The path was /there'); |
| 82 | + })); |
| 83 | + })); |
| 84 | + req.setHeader('Cookie', ['node=awesome', 'ta=da']); |
| 85 | + req.end(); |
| 86 | + }), 1); |
| 87 | + |
| 88 | + setTimeout(common.mustCall(function() { |
59 | 89 | const req = http.request({
|
60 | 90 | port: server.address().port,
|
61 | 91 | method: 'POST',
|
62 | 92 | path: '/world',
|
| 93 | + headers: [ ['Cookie', 'abc=123'], |
| 94 | + ['Cookie', 'def=456'], |
| 95 | + ['Cookie', 'ghi=789'] ], |
63 | 96 | agent: agent
|
64 |
| - }, function(res) { |
65 |
| - assert.strictEqual(200, res.statusCode); |
66 |
| - responses_recvd += 1; |
| 97 | + }, common.mustCall(function(res) { |
| 98 | + const cookieHeaders = req._header.match(/^Cookie: .+$/img); |
| 99 | + assert.deepStrictEqual(cookieHeaders, |
| 100 | + ['Cookie: abc=123', |
| 101 | + 'Cookie: def=456', |
| 102 | + 'Cookie: ghi=789']); |
| 103 | + assert.strictEqual(res.statusCode, 200); |
| 104 | + let body = ''; |
67 | 105 | res.setEncoding('utf8');
|
68 |
| - res.on('data', function(chunk) { body1 += chunk; }); |
69 |
| - console.error('Got /world response'); |
70 |
| - }); |
| 106 | + res.on('data', function(chunk) { body += chunk; }); |
| 107 | + res.on('end', common.mustCall(function() { |
| 108 | + assert.strictEqual(body, 'The path was /world'); |
| 109 | + })); |
| 110 | + })); |
71 | 111 | req.end();
|
72 |
| - }, 1); |
73 |
| -}); |
74 |
| - |
75 |
| -process.on('exit', function() { |
76 |
| - console.error('responses_recvd: ' + responses_recvd); |
77 |
| - assert.strictEqual(2, responses_recvd); |
78 |
| - |
79 |
| - console.error('responses_sent: ' + responses_sent); |
80 |
| - assert.strictEqual(2, responses_sent); |
81 |
| - |
82 |
| - assert.strictEqual('The path was /hello', body0); |
83 |
| - assert.strictEqual('The path was /world', body1); |
| 112 | + }), 2); |
84 | 113 | });
|
0 commit comments