-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use cors module instead of custom cors logic (#2823)
* use cors module instead of custom cors logic #2762 - adds support for regex COMPANION_CLIENT_ORIGINS_REGEX - encapsulate custom cors header merge logic in own middleware - pull out non cors logic from middleware - unit test the cors middleware * fix capitalization Co-authored-by: Julian Gruber <julian@juliangruber.com> Co-authored-by: Julian Gruber <julian@juliangruber.com>
- Loading branch information
1 parent
6c4f79b
commit 415681f
Showing
9 changed files
with
150 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* global jest:false, test:false, describe:false, expect:false */ | ||
|
||
const { cors } = require('../../src/server/middlewares') | ||
|
||
function testWithMock ({ corsOptions, get = () => {}, origin = 'https://localhost:1234' } = {}) { | ||
const res = { | ||
get, | ||
getHeader: get, | ||
setHeader: jest.fn(), | ||
end: jest.fn(), | ||
} | ||
const req = { | ||
method: 'OPTIONS', | ||
headers: { | ||
origin, | ||
}, | ||
} | ||
const next = jest.fn() | ||
cors(corsOptions)(req, res, next) | ||
return { res } | ||
} | ||
|
||
describe('cors', () => { | ||
test('should properly merge with existing headers', () => { | ||
const get = (header) => { | ||
if (header.toLowerCase() === 'access-control-allow-methods') return 'PATCH,OPTIONS, post' | ||
if (header.toLowerCase() === 'access-control-allow-headers') return 'test-allow-header' | ||
if (header.toLowerCase() === 'access-control-expose-headers') return 'test' | ||
return undefined | ||
} | ||
|
||
const { res } = testWithMock({ | ||
corsOptions: { | ||
sendSelfEndpoint: true, | ||
corsOrigins: /^https:\/\/localhost:.*$/, | ||
}, | ||
get, | ||
}) | ||
expect(res.setHeader.mock.calls).toEqual([ | ||
['Access-Control-Allow-Origin', 'https://localhost:1234'], | ||
['Vary', 'Origin'], | ||
['Access-Control-Allow-Credentials', 'true'], | ||
['Access-Control-Allow-Methods', 'PATCH,OPTIONS,POST,GET,DELETE'], | ||
['Access-Control-Allow-Headers', 'uppy-auth-token,uppy-versions,uppy-credentials-params,test-allow-header'], | ||
['Access-Control-Expose-Headers', 'Access-Control-Allow-Headers,i-am,test'], | ||
['Content-Length', '0'], | ||
]) | ||
// expect(next).toHaveBeenCalled() | ||
}) | ||
|
||
test('should also work when nothing added', () => { | ||
const { res } = testWithMock() | ||
expect(res.setHeader.mock.calls).toEqual([ | ||
['Access-Control-Allow-Origin', 'https://localhost:1234'], | ||
['Vary', 'Origin'], | ||
['Access-Control-Allow-Credentials', 'true'], | ||
['Access-Control-Allow-Methods', 'GET,POST,OPTIONS,DELETE'], | ||
['Access-Control-Allow-Headers', 'uppy-auth-token,uppy-versions,uppy-credentials-params'], | ||
['Access-Control-Expose-Headers', 'Access-Control-Allow-Headers'], | ||
['Content-Length', '0'], | ||
]) | ||
}) | ||
|
||
test('should support disabling cors', () => { | ||
const { res } = testWithMock({ corsOptions: { corsOrigins: false } }) | ||
expect(res.setHeader.mock.calls).toEqual([]) | ||
}) | ||
|
||
test('should support incorrect url', () => { | ||
const { res } = testWithMock({ corsOptions: { corsOrigins: /^incorrect$/ } }) | ||
expect(res.setHeader.mock.calls).toEqual([ | ||
['Vary', 'Origin'], | ||
['Access-Control-Allow-Credentials', 'true'], | ||
['Access-Control-Allow-Methods', 'GET,POST,OPTIONS,DELETE'], | ||
['Access-Control-Allow-Headers', 'uppy-auth-token,uppy-versions,uppy-credentials-params'], | ||
['Access-Control-Expose-Headers', 'Access-Control-Allow-Headers'], | ||
['Content-Length', '0'], | ||
]) | ||
}) | ||
|
||
test('should support array origin', () => { | ||
const { res } = testWithMock({ corsOptions: { corsOrigins: ['http://google.com', 'https://localhost:1234'] } }) | ||
expect(res.setHeader.mock.calls).toEqual([ | ||
['Access-Control-Allow-Origin', 'https://localhost:1234'], | ||
['Vary', 'Origin'], | ||
['Access-Control-Allow-Credentials', 'true'], | ||
['Access-Control-Allow-Methods', 'GET,POST,OPTIONS,DELETE'], | ||
['Access-Control-Allow-Headers', 'uppy-auth-token,uppy-versions,uppy-credentials-params'], | ||
['Access-Control-Expose-Headers', 'Access-Control-Allow-Headers'], | ||
['Content-Length', '0'], | ||
]) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters