Skip to content

Commit a7f6321

Browse files
committed
tests: added browser integration tests for the hook
1 parent ea046d1 commit a7f6321

File tree

3 files changed

+90
-0
lines changed
  • dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/on-request-span-end

3 files changed

+90
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
integrations: [
8+
Sentry.browserTracingIntegration({
9+
idleTimeout: 1000,
10+
onRequestSpanEnd(span, { headers }) {
11+
if (headers) {
12+
span.setAttribute('hook.called.response-type', headers.get('x-response-type'));
13+
}
14+
},
15+
}),
16+
],
17+
tracesSampleRate: 1,
18+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fetch('http://sentry-test.io/fetch', {
2+
headers: {
3+
foo: 'fetch',
4+
},
5+
});
6+
7+
const xhr = new XMLHttpRequest();
8+
9+
xhr.open('GET', 'http://sentry-test.io/xhr');
10+
xhr.setRequestHeader('foo', 'xhr');
11+
xhr.send();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/core';
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getMultipleSentryEnvelopeRequests, shouldSkipTracingTest } from '../../../../utils/helpers';
5+
6+
sentryTest('should call onRequestSpanEnd hook', async ({ browserName, getLocalTestUrl, page }) => {
7+
const supportedBrowsers = ['chromium', 'firefox'];
8+
9+
if (shouldSkipTracingTest() || !supportedBrowsers.includes(browserName)) {
10+
sentryTest.skip();
11+
}
12+
13+
await page.route('http://sentry-test.io/fetch', async route => {
14+
await route.fulfill({
15+
status: 200,
16+
headers: {
17+
'Content-Type': 'application/json',
18+
'X-Response-Type': 'fetch',
19+
'access-control-expose-headers': '*',
20+
},
21+
body: '',
22+
});
23+
});
24+
await page.route('http://sentry-test.io/xhr', async route => {
25+
await route.fulfill({
26+
status: 200,
27+
headers: {
28+
'Content-Type': 'application/json',
29+
'X-Response-Type': 'xhr',
30+
'access-control-expose-headers': '*',
31+
},
32+
body: '',
33+
});
34+
});
35+
36+
const url = await getLocalTestUrl({ testDir: __dirname });
37+
38+
const envelopes = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url, timeout: 10000 });
39+
40+
const tracingEvent = envelopes[envelopes.length - 1]; // last envelope contains tracing data on all browsers
41+
42+
expect(tracingEvent.spans).toContainEqual(
43+
expect.objectContaining({
44+
op: 'http.client',
45+
data: expect.objectContaining({
46+
type: 'xhr',
47+
'hook.called.response-type': 'xhr',
48+
}),
49+
}),
50+
);
51+
52+
expect(tracingEvent.spans).toContainEqual(
53+
expect.objectContaining({
54+
op: 'http.client',
55+
data: expect.objectContaining({
56+
type: 'fetch',
57+
'hook.called.response-type': 'fetch',
58+
}),
59+
}),
60+
);
61+
});

0 commit comments

Comments
 (0)