Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / test / helpers / ws.js
1 /*
2  * ws.js: Top level include for node-http-proxy websocket helpers
3  *
4  * (C) 2010 Nodejitsu Inc.
5  * MIT LICENCE
6  *
7  */
8
9 var assert = require('assert'),
10     https = require('https'),
11     async = require('async'),
12     io = require('socket.io'),
13     ws = require('ws'),
14     helpers = require('./index'),
15     protocols = helpers.protocols,
16     http = require('./http');
17
18 //
19 // ### function createServerPair (options, callback)
20 // #### @options {Object} Options to create target and proxy server.
21 // ####    @target {Object} Options for the target server.
22 // ####    @proxy  {Object} Options for the proxy server.
23 // #### @callback {function} Continuation to respond to when complete.
24 //
25 // Creates http target and proxy servers
26 //
27 exports.createServerPair = function (options, callback) {
28   async.series([
29     //
30     // 1. Create the target server
31     //
32     function createTarget(next) {
33       exports.createServer(options.target, next);
34     },
35     //
36     // 2. Create the proxy server
37     //
38     function createTarget(next) {
39       http.createProxyServer(options.proxy, next);
40     }
41   ], callback);
42 };
43
44 //
45 // ### function createServer (options, callback)
46 // #### @options {Object} Options for creating the socket.io or ws server.
47 // ####    @raw   {boolean} Enables ws.Websocket server.
48 //
49 // Creates a socket.io or ws server using the specified `options`.
50 //
51 exports.createServer = function (options, callback) {
52   return options.raw
53     ? exports.createWsServer(options, callback)
54     : exports.createSocketIoServer(options, callback);
55 };
56
57 //
58 // ### function createSocketIoServer (options, callback)
59 // #### @options {Object} Options for creating the socket.io server
60 // ####    @port   {number} Port to listen on
61 // ####    @input  {string} Input to expect from the only socket
62 // ####    @output {string} Output to send the only socket
63 //
64 // Creates a socket.io server on the specified `options.port` which
65 // will expect `options.input` and then send `options.output`.
66 //
67 exports.createSocketIoServer = function (options, callback) {
68   var server = protocols.target === 'https'
69     ? io.listen(options.port, helpers.https, callback)
70     : io.listen(options.port, callback);
71
72   server.sockets.on('connection', function (socket) {
73     socket.on('incoming', function (data) {
74       assert.equal(data, options.input);
75       socket.emit('outgoing', options.output);
76     });
77   });
78 };
79
80 //
81 // ### function createWsServer (options, callback)
82 // #### @options {Object} Options for creating the ws.Server instance
83 // ####    @port   {number} Port to listen on
84 // ####    @input  {string} Input to expect from the only socket
85 // ####    @output {string} Output to send the only socket
86 //
87 // Creates a ws.Server instance on the specified `options.port` which
88 // will expect `options.input` and then send `options.output`.
89 //
90 exports.createWsServer = function (options, callback) {
91   var server,
92       wss;
93
94   if (protocols.target === 'https') {
95     server = https.createServer(helpers.https, function (req, res) {
96       req.writeHead(200);
97       req.end();
98     }).listen(options.port, callback);
99
100     wss = new ws.Server({ server: server });
101   }
102   else {
103     wss = new ws.Server({ port: options.port }, callback);
104   }
105
106   wss.on('connection', function (socket) {
107     socket.on('message', function (data) {
108       assert.equal(data, options.input);
109       socket.send(options.output);
110     });
111   });
112 };