2 node-http-proxy-test.js: http proxy for node.js
4 Copyright (c) 2010 Charlie Robbins, Marak Squires and Fedor Indutny
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 var assert = require('assert'),
29 path = require('path'),
30 async = require('async'),
31 request = require('request'),
32 vows = require('vows'),
33 macros = require('../macros'),
34 helpers = require('../helpers');
36 var routeFile = path.join(__dirname, 'config.json');
38 vows.describe(helpers.describe()).addBatch({
39 "With a valid target server": {
41 "and no headers": macros.http.assertProxied(),
42 "and headers": macros.http.assertProxied({
43 request: { headers: { host: 'unknown.com' } }
45 "and request close connection header": macros.http.assertProxied({
46 request: { headers: { connection: "close" } },
47 outputHeaders: { connection: "close" }
49 "and request keep alive connection header": macros.http.assertProxied({
50 request: { headers: { connection: "keep-alive" } },
51 outputHeaders: { connection: "keep-alive" }
53 "and response close connection header": macros.http.assertProxied({
54 request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
55 targetHeaders: { connection: "close" },
56 outputHeaders: { connection: "close" }
58 "and response keep-alive connection header": macros.http.assertProxied({
59 request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
60 targetHeaders: { connection: "keep-alive" },
61 outputHeaders: { connection: "keep-alive" }
63 "and response keep-alive connection header from http 1.0 client": macros.http.assertRawHttpProxied({
64 rawRequest: "GET / HTTP/1.0\r\n\r\n",
65 targetHeaders: { connection: "keep-alive" },
66 match: /connection: close/i
68 "and request keep alive from http 1.0 client": macros.http.assertRawHttpProxied({
69 rawRequest: "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n",
70 targetHeaders: { connection: "keep-alive" },
71 match: /connection: keep-alive/i
73 "and no connection header": macros.http.assertProxied({
74 request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
75 outputHeaders: { connection: "keep-alive" }
77 "and forwarding enabled": macros.http.assertForwardProxied()
80 "and no headers": macros.http.assertProxied({
83 "and response headers": macros.http.assertProxied({
84 targetHeaders: { "x-testheader": "target" },
85 proxyHeaders: { "X-TestHeader": "proxy" },
86 outputHeaders: { "x-testheader": "target" },
90 "and timeout set": macros.http.assertProxied({
96 "With a no valid target server": {
97 "and no latency": macros.http.assertInvalidProxy(),
98 "and latency": macros.http.assertInvalidProxy({