@@ -1459,7 +1459,6 @@ describe('server', function () {
1459
1459
var socket = new eioc . Socket ( 'ws://localhost:%d' . s ( port ) , { transports : [ 'websocket' ] } ) ;
1460
1460
socket . on ( 'open' , function ( ) {
1461
1461
for ( var i = 0 ; i < messageCount ; i ++ ) {
1462
- // connection.send('message: ' + i); // works
1463
1462
connection . send ( messagePayload + '|message: ' + i ) ; // does not work
1464
1463
}
1465
1464
var receivedCount = 0 ;
@@ -2514,6 +2513,67 @@ describe('server', function () {
2514
2513
} ) ;
2515
2514
} ) ;
2516
2515
2516
+ describe ( 'cors' , function ( ) {
2517
+ it ( 'should handle OPTIONS requests' , function ( done ) {
2518
+ listen ( { handlePreflightRequest : true } , function ( port ) {
2519
+ request . options ( 'http://localhost:%d/engine.io/default/' . s ( port ) )
2520
+ . set ( 'Origin' , 'http://engine.io' )
2521
+ . query ( { transport : 'polling' } )
2522
+ . end ( function ( res ) {
2523
+ expect ( res . status ) . to . be ( 400 ) ;
2524
+ expect ( res . body . code ) . to . be ( 2 ) ;
2525
+ expect ( res . body . message ) . to . be ( 'Bad handshake method' ) ;
2526
+ expect ( res . header [ 'access-control-allow-credentials' ] ) . to . be ( 'true' ) ;
2527
+ expect ( res . header [ 'access-control-allow-origin' ] ) . to . be ( 'http://engine.io' ) ;
2528
+ done ( ) ;
2529
+ } ) ;
2530
+ } ) ;
2531
+ } ) ;
2532
+
2533
+ it ( 'should not handle OPTIONS requests' , function ( done ) {
2534
+ listen ( { handlePreflightRequest : false } , function ( port ) {
2535
+ request . options ( 'http://localhost:%d/engine.io/default/' . s ( port ) )
2536
+ . set ( 'Origin' , 'http://engine.io' )
2537
+ . query ( { transport : 'polling' } )
2538
+ . end ( function ( res ) {
2539
+ expect ( res . status ) . to . be ( 501 ) ;
2540
+ expect ( res . body . code ) . to . be ( undefined ) ;
2541
+ done ( ) ;
2542
+ } ) ;
2543
+ } ) ;
2544
+ } ) ;
2545
+
2546
+ it ( 'should handle OPTIONS requests with the given function' , function ( done ) {
2547
+ const handlePreflightRequest = ( req , res ) => {
2548
+ let headers = { } ;
2549
+ if ( req . headers . origin ) {
2550
+ headers [ 'Access-Control-Allow-Credentials' ] = 'true' ;
2551
+ headers [ 'Access-Control-Allow-Origin' ] = req . headers . origin ;
2552
+ } else {
2553
+ headers [ 'Access-Control-Allow-Origin' ] = '*' ;
2554
+ }
2555
+ headers [ 'Access-Control-Allow-Methods' ] = 'GET,HEAD,PUT,PATCH,POST,DELETE' ;
2556
+ headers [ 'Access-Control-Allow-Headers' ] = 'origin, content-type, accept' ;
2557
+ res . writeHead ( 200 , headers ) ;
2558
+ res . end ( ) ;
2559
+ } ;
2560
+ listen ( { handlePreflightRequest} , function ( port ) {
2561
+ request . options ( 'http://localhost:%d/engine.io/default/' . s ( port ) )
2562
+ . set ( 'Origin' , 'http://engine.io' )
2563
+ . query ( { transport : 'polling' } )
2564
+ . end ( function ( res ) {
2565
+ expect ( res . status ) . to . be ( 200 ) ;
2566
+ expect ( res . body ) . to . be . empty ( ) ;
2567
+ expect ( res . header [ 'access-control-allow-credentials' ] ) . to . be ( 'true' ) ;
2568
+ expect ( res . header [ 'access-control-allow-origin' ] ) . to . be ( 'http://engine.io' ) ;
2569
+ expect ( res . header [ 'access-control-allow-methods' ] ) . to . be ( 'GET,HEAD,PUT,PATCH,POST,DELETE' ) ;
2570
+ expect ( res . header [ 'access-control-allow-headers' ] ) . to . be ( 'origin, content-type, accept' ) ;
2571
+ done ( ) ;
2572
+ } ) ;
2573
+ } ) ;
2574
+ } ) ;
2575
+ } ) ;
2576
+
2517
2577
if ( ! UWS_ENGINE && parseInt ( process . versions . node , 10 ) >= 4 ) {
2518
2578
describe ( 'wsEngine option' , function ( ) {
2519
2579
it ( 'should allow loading of other websocket server implementation like uws' , function ( done ) {
0 commit comments