Skip to content

Commit

Permalink
🐛 Add cors allowed headers header
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Oct 9, 2020
1 parent f11694f commit 2f183b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/core/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ async function getReply(routes, request) {

// cors preflight
if (request.method === 'OPTIONS') {
reply = [204, { 'Access-Control-Allow-Methods': 'GET,POST' }];
reply = [204, {}];
reply[1]['Access-Control-Allow-Methods'] = 'GET,POST,OPTIONS';
reply[1]['Access-Control-Request-Headers'] = 'Vary';
let allowed = request.headers['access-control-request-headers'];
if (allowed?.length) reply[1]['Access-Control-Allow-Headers'] = allowed;
} else {
reply = await Promise.resolve()
.then(() => routes.middleware?.(request))
Expand Down
15 changes: 13 additions & 2 deletions packages/core/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,28 @@ describe('Snapshot Server', () => {

it('accepts preflight cors checks', async () => {
let called = false;
let response;

await percy.start();
percy.snapshot = async () => (called = true);

let response = await fetch('http://localhost:1337/percy/snapshot', {
response = await fetch('http://localhost:1337/percy/snapshot', {
method: 'OPTIONS'
});

expect(response.status).toBe(204);
expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*');
expect(response.headers.get('Access-Control-Allow-Methods')).toBe('GET,POST');
expect(response.headers.get('Access-Control-Allow-Methods')).toBe('GET,POST,OPTIONS');
expect(response.headers.get('Access-Control-Request-Headers')).toBe('Vary');
expect(called).toBe(false);

response = await fetch('http://localhost:1337/percy/snapshot', {
headers: { 'Access-Control-Request-Headers': 'Content-Type' },
method: 'OPTIONS'
});

expect(response.status).toBe(204);
expect(response.headers.get('Access-Control-Allow-Headers')).toBe('Content-Type');
expect(called).toBe(false);
});

Expand Down

0 comments on commit 2f183b3

Please sign in to comment.