55*/
66const request = require ( 'supertest' ) ;
77const sockjs = require ( 'sockjs' ) ;
8+ const SockJS = require ( 'sockjs-client/dist/sockjs' ) ;
89const SockJSServer = require ( '../../lib/servers/SockJSServer' ) ;
910const config = require ( '../fixtures/simple-config/webpack.config' ) ;
1011const testServer = require ( '../helpers/test-server' ) ;
@@ -77,6 +78,7 @@ describe('serverMode option', () => {
7778
7879 describe ( 'as a class (custom "sockjs" implementation)' , ( ) => {
7980 it ( 'uses supplied server implementation' , ( done ) => {
81+ let sockPath ;
8082 server = testServer . start (
8183 config ,
8284 {
@@ -102,7 +104,7 @@ describe('serverMode option', () => {
102104 prefix : this . server . sockPath ,
103105 } ) ;
104106
105- expect ( server . options . sockPath ) . toEqual ( '/foo/test/bar/' ) ;
107+ sockPath = server . options . sockPath ;
106108 }
107109
108110 send ( connection , message ) {
@@ -114,11 +116,20 @@ describe('serverMode option', () => {
114116 }
115117
116118 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 ) ;
118126 }
119127 } ,
120128 } ,
121- done
129+ ( ) => {
130+ expect ( sockPath ) . toEqual ( '/foo/test/bar/' ) ;
131+ done ( ) ;
132+ }
122133 ) ;
123134 } ) ;
124135 } ) ;
@@ -137,4 +148,79 @@ describe('serverMode option', () => {
137148 } ) . toThrow ( / s e r v e r M o d e m u s t b e a s t r i n g / ) ;
138149 } ) ;
139150 } ) ;
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+ } ) ;
140226} ) ;
0 commit comments