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'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const http = require('http'); 26const url = require('url'); 27 28const expectedRequests = ['/hello', '/there', '/world']; 29 30const server = http.Server(common.mustCall((req, res) => { 31 assert.strictEqual(expectedRequests.shift(), req.url); 32 33 switch (req.url) { 34 case '/hello': 35 assert.strictEqual(req.method, 'GET'); 36 assert.strictEqual(req.headers.accept, '*/*'); 37 assert.strictEqual(req.headers.foo, 'bar'); 38 assert.strictEqual(req.headers.cookie, 'foo=bar; bar=baz; baz=quux'); 39 break; 40 case '/there': 41 assert.strictEqual(req.method, 'PUT'); 42 assert.strictEqual(req.headers.cookie, 'node=awesome; ta=da'); 43 break; 44 case '/world': 45 assert.strictEqual(req.method, 'POST'); 46 assert.deepStrictEqual(req.headers.cookie, 'abc=123; def=456; ghi=789'); 47 break; 48 default: 49 assert(false, `Unexpected request for ${req.url}`); 50 } 51 52 if (expectedRequests.length === 0) 53 server.close(); 54 55 req.on('end', () => { 56 res.writeHead(200, { 'Content-Type': 'text/plain' }); 57 res.write(`The path was ${url.parse(req.url).pathname}`); 58 res.end(); 59 }); 60 req.resume(); 61}, 3)); 62server.listen(0); 63 64server.on('listening', () => { 65 const agent = new http.Agent({ port: server.address().port, maxSockets: 1 }); 66 const req = http.get({ 67 port: server.address().port, 68 path: '/hello', 69 headers: { 70 Accept: '*/*', 71 Foo: 'bar', 72 Cookie: [ 'foo=bar', 'bar=baz', 'baz=quux' ] 73 }, 74 agent: agent 75 }, common.mustCall((res) => { 76 const cookieHeaders = req._header.match(/^Cookie: .+$/img); 77 assert.deepStrictEqual(cookieHeaders, 78 ['Cookie: foo=bar; bar=baz; baz=quux']); 79 assert.strictEqual(res.statusCode, 200); 80 let body = ''; 81 res.setEncoding('utf8'); 82 res.on('data', (chunk) => { body += chunk; }); 83 res.on('end', common.mustCall(() => { 84 assert.strictEqual(body, 'The path was /hello'); 85 })); 86 })); 87 88 setTimeout(common.mustCall(() => { 89 const req = http.request({ 90 port: server.address().port, 91 method: 'PUT', 92 path: '/there', 93 agent: agent 94 }, common.mustCall((res) => { 95 const cookieHeaders = req._header.match(/^Cookie: .+$/img); 96 assert.deepStrictEqual(cookieHeaders, ['Cookie: node=awesome; ta=da']); 97 assert.strictEqual(res.statusCode, 200); 98 let body = ''; 99 res.setEncoding('utf8'); 100 res.on('data', (chunk) => { body += chunk; }); 101 res.on('end', common.mustCall(() => { 102 assert.strictEqual(body, 'The path was /there'); 103 })); 104 })); 105 req.setHeader('Cookie', ['node=awesome', 'ta=da']); 106 req.end(); 107 }), 1); 108 109 setTimeout(common.mustCall(() => { 110 const req = http.request({ 111 port: server.address().port, 112 method: 'POST', 113 path: '/world', 114 headers: [ ['Cookie', 'abc=123'], 115 ['Cookie', 'def=456'], 116 ['Cookie', 'ghi=789'] ], 117 agent: agent 118 }, common.mustCall((res) => { 119 const cookieHeaders = req._header.match(/^Cookie: .+$/img); 120 assert.deepStrictEqual(cookieHeaders, 121 ['Cookie: abc=123', 122 'Cookie: def=456', 123 'Cookie: ghi=789']); 124 assert.strictEqual(res.statusCode, 200); 125 let body = ''; 126 res.setEncoding('utf8'); 127 res.on('data', (chunk) => { body += chunk; }); 128 res.on('end', common.mustCall(() => { 129 assert.strictEqual(body, 'The path was /world'); 130 })); 131 })); 132 req.end(); 133 }), 2); 134}); 135