Skip to content

Commit

Permalink
test(browser): Add integration test for httpContextIntegration (#13553
Browse files Browse the repository at this point in the history
)

Add a test to our browser integration tests where we explicitly check that httpContextIntegration sets all its supported properties: url, user-agent and (previously untested) referer.
  • Loading branch information
Lms24 committed Sep 3, 2024
1 parent 9b8f9ea commit 128ab44
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

const integrations = Sentry.getDefaultIntegrations({}).filter(
defaultIntegration => defaultIntegration.name === 'HttpContext',
);

const client = new Sentry.BrowserClient({
dsn: 'https://[email protected]/1337',
transport: Sentry.makeFetchTransport,
stackParser: Sentry.defaultStackParser,
integrations: integrations,
});

const scope = new Sentry.Scope();
scope.setClient(client);
client.init();

window._sentryScope = scope;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window._sentryScope.captureException(new Error('client init'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/types';

import { sentryTest } from '../../../utils/fixtures';
import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers';

sentryTest('httpContextIntegration captures user-agent and referrer', async ({ getLocalTestPath, page }) => {
const url = await getLocalTestPath({ testDir: __dirname });

const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page);

// Simulate document.referrer being set to test full functionality of the integration
await page.goto(url, { referer: 'https://sentry.io/' });

const errorEvent = await errorEventPromise;

expect(errorEvent.exception?.values).toHaveLength(1);

expect(errorEvent.request).toEqual({
headers: {
'User-Agent': expect.any(String),
Referer: 'https://sentry.io/',
},
url: expect.any(String),
});
});

0 comments on commit 128ab44

Please sign in to comment.