1
+ /*
2
+ node-http-proxy-test.js: http proxy for node.js
3
+
4
+ Copyright (c) 2010 Charlie Robbins, Marak Squires and Fedor Indutny
5
+
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:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
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.
24
+
25
+ */
26
+
27
+ var vows = require ( 'vows' ) ,
28
+ util = require ( 'util' ) ,
29
+ request = require ( 'request' ) ,
30
+ assert = require ( 'assert' ) ,
31
+ io = require ( 'socket.io' ) ,
32
+ utils = require ( 'socket.io/lib/socket.io/utils' ) ,
33
+ websocket = require ( './../vendor/websocket' ) ,
34
+ helpers = require ( './helpers' ) ;
35
+
36
+ var runner = new helpers . TestRunner ( ) ;
37
+
38
+ vows . describe ( 'node-http-proxy/websocket' ) . addBatch ( {
39
+ "When using server created by httpProxy.createServer()" : {
40
+ "with no latency" : {
41
+ "when an inbound message is sent from a WebSocket client" : {
42
+ topic : function ( ) {
43
+ var that = this ;
44
+
45
+ runner . startTargetServer ( 8130 , 'hello websocket' , function ( err , target ) {
46
+ var socket = io . listen ( target ) ;
47
+
48
+ socket . on ( 'connection' , function ( client ) {
49
+ client . on ( 'message' , function ( msg ) {
50
+ that . callback ( null , msg ) ;
51
+ } ) ;
52
+ } ) ;
53
+
54
+ runner . startProxyServer ( 8131 , 8130 , 'localhost' , function ( err , proxy ) {
55
+ //
56
+ // Setup the web socket against our proxy
57
+ //
58
+ var ws = new websocket . WebSocket ( 'ws://localhost:8131/socket.io/websocket/' , 'borf' ) ;
59
+
60
+ ws . on ( 'open' , function ( ) {
61
+ ws . send ( utils . encode ( 'from client' ) ) ;
62
+ } ) ;
63
+ } ) ;
64
+ } )
65
+ } ,
66
+ "the target server should receive the message" : function ( err , msg ) {
67
+ assert . equal ( msg , 'from client' ) ;
68
+ }
69
+ } ,
70
+ "when an outbound message is sent from the target server" : {
71
+ topic : function ( ) {
72
+ var that = this ;
73
+
74
+ runner . startTargetServer ( 8132 , 'hello websocket' , function ( err , target ) {
75
+ var socket = io . listen ( target ) ;
76
+
77
+ socket . on ( 'connection' , function ( client ) {
78
+ socket . broadcast ( 'from server' ) ;
79
+ } ) ;
80
+
81
+ runner . startProxyServer ( 8133 , 8132 , 'localhost' , function ( err , proxy ) {
82
+ //
83
+ // Setup the web socket against our proxy
84
+ //
85
+ var ws = new websocket . WebSocket ( 'ws://localhost:8133/socket.io/websocket/' , 'borf' ) ;
86
+
87
+ ws . on ( 'message' , function ( msg ) {
88
+ msg = utils . decode ( msg ) ;
89
+ if ( ! / \d + / . test ( msg ) ) {
90
+ that . callback ( null , msg ) ;
91
+ }
92
+ } ) ;
93
+ } ) ;
94
+ } )
95
+ } ,
96
+ "the client should receive the message" : function ( err , msg ) {
97
+ assert . equal ( msg , 'from server' ) ;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ } ) . addBatch ( {
103
+ "When the tests are over" : {
104
+ topic : function ( ) {
105
+ return runner . closeServers ( ) ;
106
+ } ,
107
+ "the servers should clean up" : function ( ) {
108
+ assert . isTrue ( true ) ;
109
+ }
110
+ }
111
+ } ) . export ( module ) ;
0 commit comments