From 128ab44c58da6796425a491a5e516594789bef0a Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 3 Sep 2024 10:12:46 +0200 Subject: [PATCH] test(browser): Add integration test for `httpContextIntegration` (#13553) 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. --- .../suites/integrations/httpContext/init.js | 20 ++++++++++++++ .../integrations/httpContext/subject.js | 1 + .../suites/integrations/httpContext/test.ts | 26 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 dev-packages/browser-integration-tests/suites/integrations/httpContext/init.js create mode 100644 dev-packages/browser-integration-tests/suites/integrations/httpContext/subject.js create mode 100644 dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts diff --git a/dev-packages/browser-integration-tests/suites/integrations/httpContext/init.js b/dev-packages/browser-integration-tests/suites/integrations/httpContext/init.js new file mode 100644 index 000000000000..984534454ac7 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/integrations/httpContext/init.js @@ -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://public@dsn.ingest.sentry.io/1337', + transport: Sentry.makeFetchTransport, + stackParser: Sentry.defaultStackParser, + integrations: integrations, +}); + +const scope = new Sentry.Scope(); +scope.setClient(client); +client.init(); + +window._sentryScope = scope; diff --git a/dev-packages/browser-integration-tests/suites/integrations/httpContext/subject.js b/dev-packages/browser-integration-tests/suites/integrations/httpContext/subject.js new file mode 100644 index 000000000000..62ce205e2ffc --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/integrations/httpContext/subject.js @@ -0,0 +1 @@ +window._sentryScope.captureException(new Error('client init')); diff --git a/dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts b/dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts new file mode 100644 index 000000000000..02a62142e02b --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts @@ -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(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), + }); +});