-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(v8/node): Ensure express requests are properly handled (#14851)
- Loading branch information
Showing
4 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
dev-packages/node-integration-tests/suites/express/requestUser/server.js
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,49 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
transport: loggingTransport, | ||
debug: true, | ||
}); | ||
|
||
// express must be required after Sentry is initialized | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); | ||
|
||
const app = express(); | ||
|
||
app.use(cors()); | ||
|
||
app.use((req, _res, next) => { | ||
// We simulate this, which would in other cases be done by some middleware | ||
req.user = { | ||
id: '1', | ||
email: '[email protected]', | ||
}; | ||
|
||
next(); | ||
}); | ||
|
||
app.get('/test1', (_req, _res) => { | ||
throw new Error('error_1'); | ||
}); | ||
|
||
app.use((_req, _res, next) => { | ||
Sentry.setUser({ | ||
id: '2', | ||
email: '[email protected]', | ||
}); | ||
|
||
next(); | ||
}); | ||
|
||
app.get('/test2', (_req, _res) => { | ||
throw new Error('error_2'); | ||
}); | ||
|
||
Sentry.setupExpressErrorHandler(app); | ||
|
||
startExpressServerAndSendPortToRunner(app); |
49 changes: 49 additions & 0 deletions
49
dev-packages/node-integration-tests/suites/express/requestUser/test.ts
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,49 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
describe('express user handling', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('picks user from request', done => { | ||
createRunner(__dirname, 'server.js') | ||
.expect({ | ||
event: { | ||
user: { | ||
id: '1', | ||
email: '[email protected]', | ||
}, | ||
exception: { | ||
values: [ | ||
{ | ||
value: 'error_1', | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start(done) | ||
.makeRequest('get', '/test1', { expectError: true }); | ||
}); | ||
|
||
test('setUser overwrites user from request', done => { | ||
createRunner(__dirname, 'server.js') | ||
.expect({ | ||
event: { | ||
user: { | ||
id: '2', | ||
email: '[email protected]', | ||
}, | ||
exception: { | ||
values: [ | ||
{ | ||
value: 'error_2', | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start(done) | ||
.makeRequest('get', '/test2', { expectError: true }); | ||
}); | ||
}); |
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