2 * http.js: Top level include for node-http-proxy http helpers
4 * (C) 2010 Nodejitsu Inc.
9 var assert = require('assert'),
10 http = require('http'),
11 https = require('https'),
13 async = require('async'),
14 helpers = require('./index'),
15 protocols = helpers.protocols,
16 httpProxy = require('../../lib/node-http-proxy');
19 // ### function createServerPair (options, callback)
20 // #### @options {Object} Options to create target and proxy server.
21 // #### @callback {function} Continuation to respond to when complete.
23 // Creates http target and proxy servers
25 exports.createServerPair = function (options, callback) {
28 // 1. Create the target server
30 function createTarget(next) {
31 exports.createServer(options.target, next);
34 // 2. Create the proxy server
36 function createTarget(next) {
37 exports.createProxyServer(options.proxy, next);
43 // ### function createServer (options, callback)
44 // #### @options {Object} Options for creatig an http server.
45 // #### @port {number} Port to listen on
46 // #### @output {string} String to write to each HTTP response
47 // #### @headers {Object} Headers to assert are sent by `node-http-proxy`.
48 // #### @callback {function} Continuation to respond to when complete.
50 // Creates a target server that the tests will proxy to.
52 exports.createServer = function (options, callback) {
54 // Request handler to use in either `http`
57 function requestHandler(req, res) {
58 if (options.headers) {
59 Object.keys(options.headers).forEach(function (key) {
60 assert.equal(req.headers[key], options.headers[key]);
64 if (options.outputHeaders) {
65 Object.keys(options.outputHeaders).forEach(function (header) {
66 res.setHeader(header, options.outputHeaders[header]);
70 setTimeout(function() {
71 res.writeHead(200, { 'Content-Type': 'text/plain' });
72 res.write(options.output || 'hello proxy');
74 }, options.latency || 1);
77 var server = protocols.target === 'https'
78 ? https.createServer(helpers.https, requestHandler)
79 : http.createServer(requestHandler);
81 server.listen(options.port, function () {
87 // ### function createProxyServer (options, callback)
88 // #### @options {Object} Options for creatig an http server.
89 // #### @port {number} Port to listen on
90 // #### @latency {number} Latency of this server in milliseconds
91 // #### @proxy {Object} Options to pass to the HttpProxy.
92 // #### @routing {boolean} Enables `httpProxy.RoutingProxy`
93 // #### @callback {function} Continuation to respond to when complete.
95 // Creates a proxy server that the tests will request against.
97 exports.createProxyServer = function (options, callback) {
98 if (!options.latency) {
99 if (protocols.proxy === 'https') {
100 options.proxy.https = helpers.https;
102 options.proxy.rejectUnauthorized = false;
105 .createServer(options.proxy)
106 .listen(options.port, function () {
107 callback(null, this);
114 proxy = options.routing
115 ? new httpProxy.RoutingProxy(options.proxy)
116 : new httpProxy.HttpProxy(options.proxy);
119 // Request handler to use in either `http`
120 // or `https` server.
122 function requestHandler(req, res) {
123 var buffer = httpProxy.buffer(req);
125 if (options.outputHeaders) {
126 Object.keys(options.outputHeaders).forEach(function (header) {
127 res.setHeader(header, options.outputHeaders[header]);
130 setTimeout(function () {
132 // Setup options dynamically for `RoutingProxy.prototype.proxyRequest`
133 // or `HttpProxy.prototype.proxyRequest`.
135 buffer = options.routing ? { buffer: buffer } : buffer;
136 proxy.proxyRequest(req, res, buffer);
140 server = protocols.proxy === 'https'
141 ? https.createServer(helpers.https, requestHandler)
142 : http.createServer(requestHandler);
144 server.listen(options.port, function () {
145 callback(null, this);
150 // ### function assignPortsToRoutes (routes)
151 // #### @routes {Object} Routing table to assign ports to
153 // Assigns dynamic ports to the `routes` for runtime testing.
155 exports.assignPortsToRoutes = function (routes) {
156 Object.keys(routes).forEach(function (source) {
157 routes[source] = routes[source].replace('{PORT}', helpers.nextPort);
164 // ### function parseRoutes (options)
165 // #### @options {Object} Options to use when parsing routes
166 // #### @protocol {string} Protocol to use in the routes
167 // #### @routes {Object} Routes to parse.
169 // Returns an Array of fully-parsed URLs for the source and
170 // target of `options.routes`.
172 exports.parseRoutes = function (options) {
173 var protocol = options.protocol || 'http',
174 routes = options.routes;
176 return Object.keys(routes).map(function (source) {
178 source: url.parse(protocol + '://' + source),
179 target: url.parse(protocol + '://' + routes[source])