5
5
*/
6
6
const request = require ( 'supertest' ) ;
7
7
const sockjs = require ( 'sockjs' ) ;
8
+ const SockJS = require ( 'sockjs-client/dist/sockjs' ) ;
8
9
const SockJSServer = require ( '../../lib/servers/SockJSServer' ) ;
9
10
const config = require ( '../fixtures/simple-config/webpack.config' ) ;
10
11
const testServer = require ( '../helpers/test-server' ) ;
@@ -77,6 +78,7 @@ describe('serverMode option', () => {
77
78
78
79
describe ( 'as a class (custom "sockjs" implementation)' , ( ) => {
79
80
it ( 'uses supplied server implementation' , ( done ) => {
81
+ let sockPath ;
80
82
server = testServer . start (
81
83
config ,
82
84
{
@@ -102,7 +104,7 @@ describe('serverMode option', () => {
102
104
prefix : this . server . sockPath ,
103
105
} ) ;
104
106
105
- expect ( server . options . sockPath ) . toEqual ( '/foo/test/bar/' ) ;
107
+ sockPath = server . options . sockPath ;
106
108
}
107
109
108
110
send ( connection , message ) {
@@ -114,11 +116,20 @@ describe('serverMode option', () => {
114
116
}
115
117
116
118
onConnection ( f ) {
117
- this . socket . on ( 'connection' , f ) ;
119
+ this . socket . on ( 'connection' , ( connection ) => {
120
+ f ( connection , connection . headers ) ;
121
+ } ) ;
122
+ }
123
+
124
+ onConnectionClose ( connection , f ) {
125
+ connection . on ( 'close' , f ) ;
118
126
}
119
127
} ,
120
128
} ,
121
- done
129
+ ( ) => {
130
+ expect ( sockPath ) . toEqual ( '/foo/test/bar/' ) ;
131
+ done ( ) ;
132
+ }
122
133
) ;
123
134
} ) ;
124
135
} ) ;
@@ -137,4 +148,79 @@ describe('serverMode option', () => {
137
148
} ) . toThrow ( / s e r v e r M o d e m u s t b e a s t r i n g / ) ;
138
149
} ) ;
139
150
} ) ;
151
+
152
+ describe ( 'with a bad host header' , ( ) => {
153
+ beforeAll ( ( done ) => {
154
+ server = testServer . start (
155
+ config ,
156
+ {
157
+ port,
158
+ serverMode : class MySockJSServer extends BaseServer {
159
+ constructor ( serv ) {
160
+ super ( serv ) ;
161
+ this . socket = sockjs . createServer ( {
162
+ // Use provided up-to-date sockjs-client
163
+ sockjs_url : '/__webpack_dev_server__/sockjs.bundle.js' ,
164
+ // Limit useless logs
165
+ log : ( severity , line ) => {
166
+ if ( severity === 'error' ) {
167
+ this . server . log . error ( line ) ;
168
+ } else {
169
+ this . server . log . debug ( line ) ;
170
+ }
171
+ } ,
172
+ } ) ;
173
+
174
+ this . socket . installHandlers ( this . server . listeningApp , {
175
+ prefix : this . server . sockPath ,
176
+ } ) ;
177
+ }
178
+
179
+ send ( connection , message ) {
180
+ connection . write ( message ) ;
181
+ }
182
+
183
+ close ( connection ) {
184
+ connection . close ( ) ;
185
+ }
186
+
187
+ onConnection ( f ) {
188
+ this . socket . on ( 'connection' , ( connection ) => {
189
+ f ( connection , {
190
+ host : null ,
191
+ } ) ;
192
+ } ) ;
193
+ }
194
+
195
+ onConnectionClose ( connection , f ) {
196
+ connection . on ( 'close' , f ) ;
197
+ }
198
+ } ,
199
+ } ,
200
+ done
201
+ ) ;
202
+ } ) ;
203
+
204
+ it ( 'results in an error' , ( done ) => {
205
+ const data = [ ] ;
206
+ const client = new SockJS ( `http://localhost:${ port } /sockjs-node` ) ;
207
+
208
+ client . onopen = ( ) => {
209
+ data . push ( 'open' ) ;
210
+ } ;
211
+
212
+ client . onmessage = ( e ) => {
213
+ data . push ( e . data ) ;
214
+ } ;
215
+
216
+ client . onclose = ( ) => {
217
+ data . push ( 'close' ) ;
218
+ } ;
219
+
220
+ setTimeout ( ( ) => {
221
+ expect ( data ) . toMatchSnapshot ( ) ;
222
+ done ( ) ;
223
+ } , 5000 ) ;
224
+ } ) ;
225
+ } ) ;
140
226
} ) ;
0 commit comments