|
| 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('private-network-access defaults to false', (t) => { |
| 12 | + t.plan(3); |
| 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-private-network'], 'undefined'); |
| 30 | + }); |
| 31 | + }); |
| 32 | + t.once('end', () => { |
| 33 | + httpServer.close(); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +test('privateNetworkAccess set to false', (t) => { |
| 38 | + t.plan(3); |
| 39 | + |
| 40 | + const httpServer = http.createServer( |
| 41 | + server({ |
| 42 | + root, |
| 43 | + privateNetworkAccess: false, |
| 44 | + autoIndex: true, |
| 45 | + defaultExt: 'html', |
| 46 | + }) |
| 47 | + ); |
| 48 | + |
| 49 | + httpServer.listen(() => { |
| 50 | + const port = httpServer.address().port; |
| 51 | + const uri = `http://localhost:${port}/subdir/index.html`; |
| 52 | + |
| 53 | + request.get({ uri }, (err, res) => { |
| 54 | + t.ifError(err); |
| 55 | + t.equal(res.statusCode, 200); |
| 56 | + t.type(res.headers['access-control-allow-private-network'], 'undefined'); |
| 57 | + }); |
| 58 | + }); |
| 59 | + t.once('end', () => { |
| 60 | + httpServer.close(); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +test('privateNetworkAccess set to true', (t) => { |
| 65 | + t.plan(3); |
| 66 | + |
| 67 | + const httpServer = http.createServer( |
| 68 | + server({ |
| 69 | + root, |
| 70 | + privateNetworkAccess: true, |
| 71 | + autoIndex: true, |
| 72 | + defaultExt: 'html', |
| 73 | + }) |
| 74 | + ); |
| 75 | + |
| 76 | + httpServer.listen(() => { |
| 77 | + const port = httpServer.address().port; |
| 78 | + const uri = `http://localhost:${port}/subdir/index.html`; |
| 79 | + request.get({ uri }, (err, res) => { |
| 80 | + t.ifError(err); |
| 81 | + t.equal(res.statusCode, 200); |
| 82 | + t.equal(res.headers['access-control-allow-private-network'], 'true'); |
| 83 | + }); |
| 84 | + }); |
| 85 | + t.once('end', () => { |
| 86 | + httpServer.close(); |
| 87 | + }); |
| 88 | +}); |
0 commit comments