@@ -895,7 +895,7 @@ describe('WebSocket', () => {
895
895
} ) ;
896
896
} ) ;
897
897
898
- it ( 'fails if server sends an invalid subprotocol' , ( done ) => {
898
+ it ( 'fails if server sends an invalid subprotocol (1/2) ' , ( done ) => {
899
899
const wss = new WebSocket . Server ( {
900
900
handleProtocols : ( ) => 'baz' ,
901
901
server
@@ -914,6 +914,36 @@ describe('WebSocket', () => {
914
914
} ) ;
915
915
} ) ;
916
916
917
+ it ( 'fails if server sends an invalid subprotocol (2/2)' , ( done ) => {
918
+ server . once ( 'upgrade' , ( req , socket ) => {
919
+ const key = crypto
920
+ . createHash ( 'sha1' )
921
+ . update ( req . headers [ 'sec-websocket-key' ] + GUID )
922
+ . digest ( 'base64' ) ;
923
+
924
+ socket . end (
925
+ 'HTTP/1.1 101 Switching Protocols\r\n' +
926
+ 'Upgrade: websocket\r\n' +
927
+ 'Connection: Upgrade\r\n' +
928
+ `Sec-WebSocket-Accept: ${ key } \r\n` +
929
+ 'Sec-WebSocket-Protocol:\r\n' +
930
+ '\r\n'
931
+ ) ;
932
+ } ) ;
933
+
934
+ const ws = new WebSocket ( `ws://localhost:${ server . address ( ) . port } ` , [
935
+ 'foo' ,
936
+ 'bar'
937
+ ] ) ;
938
+
939
+ ws . on ( 'open' , ( ) => done ( new Error ( "Unexpected 'open' event" ) ) ) ;
940
+ ws . on ( 'error' , ( err ) => {
941
+ assert . ok ( err instanceof Error ) ;
942
+ assert . strictEqual ( err . message , 'Server sent an invalid subprotocol' ) ;
943
+ ws . on ( 'close' , ( ) => done ( ) ) ;
944
+ } ) ;
945
+ } ) ;
946
+
917
947
it ( 'fails if server sends no subprotocol' , ( done ) => {
918
948
const wss = new WebSocket . Server ( {
919
949
handleProtocols ( ) { } ,
0 commit comments