Skip to content

Commit 521c69d

Browse files
committed
test(browser): Add tests for getRequestPayloadXhrOrFetch
Signed-off-by: Kaung Zin Hein <[email protected]>
1 parent fea96aa commit 521c69d

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

packages/browser/src/integrations/graphqlClient.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function _updateSpanWithGraphQLData(client: Client, options: GraphQLClientOption
6262

6363
const { endpoints } = options;
6464
const isTracedGraphqlEndpoint = stringMatchesSomePattern(httpUrl, endpoints);
65-
const payload = _getRequestPayloadXhrOrFetch(hint);
65+
const payload = getRequestPayloadXhrOrFetch(hint);
6666

6767
if (isTracedGraphqlEndpoint && payload) {
6868
const graphqlBody = getGraphQLRequestPayload(payload) as GraphQLRequestPayload;
@@ -87,7 +87,7 @@ function _updateBreadcrumbWithGraphQLData(client: Client, options: GraphQLClient
8787
const { endpoints } = options;
8888

8989
const isTracedGraphqlEndpoint = stringMatchesSomePattern(httpUrl, endpoints);
90-
const payload = _getRequestPayloadXhrOrFetch(handlerData);
90+
const payload = getRequestPayloadXhrOrFetch(handlerData);
9191

9292
if (isTracedGraphqlEndpoint && data && payload) {
9393
const graphqlBody = getGraphQLRequestPayload(payload);
@@ -117,18 +117,17 @@ function _getGraphQLOperation(requestBody: GraphQLRequestPayload): string {
117117

118118
/**
119119
* Get the request body/payload based on the shape of the hint
120-
* TODO: export for test?
120+
* Exported for tests only.
121121
*/
122-
function _getRequestPayloadXhrOrFetch(hint: XhrHint | FetchHint): string | undefined {
122+
export function getRequestPayloadXhrOrFetch(hint: XhrHint | FetchHint): string | undefined {
123123
const isXhr = 'xhr' in hint;
124-
const isFetch = !isXhr;
125124

126125
let body: string | undefined;
127126

128127
if (isXhr) {
129128
const sentryXhrData = hint.xhr[SENTRY_XHR_DATA_KEY];
130129
body = sentryXhrData && getBodyString(sentryXhrData.body)[0];
131-
} else if (isFetch) {
130+
} else {
132131
const sentryFetchData = parseFetchPayload(hint.input);
133132
body = getBodyString(sentryFetchData)[0];
134133
}

packages/browser/test/integrations/graphqlClient.test.ts

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
import { getGraphQLRequestPayload, parseFetchPayload, parseGraphQLQuery } from '../../src/integrations/graphqlClient';
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
5+
import { describe, expect, test } from 'vitest';
6+
7+
import { SENTRY_XHR_DATA_KEY } from '@sentry-internal/browser-utils';
8+
import type { FetchHint, XhrHint } from '@sentry-internal/replay';
9+
import {
10+
getGraphQLRequestPayload,
11+
getRequestPayloadXhrOrFetch,
12+
parseFetchPayload,
13+
parseGraphQLQuery,
14+
} from '../../src/integrations/graphqlClient';
215

316
describe('GraphqlClient', () => {
417
describe('parseFetchPayload', () => {
518
const data = [1, 2, 3];
619
const jsonData = '{"data":[1,2,3]}';
720

8-
it.each([
21+
test.each([
922
['string URL only', ['http://example.com'], undefined],
1023
['URL object only', [new URL('http://example.com')], undefined],
1124
['Request URL only', [{ url: 'http://example.com' }], undefined],
@@ -88,4 +101,72 @@ describe('GraphqlClient', () => {
88101
expect(getGraphQLRequestPayload(JSON.stringify(requestBody))).toEqual(requestBody);
89102
});
90103
});
104+
105+
describe('getRequestPayloadXhrOrFetch', () => {
106+
test('should parse xhr payload', () => {
107+
const hint: XhrHint = {
108+
xhr: {
109+
[SENTRY_XHR_DATA_KEY]: {
110+
method: 'POST',
111+
url: 'http://example.com/test',
112+
status_code: 200,
113+
body: JSON.stringify({ key: 'value' }),
114+
request_headers: {
115+
'Content-Type': 'application/json',
116+
},
117+
},
118+
...new XMLHttpRequest(),
119+
},
120+
input: JSON.stringify({ key: 'value' }),
121+
startTimestamp: Date.now(),
122+
endTimestamp: Date.now() + 1000,
123+
};
124+
125+
const result = getRequestPayloadXhrOrFetch(hint);
126+
expect(result).toEqual(JSON.stringify({ key: 'value' }));
127+
});
128+
test('should parse fetch payload', () => {
129+
const hint: FetchHint = {
130+
input: [
131+
'http://example.com/test',
132+
{
133+
method: 'POST',
134+
headers: {
135+
'Content-Type': 'application/json',
136+
},
137+
body: JSON.stringify({ key: 'value' }),
138+
},
139+
],
140+
response: new Response(JSON.stringify({ key: 'value' }), {
141+
status: 200,
142+
headers: {
143+
'Content-Type': 'application/json',
144+
},
145+
}),
146+
startTimestamp: Date.now(),
147+
endTimestamp: Date.now() + 1000,
148+
};
149+
150+
const result = getRequestPayloadXhrOrFetch(hint);
151+
expect(result).toEqual(JSON.stringify({ key: 'value' }));
152+
});
153+
test('should return undefined if no body is in the response', () => {
154+
const hint: FetchHint = {
155+
input: [
156+
'http://example.com/test',
157+
{
158+
method: 'GET',
159+
},
160+
],
161+
response: new Response(null, {
162+
status: 200,
163+
}),
164+
startTimestamp: Date.now(),
165+
endTimestamp: Date.now() + 1000,
166+
};
167+
168+
const result = getRequestPayloadXhrOrFetch(hint);
169+
expect(result).toBeUndefined();
170+
});
171+
});
91172
});

0 commit comments

Comments
 (0)