Skip to content

Commit 841c0d3

Browse files
authored
test(tracing): Add getMultipleSentryTransactionRequests (#4375)
1 parent e0eba88 commit 841c0d3

File tree

1 file changed

+63
-16
lines changed

1 file changed

+63
-16
lines changed

packages/integration-tests/utils/helpers.ts

+63-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import { Event } from '@sentry/types';
44
const storeUrlRegex = /\.sentry\.io\/api\/\d+\/store\//;
55
const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\//;
66

7+
const storeRequestParser = (request: Request | null): Event => JSON.parse((request && request.postData()) || '');
8+
const envelopeRequestParser = (request: Request | null): Event => {
9+
// https://develop.sentry.dev/sdk/envelopes/
10+
const envelope = request?.postData() || '';
11+
12+
// Third row of the envelop is the event payload.
13+
return envelope.split('\n').map(line => JSON.parse(line))[2];
14+
};
15+
716
type SentryRequestType = 'event' | 'transaction';
817

918
/**
@@ -31,28 +40,34 @@ async function waitForSentryRequest(page: Page, requestType: SentryRequestType =
3140
* Wait and get Sentry's request sending the event at the given URL, or the current page
3241
*
3342
* @param {Page} page
34-
* @param {string} url
43+
* @param {string} [url]
3544
* @return {*} {Promise<Event>}
3645
*/
3746
async function getSentryRequest(page: Page, url?: string): Promise<Event> {
38-
const request = (await Promise.all([page.goto(url || '#'), waitForSentryRequest(page)]))[1];
47+
const request = (await Promise.all([url ? page.goto(url) : Promise.resolve(null), waitForSentryRequest(page)]))[1];
3948

40-
return JSON.parse((request && request.postData()) || '');
49+
return storeRequestParser(request);
4150
}
4251

52+
/**
53+
* Wait and get Sentry's request sending transaction at the given URL, or the current page
54+
*
55+
* @param {Page} page
56+
* @param {string} [url]
57+
* @return {*} {Promise<Event>}
58+
*/
4359
async function getSentryTransactionRequest(page: Page, url?: string): Promise<Event> {
44-
const request = (await Promise.all([page.goto(url || '#'), waitForSentryRequest(page, 'transaction')]))[1];
60+
const request = (
61+
await Promise.all([url ? page.goto(url) : Promise.resolve(null), waitForSentryRequest(page, 'transaction')])
62+
)[1];
4563

4664
try {
47-
// https://develop.sentry.dev/sdk/envelopes/
48-
const envelope = request?.postData() || '';
49-
50-
// Third row of the envelop is the event payload.
51-
return envelope.split('\n').map(line => JSON.parse(line))[2];
65+
return envelopeRequestParser(request);
5266
} catch (err) {
5367
return Promise.reject(err);
5468
}
5569
}
70+
5671
/**
5772
* Get Sentry events at the given URL, or the current page.
5873
*
@@ -70,24 +85,31 @@ async function getSentryEvents(page: Page, url?: string): Promise<Array<Event>>
7085
}
7186

7287
/**
73-
* Wait and get multiple event requests at the given URL, or the current page
88+
* Wait and get multiple requests matching urlRgx at the given URL, or the current page
7489
*
7590
* @param {Page} page
7691
* @param {number} count
77-
* @param {string} url
78-
* @return {*} {Promise<Event>}
92+
* @param {RegExp} urlRgx
93+
* @param {(req: Request) => Event} requestParser
94+
* @param {string} [url]
95+
* @return {*} {Promise<Event[]>}
7996
*/
80-
async function getMultipleSentryRequests(page: Page, count: number, url?: string): Promise<Event[]> {
97+
async function getMultipleRequests(
98+
page: Page,
99+
count: number,
100+
urlRgx: RegExp,
101+
requestParser: (req: Request) => Event,
102+
url?: string,
103+
): Promise<Event[]> {
81104
const requests: Promise<Event[]> = new Promise((resolve, reject) => {
82105
let reqCount = count;
83106
const requestData: Event[] = [];
84107

85108
page.on('request', request => {
86-
if (storeUrlRegex.test(request.url())) {
109+
if (urlRgx.test(request.url())) {
87110
try {
88111
reqCount -= 1;
89-
requestData.push(JSON.parse((request && request.postData()) || ''));
90-
112+
requestData.push(requestParser(request));
91113
if (reqCount === 0) {
92114
resolve(requestData);
93115
}
@@ -105,6 +127,30 @@ async function getMultipleSentryRequests(page: Page, count: number, url?: string
105127
return requests;
106128
}
107129

130+
/**
131+
* Wait and get multiple event requests at the given URL, or the current page
132+
*
133+
* @param {Page} page
134+
* @param {number} count
135+
* @param {string} [url]
136+
* @return {*} {Promise<Event>}
137+
*/
138+
async function getMultipleSentryRequests(page: Page, count: number, url?: string): Promise<Event[]> {
139+
return getMultipleRequests(page, count, storeUrlRegex, storeRequestParser, url);
140+
}
141+
142+
/**
143+
* Wait and get multiple transaction requests at the given URL, or the current page
144+
*
145+
* @param {Page} page
146+
* @param {number} count
147+
* @param {string} [url]
148+
* @return {*} {Promise<Event>}
149+
*/
150+
async function getMultipleSentryTransactionRequests(page: Page, count: number, url?: string): Promise<Event[]> {
151+
return getMultipleRequests(page, count, envelopeUrlRegex, envelopeRequestParser, url);
152+
}
153+
108154
/**
109155
* Manually inject a script into the page of given URL.
110156
* This function is useful to create more complex test subjects that can't be achieved by pre-built pages.
@@ -126,6 +172,7 @@ export {
126172
runScriptInSandbox,
127173
waitForSentryRequest,
128174
getMultipleSentryRequests,
175+
getMultipleSentryTransactionRequests,
129176
getSentryRequest,
130177
getSentryTransactionRequest,
131178
getSentryEvents,

0 commit comments

Comments
 (0)