Skip to content

test(tracing): Add getMultipleSentryTransactionRequests helper #4375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 63 additions & 16 deletions packages/integration-tests/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { Event } from '@sentry/types';
const storeUrlRegex = /\.sentry\.io\/api\/\d+\/store\//;
const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\//;

const storeRequestParser = (request: Request | null): Event => JSON.parse((request && request.postData()) || '');
const envelopeRequestParser = (request: Request | null): Event => {
// https://develop.sentry.dev/sdk/envelopes/
const envelope = request?.postData() || '';

// Third row of the envelop is the event payload.
return envelope.split('\n').map(line => JSON.parse(line))[2];
};

type SentryRequestType = 'event' | 'transaction';

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

return JSON.parse((request && request.postData()) || '');
return storeRequestParser(request);
}

/**
* Wait and get Sentry's request sending transaction at the given URL, or the current page
*
* @param {Page} page
* @param {string} [url]
* @return {*} {Promise<Event>}
*/
async function getSentryTransactionRequest(page: Page, url?: string): Promise<Event> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this just be getMultipleSentryTransactionRequests with count of 1?

const request = (await Promise.all([page.goto(url || '#'), waitForSentryRequest(page, 'transaction')]))[1];
const request = (
await Promise.all([url ? page.goto(url) : Promise.resolve(null), waitForSentryRequest(page, 'transaction')])
)[1];

try {
// https://develop.sentry.dev/sdk/envelopes/
const envelope = request?.postData() || '';

// Third row of the envelop is the event payload.
return envelope.split('\n').map(line => JSON.parse(line))[2];
return envelopeRequestParser(request);
} catch (err) {
return Promise.reject(err);
}
}

/**
* Get Sentry events at the given URL, or the current page.
*
Expand All @@ -70,24 +85,31 @@ async function getSentryEvents(page: Page, url?: string): Promise<Array<Event>>
}

/**
* Wait and get multiple event requests at the given URL, or the current page
* Wait and get multiple requests matching urlRgx at the given URL, or the current page
*
* @param {Page} page
* @param {number} count
* @param {string} url
* @return {*} {Promise<Event>}
* @param {RegExp} urlRgx
* @param {(req: Request) => Event} requestParser
* @param {string} [url]
* @return {*} {Promise<Event[]>}
*/
async function getMultipleSentryRequests(page: Page, count: number, url?: string): Promise<Event[]> {
async function getMultipleRequests(
page: Page,
count: number,
urlRgx: RegExp,
requestParser: (req: Request) => Event,
url?: string,
): Promise<Event[]> {
const requests: Promise<Event[]> = new Promise((resolve, reject) => {
let reqCount = count;
const requestData: Event[] = [];

page.on('request', request => {
if (storeUrlRegex.test(request.url())) {
if (urlRgx.test(request.url())) {
try {
reqCount -= 1;
requestData.push(JSON.parse((request && request.postData()) || ''));

requestData.push(requestParser(request));
if (reqCount === 0) {
resolve(requestData);
}
Expand All @@ -105,6 +127,30 @@ async function getMultipleSentryRequests(page: Page, count: number, url?: string
return requests;
}

/**
* Wait and get multiple event requests at the given URL, or the current page
*
* @param {Page} page
* @param {number} count
* @param {string} [url]
* @return {*} {Promise<Event>}
*/
async function getMultipleSentryRequests(page: Page, count: number, url?: string): Promise<Event[]> {
return getMultipleRequests(page, count, storeUrlRegex, storeRequestParser, url);
}

/**
* Wait and get multiple transaction requests at the given URL, or the current page
*
* @param {Page} page
* @param {number} count
* @param {string} [url]
* @return {*} {Promise<Event>}
*/
async function getMultipleSentryTransactionRequests(page: Page, count: number, url?: string): Promise<Event[]> {
return getMultipleRequests(page, count, envelopeUrlRegex, envelopeRequestParser, url);
}

/**
* Manually inject a script into the page of given URL.
* This function is useful to create more complex test subjects that can't be achieved by pre-built pages.
Expand All @@ -126,6 +172,7 @@ export {
runScriptInSandbox,
waitForSentryRequest,
getMultipleSentryRequests,
getMultipleSentryTransactionRequests,
getSentryRequest,
getSentryTransactionRequest,
getSentryEvents,
Expand Down