Skip to content

Commit 563706f

Browse files
author
Emilien Kenler
committed
Add tests for the handlePreflightRequest option
1 parent 6890481 commit 563706f

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

test/server.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,6 @@ describe('server', function () {
14591459
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] });
14601460
socket.on('open', function () {
14611461
for (var i = 0; i < messageCount; i++) {
1462-
// connection.send('message: ' + i); // works
14631462
connection.send(messagePayload + '|message: ' + i); // does not work
14641463
}
14651464
var receivedCount = 0;
@@ -2514,6 +2513,67 @@ describe('server', function () {
25142513
});
25152514
});
25162515

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+
25172577
if (!UWS_ENGINE && parseInt(process.versions.node, 10) >= 4) {
25182578
describe('wsEngine option', function () {
25192579
it('should allow loading of other websocket server implementation like uws', function (done) {

0 commit comments

Comments
 (0)