1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var common = require('../common');
23 var assert = require('assert');
24 var http = require('http');
25 var url = require('url');
27 var PROXY_PORT = common.PORT;
28 var BACKEND_PORT = common.PORT + 1;
31 'session_token=; path=/; expires=Sun, 15-Sep-2030 13:48:52 GMT',
32 'prefers_open_id=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT'
35 var headers = {'content-type': 'text/plain',
36 'set-cookie': cookies,
39 var backend = http.createServer(function (req, res) {
40 common.debug('backend request');
41 res.writeHead(200, headers);
42 res.write('hello world\n');
46 var proxy = http.createServer(function (req, res) {
47 common.debug('proxy req headers: ' + JSON.stringify(req.headers));
48 var proxy_req = http.get({
50 path: url.parse(req.url).pathname
51 }, function (proxy_res) {
53 common.debug('proxy res headers: ' + JSON.stringify(proxy_res.headers));
55 assert.equal('world', proxy_res.headers['hello']);
56 assert.equal('text/plain', proxy_res.headers['content-type']);
57 assert.deepEqual(cookies, proxy_res.headers['set-cookie']);
59 res.writeHead(proxy_res.statusCode, proxy_res.headers);
61 proxy_res.on('data', function (chunk) {
65 proxy_res.on('end', function () {
67 common.debug('proxy res');
77 if (nlistening < 2) return;
79 var client = http.get({
80 port: common.PROXY_PORT,
83 common.debug('got res');
84 assert.equal(200, res.statusCode);
86 assert.equal('world', res.headers['hello']);
87 assert.equal('text/plain', res.headers['content-type']);
88 assert.deepEqual(cookies, res.headers['set-cookie']);
90 res.setEncoding('utf8');
91 res.on('data', function (chunk) { body += chunk; });
92 res.on('end', function () {
95 common.debug('closed both');
98 common.debug('client req');
101 common.debug('listen proxy');
102 proxy.listen(PROXY_PORT, startReq);
104 common.debug('listen backend');
105 backend.listen(BACKEND_PORT, startReq);
107 process.on('exit', function () {
108 assert.equal(body, 'hello world\n');