-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add cookies as an optional property in the request handler #2167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ThatTobMate
wants to merge
3
commits into
getsentry:master
from
ThatTobMate:tobias/make-cookies-optional-in-req
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,73 @@ | ||
| import { parseRequest } from '../src/handlers'; | ||
| import { Event } from '../src'; | ||
|
|
||
| describe('parseRequest', () => { | ||
| const mockReq = { | ||
| method: 'GET', | ||
| url: '/some/path?key=value', | ||
| headers: { | ||
| host: 'mattrobenolt.com', | ||
| }, | ||
| cookies: { test: 'test' }, | ||
| body: '', | ||
| user: { | ||
| id: 123, | ||
| username: 'tobias', | ||
| email: '[email protected]', | ||
| custom_property: 'foo', | ||
| }, | ||
| }; | ||
|
|
||
| describe('parseRequest.user properties', () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought i'd add a bit of test coverage for some of this logic for both the |
||
| const DEFAULT_USER_KEYS = ['id', 'username', 'email']; | ||
| const CUSTOM_USER_KEYS = ['custom_property']; | ||
|
|
||
| test('parseRequest.user only contains the default properties from the user', done => { | ||
| const fakeEvent: Event = {}; | ||
| const parsedRequest: Event = parseRequest(fakeEvent, mockReq); | ||
| const userKeys = Object.keys(parsedRequest.user); | ||
|
|
||
| expect(userKeys).toEqual(DEFAULT_USER_KEYS); | ||
| expect(userKeys).not.toEqual(expect.arrayContaining(CUSTOM_USER_KEYS)); | ||
| done(); | ||
| }); | ||
|
|
||
| test('parseRequest.user only contains the custom properties specified in the options.user array', done => { | ||
| const options = { | ||
| user: CUSTOM_USER_KEYS, | ||
| }; | ||
| const fakeEvent: Event = {}; | ||
| const parsedRequest: Event = parseRequest(fakeEvent, mockReq, options); | ||
| const userKeys = Object.keys(parsedRequest.user); | ||
|
|
||
| expect(userKeys).toEqual(CUSTOM_USER_KEYS); | ||
| expect(userKeys).not.toEqual(expect.arrayContaining(DEFAULT_USER_KEYS)); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('parseRequest.request properties', () => { | ||
| test('parseRequest.request only contains the default set of properties from the request', done => { | ||
| const DEFAULT_REQUEST_PROPERTIES = ['cookies', 'data', 'headers', 'method', 'query_string', 'url']; | ||
| const fakeEvent: Event = {}; | ||
| const parsedRequest: Event = parseRequest(fakeEvent, mockReq, {}); | ||
| expect(Object.keys(parsedRequest.request)).toEqual(DEFAULT_REQUEST_PROPERTIES); | ||
| done(); | ||
| }); | ||
|
|
||
| test('parseRequest.request only contains the specified properties in the options.request array', done => { | ||
| const EXCLUDED_PROPERTIES = ['cookies', 'method']; | ||
| const INCLUDED_PROPERTIES = ['data', 'headers', 'query_string', 'url']; | ||
| const options = { | ||
| request: INCLUDED_PROPERTIES, | ||
| }; | ||
| const fakeEvent: Event = {}; | ||
| const parsedRequest: Event = parseRequest(fakeEvent, mockReq, options); | ||
| const requestKeys = Object.keys(parsedRequest.request); | ||
|
|
||
| expect(requestKeys).toEqual(INCLUDED_PROPERTIES); | ||
| expect(requestKeys).not.toEqual(expect.arrayContaining(EXCLUDED_PROPERTIES)); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using slightly different logic here than the
extractUserDatafunction, as you are already running some custom logic to set/manipulate keys on the request interface.In this PR we build the request interface as before then remove any of the properties not specified in the optional array (if it exists).
This behaviour is slightly different to the
extractUserDatafn as it allows users to request any of the properties from the request interface rather than any properties of thereqargument itself.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's a bit too ambiguous we could do something like below which would allow users to extract any property on the
reqobject: