Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / test / helpers / http.js
1 /*
2  * http.js: Top level include for node-http-proxy http helpers
3  *
4  * (C) 2010 Nodejitsu Inc.
5  * MIT LICENCE
6  *
7  */
8
9 var assert = require('assert'),
10     http = require('http'),
11     https = require('https'),
12     url = require('url'),
13     async = require('async'),
14     helpers = require('./index'),
15     protocols = helpers.protocols,
16     httpProxy = require('../../lib/node-http-proxy');
17
18 //
19 // ### function createServerPair (options, callback)
20 // #### @options {Object} Options to create target and proxy server.
21 // #### @callback {function} Continuation to respond to when complete.
22 //
23 // Creates http target and proxy servers
24 //
25 exports.createServerPair = function (options, callback) {
26   async.series([
27     //
28     // 1. Create the target server
29     //
30     function createTarget(next) {
31       exports.createServer(options.target, next);
32     },
33     //
34     // 2. Create the proxy server
35     //
36     function createTarget(next) {
37       exports.createProxyServer(options.proxy, next);
38     }
39   ], callback);
40 };
41
42 //
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.
49 //
50 // Creates a target server that the tests will proxy to.
51 //
52 exports.createServer = function (options, callback) {
53   //
54   // Request handler to use in either `http`
55   // or `https` server.
56   //
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]);
61       });
62     }
63
64     if (options.outputHeaders) {
65       Object.keys(options.outputHeaders).forEach(function (header) {
66         res.setHeader(header, options.outputHeaders[header]);
67       });
68     }
69
70     setTimeout(function() {
71       res.writeHead(200, { 'Content-Type': 'text/plain' });
72       res.write(options.output || 'hello proxy'); 
73       res.end(); 
74     }, options.latency || 1);
75   }
76
77   var server = protocols.target === 'https'
78     ? https.createServer(helpers.https, requestHandler)
79     : http.createServer(requestHandler);
80
81   server.listen(options.port, function () {
82     callback(null, this);
83   });
84 };
85
86 //
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.
94 //
95 // Creates a proxy server that the tests will request against.
96 //
97 exports.createProxyServer = function (options, callback) {
98   if (!options.latency) {
99     if (protocols.proxy === 'https') {
100       options.proxy.https = helpers.https;
101     }
102     options.proxy.rejectUnauthorized = false;
103
104     return httpProxy
105       .createServer(options.proxy)
106       .listen(options.port, function () {
107         callback(null, this);
108       });
109   }
110
111   var server,
112       proxy;
113
114   proxy = options.routing
115     ? new httpProxy.RoutingProxy(options.proxy)
116     : new httpProxy.HttpProxy(options.proxy);
117
118   //
119   // Request handler to use in either `http`
120   // or `https` server.
121   //
122   function requestHandler(req, res) {
123     var buffer = httpProxy.buffer(req);
124
125     if (options.outputHeaders) {
126       Object.keys(options.outputHeaders).forEach(function (header) {
127         res.setHeader(header, options.outputHeaders[header]);
128       });
129     }
130     setTimeout(function () {
131       //
132       // Setup options dynamically for `RoutingProxy.prototype.proxyRequest`
133       // or `HttpProxy.prototype.proxyRequest`.
134       //
135       buffer = options.routing ? { buffer: buffer } : buffer;
136       proxy.proxyRequest(req, res, buffer);
137     }, options.latency);
138   }
139
140   server = protocols.proxy === 'https'
141     ? https.createServer(helpers.https, requestHandler)
142     : http.createServer(requestHandler);
143
144   server.listen(options.port, function () {
145     callback(null, this);
146   });
147 };
148
149 //
150 // ### function assignPortsToRoutes (routes)
151 // #### @routes {Object} Routing table to assign ports to
152 //
153 // Assigns dynamic ports to the `routes` for runtime testing.
154 //
155 exports.assignPortsToRoutes = function (routes) {
156   Object.keys(routes).forEach(function (source) {
157     routes[source] = routes[source].replace('{PORT}', helpers.nextPort);
158   });
159
160   return routes;
161 };
162
163 //
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.
168 //
169 // Returns an Array of fully-parsed URLs for the source and
170 // target of `options.routes`.
171 //
172 exports.parseRoutes = function (options) {
173   var protocol = options.protocol || 'http',
174       routes = options.routes;
175
176   return Object.keys(routes).map(function (source) {
177     return {
178       source: url.parse(protocol + '://' + source),
179       target: url.parse(protocol + '://' + routes[source])
180     };
181   });
182 };