Skip to content

Commit

Permalink
test(browser): Add integration test for httpContextIntegration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Sep 2, 2024
1 parent b4112b0 commit 1eb18cd
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 1eb18cd

Please sign in to comment.