Skip to content

Commit

Permalink
always set CORS headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Aug 9, 2024
1 parent 4571da0 commit 4c61e0f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
18 changes: 8 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ export default {
break;
}

if (response.ok) {
response.headers.set(
"Access-Control-Allow-Origin",
corsHeaders["Access-Control-Allow-Origin"],
);
response.headers.set(
"Access-Control-Allow-Methods",
corsHeaders["Access-Control-Allow-Methods"],
);
}
response.headers.set(
"Access-Control-Allow-Origin",
corsHeaders["Access-Control-Allow-Origin"],
);
response.headers.set(
"Access-Control-Allow-Methods",
corsHeaders["Access-Control-Allow-Methods"],
);

return response;
},
Expand Down
5 changes: 4 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { test, expect } from "vitest";
test("unknown URL path", async () => {
const response = await SELF.fetch("https://example.com/unknown", {});
expect(response.status).toBe(404);
expect(Object.fromEntries(response.headers.entries())).toStrictEqual({});
expect(Object.fromEntries(response.headers.entries())).toStrictEqual({
"access-control-allow-origin": "*",
"access-control-allow-methods": "GET,HEAD,POST,OPTIONS",
});
});

test("standard OPTIONS request", async () => {
Expand Down
2 changes: 0 additions & 2 deletions test/select-zls-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,6 @@ describe("/v1/zls/select-version", () => {
error: "method must be 'GET'",
});
expect(response.status).toBe(405);
expect(response.headers.has("Access-Control-Allow-Origin")).toBe(false);
expect(response.headers.has("Access-Control-Allow-Methods")).toBe(false);
});

test.each<unknown>([null, "", {}, []])(
Expand Down

0 comments on commit 4c61e0f

Please sign in to comment.