Skip to content

Commit 1fd6481

Browse files
committed
fix streaming tests + cleanup
1 parent 4b39078 commit 1fd6481

File tree

1 file changed

+49
-125
lines changed

1 file changed

+49
-125
lines changed

packages/react-router/test/server/wrapSentryHandleRequest.test.ts

Lines changed: 49 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -176,58 +176,6 @@ describe('wrapSentryHandleRequest', () => {
176176
});
177177
});
178178

179-
test('should call original handler with same parameters', async () => {
180-
const originalHandler = vi.fn().mockResolvedValue('original response');
181-
const wrappedHandler = wrapSentryHandleRequest(originalHandler);
182-
183-
const request = new Request('https://taco.burrito');
184-
const responseStatusCode = 200;
185-
const responseHeaders = new Headers();
186-
const routerContext = { staticHandlerContext: { matches: [] } } as any;
187-
const loadContext = {} as any;
188-
189-
const result = await wrappedHandler(request, responseStatusCode, responseHeaders, routerContext, loadContext);
190-
191-
expect(originalHandler).toHaveBeenCalledWith(
192-
request,
193-
responseStatusCode,
194-
responseHeaders,
195-
routerContext,
196-
loadContext,
197-
);
198-
expect(result).toBe('original response');
199-
});
200-
201-
test('should set span attributes when parameterized path exists and active span exists', async () => {
202-
const originalHandler = vi.fn().mockResolvedValue('test');
203-
const wrappedHandler = wrapSentryHandleRequest(originalHandler);
204-
205-
const mockActiveSpan = {};
206-
const mockRootSpan = { setAttributes: vi.fn() };
207-
const mockRpcMetadata = { type: RPCType.HTTP, route: '/some-path' };
208-
209-
(getActiveSpan as unknown as ReturnType<typeof vi.fn>).mockReturnValue(mockActiveSpan);
210-
(getRootSpan as unknown as ReturnType<typeof vi.fn>).mockReturnValue(mockRootSpan);
211-
const getRPCMetadata = vi.fn().mockReturnValue(mockRpcMetadata);
212-
(vi.importActual('@opentelemetry/core') as unknown as { getRPCMetadata: typeof getRPCMetadata }).getRPCMetadata =
213-
getRPCMetadata;
214-
215-
const routerContext = {
216-
staticHandlerContext: {
217-
matches: [{ route: { path: 'some-path' } }],
218-
},
219-
} as any;
220-
221-
await wrappedHandler(new Request('https://nacho.queso'), 200, new Headers(), routerContext, {} as any);
222-
223-
expect(mockRootSpan.setAttributes).toHaveBeenCalledWith({
224-
[ATTR_HTTP_ROUTE]: '/some-path',
225-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
226-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router.request-handler',
227-
});
228-
expect(mockRpcMetadata.route).toBe('/some-path');
229-
});
230-
231179
test('should not set span attributes when parameterized path does not exist', async () => {
232180
const mockActiveSpan = {};
233181
const mockRootSpan = { setAttributes: vi.fn() };
@@ -249,29 +197,6 @@ test('should not set span attributes when parameterized path does not exist', as
249197
expect(mockRootSpan.setAttributes).not.toHaveBeenCalled();
250198
});
251199

252-
test('should not set span attributes when active span does not exist', async () => {
253-
const originalHandler = vi.fn().mockResolvedValue('test');
254-
const wrappedHandler = wrapSentryHandleRequest(originalHandler);
255-
256-
const mockRpcMetadata = { type: RPCType.HTTP, route: '/some-path' };
257-
258-
(getActiveSpan as unknown as ReturnType<typeof vi.fn>).mockReturnValue(null);
259-
260-
const getRPCMetadata = vi.fn().mockReturnValue(mockRpcMetadata);
261-
(vi.importActual('@opentelemetry/core') as unknown as { getRPCMetadata: typeof getRPCMetadata }).getRPCMetadata =
262-
getRPCMetadata;
263-
264-
const routerContext = {
265-
staticHandlerContext: {
266-
matches: [{ route: { path: 'some-path' } }],
267-
},
268-
} as any;
269-
270-
await wrappedHandler(new Request('https://tio.pepe'), 200, new Headers(), routerContext, {} as any);
271-
272-
expect(getRPCMetadata).not.toHaveBeenCalled();
273-
});
274-
275200
describe('getMetaTagTransformer', () => {
276201
beforeEach(() => {
277202
vi.clearAllMocks();
@@ -281,64 +206,63 @@ describe('getMetaTagTransformer', () => {
281206
});
282207

283208
test('should inject meta tags before closing head tag', () => {
284-
const outputStream = new PassThrough();
285-
const bodyStream = new PassThrough();
286-
const transformer = getMetaTagTransformer(bodyStream);
287-
288-
let outputData = '';
289-
outputStream.on('data', chunk => {
290-
outputData += chunk.toString();
291-
});
292-
293-
outputStream.on('end', () => {
294-
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
295-
expect(outputData).not.toContain('</head></head>');
209+
return new Promise<void>(resolve => {
210+
const bodyStream = new PassThrough();
211+
const transformer = getMetaTagTransformer(bodyStream);
212+
213+
let outputData = '';
214+
bodyStream.on('data', chunk => {
215+
outputData += chunk.toString();
216+
});
217+
218+
bodyStream.on('end', () => {
219+
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
220+
expect(outputData).not.toContain('</head></head>');
221+
resolve();
222+
});
223+
224+
transformer.write('<html><head></head><body>Test</body></html>');
225+
transformer.end();
296226
});
297-
298-
transformer.pipe(outputStream);
299-
300-
bodyStream.write('<html><head></head><body>Test</body></html>');
301-
bodyStream.end();
302227
});
303228

304229
test('should not modify chunks without head closing tag', () => {
305-
const outputStream = new PassThrough();
306-
const bodyStream = new PassThrough();
307-
const transformer = getMetaTagTransformer(bodyStream);
308-
309-
let outputData = '';
310-
outputStream.on('data', chunk => {
311-
outputData += chunk.toString();
230+
return new Promise<void>(resolve => {
231+
const bodyStream = new PassThrough();
232+
const transformer = getMetaTagTransformer(bodyStream);
233+
234+
let outputData = '';
235+
bodyStream.on('data', chunk => {
236+
outputData += chunk.toString();
237+
});
238+
239+
bodyStream.on('end', () => {
240+
expect(outputData).toBe('<html><body>Test</body></html>');
241+
resolve();
242+
});
243+
244+
transformer.write('<html><body>Test</body></html>');
245+
transformer.end();
312246
});
313-
314-
outputStream.on('end', () => {
315-
expect(outputData).toBe('<html><body>Test</body></html>');
316-
expect(getTraceMetaTags).toHaveBeenCalled();
317-
});
318-
319-
transformer.pipe(outputStream);
320-
321-
bodyStream.write('<html><body>Test</body></html>');
322-
bodyStream.end();
323247
});
324248

325249
test('should handle buffer input', () => {
326-
const outputStream = new PassThrough();
327-
const bodyStream = new PassThrough();
328-
const transformer = getMetaTagTransformer(bodyStream);
329-
330-
let outputData = '';
331-
outputStream.on('data', chunk => {
332-
outputData += chunk.toString();
333-
});
334-
335-
outputStream.on('end', () => {
336-
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
250+
return new Promise<void>(resolve => {
251+
const bodyStream = new PassThrough();
252+
const transformer = getMetaTagTransformer(bodyStream);
253+
254+
let outputData = '';
255+
bodyStream.on('data', chunk => {
256+
outputData += chunk.toString();
257+
});
258+
259+
bodyStream.on('end', () => {
260+
expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>');
261+
resolve();
262+
});
263+
264+
transformer.write(Buffer.from('<html><head></head><body>Test</body></html>'));
265+
transformer.end();
337266
});
338-
339-
transformer.pipe(outputStream);
340-
341-
bodyStream.write(Buffer.from('<html><head></head><body>Test</body></html>'));
342-
bodyStream.end();
343267
});
344268
});

0 commit comments

Comments
 (0)