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