@@ -124,121 +124,61 @@ describe("socket.io", () => {
124
124
describe ( "handshake" , ( ) => {
125
125
const request = require ( "superagent" ) ;
126
126
127
- it ( "should disallow request when origin defined and none specified" , done => {
128
- const sockets = new Server ( { origins : "http://foo.example:*" } ) . listen (
129
- 54013
130
- ) ;
131
- request
132
- . get ( "http://localhost:54013/socket.io/default/" )
133
- . query ( { transport : "polling" } )
134
- . end ( ( err , res ) => {
135
- expect ( res . status ) . to . be ( 403 ) ;
136
- done ( ) ;
137
- } ) ;
138
- } ) ;
139
-
140
- it ( "should disallow request when origin defined and a different one specified" , done => {
141
- const sockets = new Server ( { origins : "http://foo.example:*" } ) . listen (
142
- 54014
143
- ) ;
144
- request
145
- . get ( "http://localhost:54014/socket.io/default/" )
146
- . query ( { transport : "polling" } )
147
- . set ( "origin" , "http://herp.derp" )
148
- . end ( ( err , res ) => {
149
- expect ( res . status ) . to . be ( 403 ) ;
150
- done ( ) ;
151
- } ) ;
152
- } ) ;
153
-
154
- it ( "should allow request when origin defined an the same is specified" , done => {
155
- const sockets = new Server ( { origins : "http://foo.example:*" } ) . listen (
156
- 54015
157
- ) ;
158
- request
159
- . get ( "http://localhost:54015/socket.io/default/" )
160
- . set ( "origin" , "http://foo.example" )
161
- . query ( { transport : "polling" } )
162
- . end ( ( err , res ) => {
163
- expect ( res . status ) . to . be ( 200 ) ;
164
- done ( ) ;
165
- } ) ;
166
- } ) ;
167
-
168
- it ( "should allow request when origin defined as function and same is supplied" , done => {
169
- const sockets = new Server ( {
170
- origins : ( origin , callback ) => {
171
- if ( origin == "http://foo.example" ) {
172
- return callback ( null , true ) ;
173
- }
174
- return callback ( null , false ) ;
127
+ it ( "should send the Access-Control-Allow-xxx headers on OPTIONS request" , done => {
128
+ const sockets = new Server ( 54013 , {
129
+ cors : {
130
+ origin : "http://localhost:54023" ,
131
+ methods : [ "GET" , "POST" ] ,
132
+ allowedHeaders : [ "content-type" ] ,
133
+ credentials : true
175
134
}
176
- } ) . listen ( 54016 ) ;
135
+ } ) ;
177
136
request
178
- . get ( "http://localhost:54016/socket.io/default/" )
179
- . set ( "origin" , "http://foo.example" )
137
+ . options ( "http://localhost:54013/socket.io/default/" )
180
138
. query ( { transport : "polling" } )
139
+ . set ( "Origin" , "http://localhost:54023" )
181
140
. end ( ( err , res ) => {
182
- expect ( res . status ) . to . be ( 200 ) ;
183
- done ( ) ;
184
- } ) ;
185
- } ) ;
141
+ expect ( res . status ) . to . be ( 204 ) ;
186
142
187
- it ( "should allow request when origin defined as function and different is supplied" , done => {
188
- const sockets = new Server ( {
189
- origins : ( origin , callback ) => {
190
- if ( origin == "http://foo.example" ) {
191
- return callback ( null , true ) ;
192
- }
193
- return callback ( null , false ) ;
194
- }
195
- } ) . listen ( 54017 ) ;
196
- request
197
- . get ( "http://localhost:54017/socket.io/default/" )
198
- . set ( "origin" , "http://herp.derp" )
199
- . query ( { transport : "polling" } )
200
- . end ( ( err , res ) => {
201
- expect ( res . status ) . to . be ( 403 ) ;
143
+ expect ( res . headers [ "access-control-allow-origin" ] ) . to . be (
144
+ "http://localhost:54023"
145
+ ) ;
146
+ expect ( res . headers [ "access-control-allow-methods" ] ) . to . be ( "GET,POST" ) ;
147
+ expect ( res . headers [ "access-control-allow-headers" ] ) . to . be (
148
+ "content-type"
149
+ ) ;
150
+ expect ( res . headers [ "access-control-allow-credentials" ] ) . to . be ( "true" ) ;
202
151
done ( ) ;
203
152
} ) ;
204
153
} ) ;
205
154
206
- it ( "should allow request when origin defined as function and no origin is supplied " , done => {
207
- const sockets = new Server ( {
208
- origins : ( origin , callback ) => {
209
- if ( origin == "*" ) {
210
- return callback ( null , true ) ;
211
- }
212
- return callback ( null , false ) ;
155
+ it ( "should send the Access-Control-Allow-xxx headers on GET request " , done => {
156
+ const sockets = new Server ( 54014 , {
157
+ cors : {
158
+ origin : "http://localhost:54024" ,
159
+ methods : [ "GET" , "POST" ] ,
160
+ allowedHeaders : [ "content-type" ] ,
161
+ credentials : true
213
162
}
214
- } ) . listen ( 54021 ) ;
163
+ } ) ;
215
164
request
216
- . get ( "http://localhost:54021 /socket.io/default/" )
165
+ . get ( "http://localhost:54014 /socket.io/default/" )
217
166
. query ( { transport : "polling" } )
167
+ . set ( "Origin" , "http://localhost:54024" )
218
168
. end ( ( err , res ) => {
219
169
expect ( res . status ) . to . be ( 200 ) ;
220
- done ( ) ;
221
- } ) ;
222
- } ) ;
223
170
224
- it ( "should default to port 443 when protocol is https" , done => {
225
- const sockets = new Server ( { origins : "https://foo.example:443" } ) . listen (
226
- 54036
227
- ) ;
228
- request
229
- . get ( "http://localhost:54036/socket.io/default/" )
230
- . set ( "origin" , "https://foo.example" )
231
- . query ( { transport : "polling" } )
232
- . end ( ( err , res ) => {
233
- expect ( res . status ) . to . be ( 200 ) ;
171
+ expect ( res . headers [ "access-control-allow-origin" ] ) . to . be (
172
+ "http://localhost:54024"
173
+ ) ;
174
+ expect ( res . headers [ "access-control-allow-credentials" ] ) . to . be ( "true" ) ;
234
175
done ( ) ;
235
176
} ) ;
236
177
} ) ;
237
178
238
179
it ( "should allow request if custom function in opts.allowRequest returns true" , done => {
239
180
const sockets = new Server ( createServer ( ) . listen ( 54022 ) , {
240
- allowRequest : ( req , callback ) => callback ( null , true ) ,
241
- origins : "http://foo.example:*"
181
+ allowRequest : ( req , callback ) => callback ( null , true )
242
182
} ) ;
243
183
244
184
request
@@ -263,18 +203,6 @@ describe("socket.io", () => {
263
203
done ( ) ;
264
204
} ) ;
265
205
} ) ;
266
-
267
- it ( "should allow request when using an array of origins" , done => {
268
- new Server ( { origins : [ "http://foo.example:54024" ] } ) . listen ( 54024 ) ;
269
- request
270
- . get ( "http://localhost:54024/socket.io/default/" )
271
- . set ( "origin" , "http://foo.example:54024" )
272
- . query ( { transport : "polling" } )
273
- . end ( ( err , res ) => {
274
- expect ( res . status ) . to . be ( 200 ) ;
275
- done ( ) ;
276
- } ) ;
277
- } ) ;
278
206
} ) ;
279
207
280
208
describe ( "close" , ( ) => {
0 commit comments