Skip to content

Commit 75a9025

Browse files
committed
e2e: add tests for cors
1 parent b4e7ae4 commit 75a9025

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

e2e/src/express.entrypoint.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ function createRouter() {
1818

1919
export async function startExpressServer() {
2020
const {app, server, address} = await bootstrap({
21+
cors: {
22+
credentials: true,
23+
allowedHeaders: ["Authorization", "Content-Type"],
24+
methods: ["GET", "OPTIONS"],
25+
origin: "http://example.com",
26+
},
2127
router: createRouter(),
2228
})
2329

e2e/src/index.axios.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ describe.each(startServerFunctions)(
2828
server?.close()
2929
})
3030

31+
describe("CORS", () => {
32+
it("should send the correct CORS headers", async () => {
33+
const {headers} = await client.getResponsesEmpty(undefined, {
34+
headers: {
35+
Origin: "http://e2e.example.com",
36+
},
37+
})
38+
expect(headers).toMatchObject({
39+
"access-control-allow-origin": "http://example.com",
40+
"access-control-allow-credentials": "true",
41+
})
42+
})
43+
it("should support pre-flight requests", async () => {
44+
const {headers} = await client.getResponsesEmpty(undefined, {
45+
method: "OPTIONS",
46+
headers: {
47+
Origin: "http://e2e.example.com",
48+
"Access-Control-Request-Method": "POST",
49+
},
50+
})
51+
expect(headers).toMatchObject({
52+
"access-control-allow-origin": "http://example.com",
53+
"access-control-allow-credentials": "true",
54+
"access-control-allow-methods": "GET,OPTIONS",
55+
"access-control-allow-headers": "Authorization,Content-Type",
56+
})
57+
})
58+
})
59+
3160
describe("GET /headers/undeclared", () => {
3261
it("provides the default headers", async () => {
3362
const {data} = await client.getHeadersUndeclared()

e2e/src/index.fetch.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@ describe.each(startServerFunctions)(
2727
server?.close()
2828
})
2929

30+
describe("CORS", () => {
31+
it("should send the correct CORS headers", async () => {
32+
const {headers} = await client.getResponsesEmpty(undefined, {
33+
headers: {
34+
Origin: "http://e2e.example.com",
35+
},
36+
})
37+
expect(Object.fromEntries(headers)).toMatchObject({
38+
"access-control-allow-origin": "http://example.com",
39+
"access-control-allow-credentials": "true",
40+
})
41+
})
42+
it("should support pre-flight requests", async () => {
43+
const {headers} = await client.getResponsesEmpty(undefined, {
44+
method: "OPTIONS",
45+
headers: {
46+
Origin: "http://e2e.example.com",
47+
"Access-Control-Request-Method": "POST",
48+
},
49+
})
50+
expect(Object.fromEntries(headers)).toMatchObject({
51+
"access-control-allow-origin": "http://example.com",
52+
"access-control-allow-credentials": "true",
53+
"access-control-allow-methods": "GET,OPTIONS",
54+
"access-control-allow-headers": "Authorization,Content-Type",
55+
})
56+
})
57+
})
58+
3059
describe("GET /headers/undeclared", () => {
3160
it("provides the default headers", async () => {
3261
const res = await client.getHeadersUndeclared()

e2e/src/koa.entrypoint.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ function createRouter() {
4242

4343
export async function startKoaServer() {
4444
return await bootstrap({
45+
cors: {
46+
credentials: true,
47+
allowHeaders: ["Authorization", "Content-Type"],
48+
allowMethods: ["GET", "OPTIONS"],
49+
origin: "http://example.com",
50+
},
4551
router: createRouter(),
4652
})
4753
}

0 commit comments

Comments
 (0)