Skip to content

Commit d78cdf9

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

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

test/server.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,67 @@ describe('server', function () {
25142514
});
25152515
});
25162516

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

0 commit comments

Comments
 (0)