8000 feat(utils): use `req.cookies` if available instead of parsing by Ignigena · Pull Request #2985 · getsentry/sentry-javascript · GitHub
[go: up one dir, main page]

Skip to content

feat(utils): use req.cookies if available instead of parsing #2985

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

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/utils/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function extractNodeRequestData(
// url (including path and query string):
// node, express: req.originalUrl
// koa: req.url
const originalUrl = (req.originalUrl || req.url) as string;
const originalUrl = (req.originalUrl || req.url || '') as string;
// absolute url
const absoluteUrl = `${protocol}://${host}${originalUrl}`;

Expand All @@ -89,8 +89,9 @@ export function extractNodeRequestData(
case 'cookies':
// cookies:
// node, express, koa: req.headers.cookie
// vercel, sails.js, express (w/ cookie middleware): req.cookies
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
requestData.cookies = dynamicRequire(module, 'cookie').parse(headers.cookie || '');
requestData.cookies = req.cookies || dynamicRequire(module, 'cookie').parse(headers.cookie || '');
break;
case 'query_string':
// query string:
Expand Down
176 changes: 176 additions & 0 deletions packages/utils/test/node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { extractNodeRequestData } from '../src/node';

describe('extractNodeRequestData()', () => {
describe('default behaviour', () => {
test('node', () => {
expect(
extractNodeRequestData({
headers: { host: 'example.com' },
method: 'GET',
secure: true,
originalUrl: '/',
}),
).toEqual({
cookies: {},
headers: {
host: 'example.com',
},
method: 'GET',
query_string: null,
url: 'https://example.com/',
});
});

test('degrades gracefully without request data', () => {
expect(extractNodeRequestData({})).toEqual({
cookies: {},
headers: {},
method: undefined,
query_string: null,
url: 'http://<no host>',
});
});
});

describe('cookies', () => {
it('uses `req.cookies` if available', () => {
expect(
extractNodeRequestData(
{
cookies: { foo: 'bar' },
},
['cookies'],
),
).toEqual({
cookies: { foo: 'bar' },
});
});

it('parses the cookie header', () => {
expect(
extractNodeRequestData(
{
headers: {
cookie: 'foo=bar;',
},
},
['cookies'],
),
).toEqual({
cookies: { foo: 'bar' },
});
});

it('falls back if no cookies are defined', () => {
expect(extractNodeRequestData({}, ['cookies'])).toEqual({
cookies: {},
});
});
});

describe('data', () => {
it('includes data from `req.body` if available', () => {
expect(
extractNodeRequestData(
{
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'foo=bar',
},
['data'],
),
).toEqual({
data: 'foo=bar',
});
});

it('encodes JSON body contents back to a string', () => {
expect(
extractNodeRequestData(
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: { foo: 'bar' },
},
['data'],
),
).toEqual({
data: '{"foo":"bar"}',
});
});
});

describe('query_string', () => {
it('parses the query parms from the url', () => {
expect(
extractNodeRequestData(
{
headers: { host: 'example.com' },
secure: true,
originalUrl: '/?foo=bar',
},
['query_string'],
),
).toEqual({
query_string: 'foo=bar',
});
});

it('gracefully degrades if url cannot be determined', () => {
expect(extractNodeRequestData({}, ['query_string'])).toEqual({
query_string: null,
});
});
});

describe('url', () => {
test('express/koa', () => {
expect(
extractNodeRequestData(
{
host: 'example.com',
protocol: 'https',
url: '/',
},
['url'],
),
).toEqual({
url: 'https://example.com/',
});
});

test('node', () => {
expect(
extractNodeRequestData(
{
headers: { host: 'example.com' },
secure: true,
originalUrl: '/',
},
['url'],
),
).toEqual({
url: 'https://example.com/',
});
});
});

describe('custom key', () => {
it('includes the custom key if present', () => {
expect(
extractNodeRequestData(
{
httpVersion: '1.1',
},
['httpVersion'],
),
).toEqual({
httpVersion: '1.1',
});
});

it('gracefully degrades if the custom key is missing', () => {
expect(extractNodeRequestData({}, ['httpVersion'])).toEqual({});
});
});
});
0