|
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'; |
2 | 15 |
|
3 | 16 | describe('GraphqlClient', () => { |
4 | 17 | describe('parseFetchPayload', () => { |
5 | 18 | const data = [1, 2, 3]; |
6 | 19 | const jsonData = '{"data":[1,2,3]}'; |
7 | 20 |
|
8 | | - it.each([ |
| 21 | + test.each([ |
9 | 22 | ['string URL only', ['http://example.com'], undefined], |
10 | 23 | ['URL object only', [new URL('http://example.com')], undefined], |
11 | 24 | ['Request URL only', [{ url: 'http://example.com' }], undefined], |
@@ -88,4 +101,72 @@ describe('GraphqlClient', () => { |
88 | 101 | expect(getGraphQLRequestPayload(JSON.stringify(requestBody))).toEqual(requestBody); |
89 | 102 | }); |
90 | 103 | }); |
| 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 | + }); |
91 | 172 | }); |
0 commit comments