http: concatenate outgoing Cookie headers · nodejs/node@d348077
11'use strict';
2-require('../common');
2+const common = require('../common');
33const assert = require('assert');
44const http = require('http');
55const url = require('url');
667-let responses_sent = 0;
8-let responses_recvd = 0;
9-let body0 = '';
10-let body1 = '';
7+const expectedRequests = ['/hello', '/there', '/world'];
11812-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);
161117-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}`);
2329}
243025-if (responses_sent === 1) {
26-assert.strictEqual('POST', req.method);
27-assert.strictEqual('/world', url.parse(req.url).pathname);
31+if (expectedRequests.length === 0)
2832this.close();
29-}
30333134req.on('end', function() {
3235res.writeHead(200, {'Content-Type': 'text/plain'});
3336res.write('The path was ' + url.parse(req.url).pathname);
3437res.end();
35-responses_sent += 1;
3638});
3739req.resume();
38-39-//assert.strictEqual('127.0.0.1', res.connection.remoteAddress);
40-});
40+}, 3));
4141server.listen(0);
42424343server.on('listening', function() {
4444const agent = new http.Agent({ port: this.address().port, maxSockets: 1 });
45-http.get({
45+const req = http.get({
4646port: this.address().port,
4747path: '/hello',
48-headers: {'Accept': '*/*', 'Foo': 'bar'},
48+headers: {
49+Accept: '*/*',
50+Foo: 'bar',
51+Cookie: [ 'foo=bar', 'bar=baz', 'baz=quux' ]
52+},
4953agent: 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 = '';
5360res.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+}));
576658-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() {
5989const req = http.request({
6090port: server.address().port,
6191method: 'POST',
6292path: '/world',
93+headers: [ ['Cookie', 'abc=123'],
94+['Cookie', 'def=456'],
95+['Cookie', 'ghi=789'] ],
6396agent: 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 = '';
67105res.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+}));
71111req.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);
84113});