Skip to content

Commit 29720b5

Browse files
authored
feat: allow CORS requests (#121)
1 parent 714302d commit 29720b5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/localServerApi.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const createLocalServerApi = testkit => {
1818

1919
const { project } = parseDsn(userDsn)
2020
const app = express()
21+
app.use((req, res, next) => {
22+
res.header('Access-Control-Allow-Origin', '*')
23+
next()
24+
})
2125
// the performance endpoint uses a custom non-json payload so
2226
// we can't use bodyParser.json() directly
2327
app.use(

test/local-server.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ describe('sentry test-kit test suite - local server', function() {
8383
})
8484
expect(response.ok).toBe(true)
8585
})
86+
87+
test('responds with Access-Control-Allow-Origin header', async function() {
88+
const dsn = localServer.getDsn().replace(`/${PROJECT_ID}`, '')
89+
const sessionEnvelopeBody =
90+
`{"sent_at":"2021-08-17T14:27:12.489Z","sdk":{"name":"sentry.javascript.react","version":"6.11.0"}}\n` +
91+
`{"type":"session"}\n` +
92+
`{"sid":"<removed>","init":false,"started":"2021-08-17T14:27:11.361Z","timestamp":"2021-08-17T14:27:12.489Z","status":"ok","errors":1,"attrs":{"release":"<removed>","environment":"<removed>","user_agent":"<removed>"}}`
93+
94+
const response = await fetch(`${dsn}/api/${PROJECT_ID}/envelope/`, {
95+
method: 'POST',
96+
body: sessionEnvelopeBody,
97+
headers: { 'Content-Type': 'text/plain' },
98+
})
99+
expect(response.ok).toBe(true)
100+
expect(response.headers.get('access-control-allow-origin')).toBe('*')
101+
})
86102
})
87103

88104
describe('local server testkit error cases', () => {

0 commit comments

Comments
 (0)