Skip to content

Commit afb3700

Browse files
authored
feat(integrations): Add HTTPClient integration (#6500)
1 parent 0def7bc commit afb3700

File tree

10 files changed

+630
-1
lines changed

10 files changed

+630
-1
lines changed

packages/browser/src/integrations/httpcontext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class HttpContext implements Integration {
3636
...(referrer && { Referer: referrer }),
3737
...(userAgent && { 'User-Agent': userAgent }),
3838
};
39-
const request = { ...(url && { url }), headers };
39+
const request = { ...event.request, ...(url && { url }), headers };
4040

4141
return { ...event, request };
4242
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fetch('http://localhost:7654/foo', {
2+
method: 'GET',
3+
credentials: 'include',
4+
headers: {
5+
Accept: 'application/json',
6+
'Content-Type': 'application/json',
7+
Cache: 'no-cache',
8+
},
9+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { expect } from '@playwright/test';
2+
import { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers';
6+
7+
sentryTest(
8+
'should assign request and response context from a failed 500 fetch request',
9+
async ({ getLocalTestPath, page }) => {
10+
// Skipping this test when running in bundle mode, because `@sentry/integrations` bundle
11+
// is not injected to the page with the current test setup.
12+
if (process.env.PW_BUNDLE?.includes('bundle')) {
13+
sentryTest.skip();
14+
}
15+
16+
const url = await getLocalTestPath({ testDir: __dirname });
17+
18+
await page.route('**/foo', route => {
19+
return route.fulfill({
20+
status: 500,
21+
body: JSON.stringify({
22+
error: {
23+
message: 'Internal Server Error',
24+
},
25+
}),
26+
headers: {
27+
'Content-Type': 'text/html',
28+
},
29+
});
30+
});
31+
32+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
33+
34+
expect(eventData.exception?.values).toHaveLength(1);
35+
36+
// Not able to get the cookies from the request/response because of Playwright bug
37+
// https://github.com/microsoft/playwright/issues/11035
38+
expect(eventData).toMatchObject({
39+
message: 'HTTP Client Error with status code: 500',
40+
exception: {
41+
values: [
42+
{
43+
type: 'Error',
44+
value: 'HTTP Client Error with status code: 500',
45+
mechanism: {
46+
type: 'http.client',
47+
handled: true,
48+
},
49+
},
50+
],
51+
},
52+
request: {
53+
url: 'http://localhost:7654/foo',
54+
method: 'GET',
55+
headers: {
56+
accept: 'application/json',
57+
cache: 'no-cache',
58+
'content-type': 'application/json',
59+
},
60+
},
61+
contexts: {
62+
response: {
63+
status_code: 500,
64+
body_size: 45,
65+
headers: {
66+
'content-type': 'text/html',
67+
'content-length': '45',
68+
},
69+
},
70+
},
71+
});
72+
},
73+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Sentry from '@sentry/browser';
2+
import { HttpClient } from '@sentry/integrations';
3+
4+
window.Sentry = Sentry;
5+
6+
Sentry.init({
7+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
8+
integrations: [new HttpClient()],
9+
tracesSampleRate: 1,
10+
sendDefaultPii: true,
11+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const xhr = new XMLHttpRequest();
2+
3+
xhr.open('GET', 'http://localhost:7654/foo', true);
4+
xhr.withCredentials = true;
5+
xhr.setRequestHeader('Accept', 'application/json');
6+
xhr.setRequestHeader('Content-Type', 'application/json');
7+
xhr.setRequestHeader('Cache', 'no-cache');
8+
xhr.send();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { expect } from '@playwright/test';
2+
import { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers';
6+
7+
sentryTest(
8+
'should assign request and response context from a failed 500 XHR request',
9+
async ({ getLocalTestPath, page }) => {
10+
// Skipping this test when running in bundle mode, because `@sentry/integrations` bundle
11+
// is not injected to the page with the current test setup.
12+
if (process.env.PW_BUNDLE?.includes('bundle')) {
13+
sentryTest.skip();
14+
}
15+
16+
const url = await getLocalTestPath({ testDir: __dirname });
17+
18+
await page.route('**/foo', route => {
19+
return route.fulfill({
20+
status: 500,
21+
body: JSON.stringify({
22+
error: {
23+
message: 'Internal Server Error',
24+
},
25+
}),
26+
headers: {
27+
'Content-Type': 'text/html',
28+
},
29+
});
30+
});
31+
32+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
33+
34+
expect(eventData.exception?.values).toHaveLength(1);
35+
36+
// Not able to get the cookies from the request/response because of Playwright bug
37+
// https://github.com/microsoft/playwright/issues/11035
38+
expect(eventData).toMatchObject({
39+
message: 'HTTP Client Error with status code: 500',
40+
exception: {
41+
values: [
42+
{
43+
type: 'Error',
44+
value: 'HTTP Client Error with status code: 500',
45+
mechanism: {
46+
type: 'http.client',
47+
handled: true,
48+
},
49+
},
50+
],
51+
},
52+
request: {
53+
url: 'http://localhost:7654/foo',
54+
method: 'GET',
55+
headers: {
56+
Accept: 'application/json',
57+
Cache: 'no-cache',
58+
'Content-Type': 'application/json',
59+
},
60+
},
61+
contexts: {
62+
response: {
63+
status_code: 500,
64+
body_size: 45,
65+
headers: {
66+
'content-type': 'text/html',
67+
'content-length': '45',
68+
},
69+
},
70+
},
71+
});
72+
},
73+
);

0 commit comments

Comments
 (0)