-
Notifications
You must be signed in to change notification settings - Fork 5
/
configuring-cors.test.ts
48 lines (42 loc) · 1.15 KB
/
configuring-cors.test.ts
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
import {
assertEquals,
assertMatch,
} from "https://deno.land/std/testing/asserts.ts";
import { exampleServerAPI } from "./example-server-api.ts";
const REQUEST_ORIGIN_WHITELIST = [
"http://localhost:3000",
"http://localhost:1234",
];
Deno.test(
`Set 'access-control-allow-origin' response header to 'null' on getBooks when request origin IS NOT ${
REQUEST_ORIGIN_WHITELIST.join(
" or ",
)
}`,
async () => {
const res = await exampleServerAPI.getBooksResponse();
assertEquals(res.headers.get("access-control-allow-origin"), "null");
},
);
Deno.test(
`Set 'access-control-allow-origin' response header to request origin on getBooks when request origin IS ${
REQUEST_ORIGIN_WHITELIST.join(
" or ",
)
}`,
async () => {
const responses = await Promise.all(
REQUEST_ORIGIN_WHITELIST.map((origin) =>
exampleServerAPI.getBooksResponse({
headers: { origin },
})
),
);
responses.forEach((res) => {
assertMatch(
res.headers.get("access-control-allow-origin") as string,
new RegExp(REQUEST_ORIGIN_WHITELIST.join("|"), "gi"),
);
});
},
);