|
| 1 | +// Copied from browser integration tests |
| 2 | +import { Page, Request } from '@playwright/test'; |
| 3 | +import { Event, EventEnvelopeHeaders } from '@sentry/types'; |
| 4 | + |
| 5 | +const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\//; |
| 6 | + |
| 7 | +const envelopeRequestParser = (request: Request | null): Event => { |
| 8 | + // https://develop.sentry.dev/sdk/envelopes/ |
| 9 | + const envelope = request?.postData() || ''; |
| 10 | + |
| 11 | + // Third row of the envelop is the event payload. |
| 12 | + return envelope.split('\n').map(line => JSON.parse(line))[2]; |
| 13 | +}; |
| 14 | + |
| 15 | +export const envelopeHeaderRequestParser = (request: Request | null): EventEnvelopeHeaders => { |
| 16 | + // https://develop.sentry.dev/sdk/envelopes/ |
| 17 | + const envelope = request?.postData() || ''; |
| 18 | + |
| 19 | + // First row of the envelop is the event payload. |
| 20 | + return envelope.split('\n').map(line => JSON.parse(line))[0]; |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Get Sentry events at the given URL, or the current page. |
| 25 | + * |
| 26 | + * @param {Page} page |
| 27 | + * @param {string} [url] |
| 28 | + * @return {*} {Promise<Array<Event>>} |
| 29 | + */ |
| 30 | +async function getSentryEvents(page: Page, url?: string): Promise<Array<Event>> { |
| 31 | + if (url) { |
| 32 | + await page.goto(url); |
| 33 | + } |
| 34 | + const eventsHandle = await page.evaluateHandle<Array<Event>>('window.events'); |
| 35 | + |
| 36 | + return eventsHandle.jsonValue(); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Waits until a number of requests matching urlRgx at the given URL arrive. |
| 41 | + * If the timout option is configured, this function will abort waiting, even if it hasn't reveived the configured |
| 42 | + * amount of requests, and returns all the events recieved up to that point in time. |
| 43 | + */ |
| 44 | +async function getMultipleRequests( |
| 45 | + page: Page, |
| 46 | + count: number, |
| 47 | + urlRgx: RegExp, |
| 48 | + requestParser: (req: Request) => Event, |
| 49 | + options?: { |
| 50 | + url?: string; |
| 51 | + timeout?: number; |
| 52 | + }, |
| 53 | +): Promise<Event[]> { |
| 54 | + const requests: Promise<Event[]> = new Promise((resolve, reject) => { |
| 55 | + let reqCount = count; |
| 56 | + const requestData: Event[] = []; |
| 57 | + let timeoutId: NodeJS.Timeout | undefined = undefined; |
| 58 | + |
| 59 | + function requestHandler(request: Request): void { |
| 60 | + if (urlRgx.test(request.url())) { |
| 61 | + try { |
| 62 | + reqCount -= 1; |
| 63 | + requestData.push(requestParser(request)); |
| 64 | + |
| 65 | + if (reqCount === 0) { |
| 66 | + if (timeoutId) { |
| 67 | + clearTimeout(timeoutId); |
| 68 | + } |
| 69 | + page.off('request', requestHandler); |
| 70 | + resolve(requestData); |
| 71 | + } |
| 72 | + } catch (err) { |
| 73 | + reject(err); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + page.on('request', requestHandler); |
| 79 | + |
| 80 | + if (options?.timeout) { |
| 81 | + timeoutId = setTimeout(() => { |
| 82 | + resolve(requestData); |
| 83 | + }, options.timeout); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + if (options?.url) { |
| 88 | + await page.goto(options.url); |
| 89 | + } |
| 90 | + |
| 91 | + return requests; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Wait and get multiple envelope requests at the given URL, or the current page |
| 96 | + */ |
| 97 | +async function getMultipleSentryEnvelopeRequests<T>( |
| 98 | + page: Page, |
| 99 | + count: number, |
| 100 | + options?: { |
| 101 | + url?: string; |
| 102 | + timeout?: number; |
| 103 | + }, |
| 104 | + requestParser: (req: Request) => T = envelopeRequestParser as (req: Request) => T, |
| 105 | +): Promise<T[]> { |
| 106 | + // TODO: This is not currently checking the type of envelope, just casting for now. |
| 107 | + // We can update this to include optional type-guarding when we have types for Envelope. |
| 108 | + return getMultipleRequests(page, count, envelopeUrlRegex, requestParser, options) as Promise<T[]>; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Wait and get the first envelope request at the given URL, or the current page |
| 113 | + * |
| 114 | + * @template T |
| 115 | + * @param {Page} page |
| 116 | + * @param {string} [url] |
| 117 | + * @return {*} {Promise<T>} |
| 118 | + */ |
| 119 | +async function getFirstSentryEnvelopeRequest<T>( |
| 120 | + page: Page, |
| 121 | + url?: string, |
| 122 | + requestParser: (req: Request) => T = envelopeRequestParser as (req: Request) => T, |
| 123 | +): Promise<T> { |
| 124 | + return (await getMultipleSentryEnvelopeRequests<T>(page, 1, { url }, requestParser))[0]; |
| 125 | +} |
| 126 | + |
| 127 | +export { getMultipleSentryEnvelopeRequests, getFirstSentryEnvelopeRequest, getSentryEvents }; |
0 commit comments