Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / test / macros / ws.js
1 /*
2  * ws.js: Macros for proxying Websocket requests
3  *
4  * (C) 2010 Nodejitsu Inc.
5  * MIT LICENCE
6  *
7  */
8
9 var assert = require('assert'),
10     fs = require('fs'),
11     async = require('async'),
12     io = require('socket.io-client'),
13     WebSocket = require('ws'),
14     helpers = require('../helpers/index');
15
16 //
17 // ### function assertSendRecieve (options)
18 // #### @options {Object} Options for creating this assertion.
19 // ####    @raw    {boolean} Enables raw `ws.WebSocket`.
20 // ####    @uri    {string}  URI of the proxy server.
21 // ####    @input  {string}  Input to assert sent to the target ws server.
22 // ####    @output {string}  Output to assert from the taget ws server.
23 //
24 // Creates a `socket.io` or raw `WebSocket` connection and asserts that
25 // `options.input` is sent to and `options.output` is received from the
26 // connection.
27 //
28 exports.assertSendReceive = function (options) {
29   if (!options.raw) {
30     return {
31       topic: function () {
32         var socket = io.connect(options.uri);
33         socket.on('outgoing', this.callback.bind(this, null));
34         socket.emit('incoming', options.input);
35       },
36       "should send input and receive output": function (_, data) {
37         assert.equal(data, options.output);
38       }
39     };
40   }
41
42   return {
43     topic: function () {
44       var socket = new WebSocket(options.uri);
45       socket.on('message', this.callback.bind(this, null));
46       socket.on('open', function () {
47         socket.send(options.input);
48       });
49     },
50     "should send input and recieve output": function (_, data, flags) {
51       assert.equal(data, options.output);
52     }
53   };
54 };
55
56 //
57 // ### function assertProxied (options)
58 // #### @options {Object} Options for this test
59 // ####    @latency {number}  Latency in milliseconds for the proxy server.
60 // ####    @ports   {Object}  Ports for the request (target, proxy).
61 // ####    @input   {string}  Input to assert sent to the target ws server.
62 // ####    @output  {string}  Output to assert from the taget ws server.
63 // ####    @raw     {boolean} Enables raw `ws.Server` usage.
64 //
65 // Creates a complete end-to-end test for requesting against an
66 // http proxy.
67 //
68 exports.assertProxied = function (options) {
69   options = options || {};
70
71   var ports    = options.ports    || helpers.nextPortPair,
72       input    = options.input    || 'hello world to ' + ports.target,
73       output   = options.output   || 'hello world from ' + ports.target,
74       protocol = helpers.protocols.proxy;
75
76   if (options.raw) {
77     protocol = helpers.protocols.proxy === 'https'
78       ? 'wss'
79       : 'ws';
80   }
81
82   return {
83     topic: function () {
84       helpers.ws.createServerPair({
85         target: {
86           input: input,
87           output: output,
88           port: ports.target,
89           raw: options.raw
90         },
91         proxy: {
92           latency: options.latency,
93           port: ports.proxy,
94           proxy: {
95             target: {
96               https: helpers.protocols.target === 'https',
97               host: '127.0.0.1',
98               port: ports.target
99             }
100           }
101         }
102       }, this.callback);
103     },
104     "the proxy Websocket connection": exports.assertSendReceive({
105       uri: protocol + '://127.0.0.1:' + ports.proxy,
106       input: input,
107       output: output,
108       raw: options.raw
109     })
110   };
111 };
112
113 //
114 // ### function assertProxiedtoRoutes (options, nested)
115 // #### @options {Object} Options for this ProxyTable-based test
116 // ####    @raw          {boolean}       Enables ws.Server usage.
117 // ####    @routes       {Object|string} Routes to use for the proxy.
118 // ####    @hostnameOnly {boolean}       Enables hostnameOnly routing.
119 // #### @nested  {Object} Nested vows to add to the returned context.
120 //
121 // Creates a complete end-to-end test for requesting against an
122 // http proxy using `options.routes`:
123 //
124 // 1. Creates target servers for all routes in `options.routes.`
125 // 2. Creates a proxy server.
126 // 3. Ensure Websocket connections to the proxy server for all route targets
127 //    can send input and recieve output.
128 //
129 exports.assertProxiedToRoutes = function (options, nested) {
130   //
131   // Assign dynamic ports to the routes to use.
132   //
133   options.routes = helpers.http.assignPortsToRoutes(options.routes);
134
135   //
136   // Parse locations from routes for making assertion requests.
137   //
138   var locations = helpers.http.parseRoutes(options),
139       protocol = helpers.protocols.proxy,
140       port = helpers.nextPort,
141       context,
142       proxy;
143
144   if (options.raw) {
145     protocol = helpers.protocols.proxy === 'https'
146       ? 'wss'
147       : 'ws';
148   }
149
150   if (options.filename) {
151     //
152     // If we've been passed a filename write the routes to it
153     // and setup the proxy options to use that file.
154     //
155     fs.writeFileSync(options.filename, JSON.stringify({ router: options.routes }));
156     proxy = { router: options.filename };
157   }
158   else {
159     //
160     // Otherwise just use the routes themselves.
161     //
162     proxy = {
163       hostnameOnly: options.hostnameOnly,
164       router: options.routes
165     };
166   }
167
168   //
169   // Create the test context which creates all target
170   // servers for all routes and a proxy server.
171   //
172   context = {
173     topic: function () {
174       var that = this;
175
176       async.waterfall([
177         //
178         // 1. Create all the target servers
179         //
180         async.apply(
181           async.forEach,
182           locations,
183           function createRouteTarget(location, next) {
184             helpers.ws.createServer({
185               raw: options.raw,
186               port: location.target.port,
187               output: 'hello from ' + location.source.href,
188               input: 'hello to ' + location.source.href
189             }, next);
190           }
191         ),
192         //
193         // 2. Create the proxy server
194         //
195         async.apply(
196           helpers.http.createProxyServer,
197           {
198             port: port,
199             latency: options.latency,
200             routing: true,
201             proxy: proxy
202           }
203         )
204       ], function (_, server) {
205         //
206         // 3. Set the proxy server for later use
207         //
208         that.proxyServer = server;
209         that.callback();
210       });
211
212       //
213       // 4. Assign the port to the context for later use
214       //
215       this.port = port;
216     }
217   };
218
219   //
220   // Add test assertions for each of the route locations.
221   //
222   locations.forEach(function (location) {
223     context[location.source.href] = exports.assertSendRecieve({
224       uri: protocol + '://127.0.0.1:' + port + location.source.path,
225       output: 'hello from ' + location.source.href,
226       input: 'hello to ' + location.source.href,
227       raw: options.raw
228     });
229   });
230
231   return context;
232 };