Skip to content

Commit

Permalink
log user agent (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored Mar 27, 2024
1 parent b63f670 commit 813ccf0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const mail = async ({ env = {}, ...rest }) => {
attachments: !!rest.attachments,
contentType: rest.type,
status: response.status,
userAgent: rest.userAgent,
},
});
if (response.status > 299) {
Expand Down
7 changes: 6 additions & 1 deletion src/mail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ describe('mail', () => {
});
it('logs', async () => {
global.fetch.mockResolvedValue({ json: () => '{}', status: 200 });
await mail({ ...input, to: '[email protected],[email protected]' });
await mail({
...input,
to: '[email protected],[email protected]',
userAgent: 'custom-user-agent 1.0',
});
expect(global.fetch).toHaveBeenCalledWith(
'https://api.logsnag.com/v1/log',
expect.objectContaining({
Expand All @@ -61,6 +65,7 @@ describe('mail', () => {
attachments: true,
contentType: 'text/html',
status: 200,
userAgent: 'custom-user-agent 1.0',
},
}),
}),
Expand Down
2 changes: 2 additions & 0 deletions src/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const params = async (request) => {
// pass
}

data.userAgent = request.headers.get('user-agent');

data.errors = required.reduce(
(acc, el) =>
data?.[el] ? acc : [...acc, `missing required parameter '${el}'`],
Expand Down
2 changes: 2 additions & 0 deletions src/params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ describe.each(['application/json', 'application/x-www-form-urlencoded'])(
method: 'POST',
headers: {
'Content-Type': contentType,
'User-Agent': 'custom-user-agent 1.0',
},
body: (format[contentType] ?? ((d) => d))(body),
});

expect(await params(request)).toEqual({
errors: [],
...body,
userAgent: 'custom-user-agent 1.0',
});
});

Expand Down

0 comments on commit 813ccf0

Please sign in to comment.