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'; 23require('../common'); 24const assert = require('assert'); 25const http = require('http'); 26const url = require('url'); 27 28const cookies = [ 29 'session_token=; path=/; expires=Sun, 15-Sep-2030 13:48:52 GMT', 30 'prefers_open_id=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT', 31]; 32 33const headers = { 'content-type': 'text/plain', 34 'set-cookie': cookies, 35 'hello': 'world' }; 36 37const backend = http.createServer(function(req, res) { 38 console.error('backend request'); 39 res.writeHead(200, headers); 40 res.write('hello world\n'); 41 res.end(); 42}); 43 44const proxy = http.createServer(function(req, res) { 45 console.error(`proxy req headers: ${JSON.stringify(req.headers)}`); 46 http.get({ 47 port: backend.address().port, 48 path: url.parse(req.url).pathname 49 }, function(proxy_res) { 50 51 console.error(`proxy res headers: ${JSON.stringify(proxy_res.headers)}`); 52 53 assert.strictEqual(proxy_res.headers.hello, 'world'); 54 assert.strictEqual(proxy_res.headers['content-type'], 'text/plain'); 55 assert.deepStrictEqual(proxy_res.headers['set-cookie'], cookies); 56 57 res.writeHead(proxy_res.statusCode, proxy_res.headers); 58 59 proxy_res.on('data', function(chunk) { 60 res.write(chunk); 61 }); 62 63 proxy_res.on('end', function() { 64 res.end(); 65 console.error('proxy res'); 66 }); 67 }); 68}); 69 70let body = ''; 71 72let nlistening = 0; 73function startReq() { 74 nlistening++; 75 if (nlistening < 2) return; 76 77 http.get({ 78 port: proxy.address().port, 79 path: '/test' 80 }, function(res) { 81 console.error('got res'); 82 assert.strictEqual(res.statusCode, 200); 83 84 assert.strictEqual(res.headers.hello, 'world'); 85 assert.strictEqual(res.headers['content-type'], 'text/plain'); 86 assert.deepStrictEqual(res.headers['set-cookie'], cookies); 87 88 res.setEncoding('utf8'); 89 res.on('data', function(chunk) { body += chunk; }); 90 res.on('end', function() { 91 proxy.close(); 92 backend.close(); 93 console.error('closed both'); 94 }); 95 }); 96 console.error('client req'); 97} 98 99console.error('listen proxy'); 100proxy.listen(0, startReq); 101 102console.error('listen backend'); 103backend.listen(0, startReq); 104 105process.on('exit', function() { 106 assert.strictEqual(body, 'hello world\n'); 107}); 108