|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('tap').test; |
| 4 | +const server = require('../lib/core'); |
| 5 | +const http = require('http'); |
| 6 | +const path = require('path'); |
| 7 | +const request = require('request'); |
| 8 | + |
| 9 | +const root = path.join(__dirname, 'public'); |
| 10 | + |
| 11 | +test('cors defaults to false', (t) => { |
| 12 | + t.plan(4); |
| 13 | + |
| 14 | + const httpServer = http.createServer( |
| 15 | + server({ |
| 16 | + root, |
| 17 | + autoIndex: true, |
| 18 | + defaultExt: 'html', |
| 19 | + }) |
| 20 | + ); |
| 21 | + |
| 22 | + httpServer.listen(() => { |
| 23 | + const port = httpServer.address().port; |
| 24 | + const uri = `http://localhost:${port}/subdir/index.html`; |
| 25 | + |
| 26 | + request.get({ uri }, (err, res) => { |
| 27 | + t.ifError(err); |
| 28 | + t.equal(res.statusCode, 200); |
| 29 | + t.type(res.headers['access-control-allow-origin'], 'undefined'); |
| 30 | + t.type(res.headers['access-control-allow-headers'], 'undefined'); |
| 31 | + }); |
| 32 | + }); |
| 33 | + t.once('end', () => { |
| 34 | + httpServer.close(); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +test('cors set to false', (t) => { |
| 39 | + t.plan(4); |
| 40 | + |
| 41 | + const httpServer = http.createServer( |
| 42 | + server({ |
| 43 | + root, |
| 44 | + cors: false, |
| 45 | + autoIndex: true, |
| 46 | + defaultExt: 'html', |
| 47 | + }) |
| 48 | + ); |
| 49 | + |
| 50 | + httpServer.listen(() => { |
| 51 | + const port = httpServer.address().port; |
| 52 | + const uri = `http://localhost:${port}/subdir/index.html`; |
| 53 | + |
| 54 | + request.get({ uri }, (err, res) => { |
| 55 | + t.ifError(err); |
| 56 | + t.equal(res.statusCode, 200); |
| 57 | + t.type(res.headers['access-control-allow-origin'], 'undefined'); |
| 58 | + t.type(res.headers['access-control-allow-headers'], 'undefined'); |
| 59 | + }); |
| 60 | + }); |
| 61 | + t.once('end', () => { |
| 62 | + httpServer.close(); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +test('cors set to true', (t) => { |
| 67 | + t.plan(4); |
| 68 | + |
| 69 | + const httpServer = http.createServer( |
| 70 | + server({ |
| 71 | + root, |
| 72 | + cors: true, |
| 73 | + autoIndex: true, |
| 74 | + defaultExt: 'html', |
| 75 | + }) |
| 76 | + ); |
| 77 | + |
| 78 | + httpServer.listen(() => { |
| 79 | + const port = httpServer.address().port; |
| 80 | + const uri = `http://localhost:${port}/subdir/index.html`; |
| 81 | + request.get({ uri }, (err, res) => { |
| 82 | + t.ifError(err); |
| 83 | + t.equal(res.statusCode, 200); |
| 84 | + t.equal(res.headers['access-control-allow-origin'], '*'); |
| 85 | + t.equal(res.headers['access-control-allow-headers'], 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + t.once('end', () => { |
| 89 | + httpServer.close(); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +test('CORS set to true', (t) => { |
| 94 | + t.plan(4); |
| 95 | + |
| 96 | + const httpServer = http.createServer( |
| 97 | + server({ |
| 98 | + root, |
| 99 | + CORS: true, |
| 100 | + autoIndex: true, |
| 101 | + defaultExt: 'html', |
| 102 | + }) |
| 103 | + ); |
| 104 | + |
| 105 | + httpServer.listen(() => { |
| 106 | + const port = httpServer.address().port; |
| 107 | + const uri = `http://localhost:${port}/subdir/index.html`; |
| 108 | + request.get({ uri }, (err, res) => { |
| 109 | + t.ifError(err); |
| 110 | + t.equal(res.statusCode, 200); |
| 111 | + t.equal(res.headers['access-control-allow-origin'], '*'); |
| 112 | + t.equal(res.headers['access-control-allow-headers'], 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since'); |
| 113 | + }); |
| 114 | + }); |
| 115 | + t.once('end', () => { |
| 116 | + httpServer.close(); |
| 117 | + }); |
| 118 | +}); |
0 commit comments