|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { sentryRemixMiddleware } from '../../src/v3/server/middleware'; |
| 4 | +import type { MatcherLike, RequestContextLike } from '../../src/v3/types'; |
| 5 | + |
| 6 | +/** A matcher that resolves every URL to the one pattern, or to nothing. */ |
| 7 | +function matcherFor(pattern: string | undefined): MatcherLike { |
| 8 | + return { |
| 9 | + add: () => {}, |
| 10 | + matchAll: () => (pattern ? [{ data: { pattern: { source: pattern }, method: 'GET' }, params: {} }] : []), |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +function contextFor(url: string, headers: Record<string, string> = {}): RequestContextLike { |
| 15 | + return { request: new Request(url, { headers }), url: new URL(url), method: 'GET', params: {} }; |
| 16 | +} |
| 17 | + |
| 18 | +describe('sentryRemixMiddleware', () => { |
| 19 | + it('reports the route on HTML responses through Server-Timing', async () => { |
| 20 | + const middleware = sentryRemixMiddleware(matcherFor('/users/:id')); |
| 21 | + |
| 22 | + const response = await middleware( |
| 23 | + contextFor('http://x/users/12345', { accept: 'text/html' }), |
| 24 | + async () => new Response('<html></html>', { headers: { 'content-type': 'text/html' } }), |
| 25 | + ); |
| 26 | + |
| 27 | + expect(response.headers.get('server-timing')).toBe('sentry-route;desc="/users/:id"'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('quotes a pattern that contains header-breaking characters', async () => { |
| 31 | + const middleware = sentryRemixMiddleware(matcherFor('/files/:name("a\\b")')); |
| 32 | + |
| 33 | + const response = await middleware( |
| 34 | + contextFor('http://x/files/x', { accept: 'text/html' }), |
| 35 | + async () => new Response(''), |
| 36 | + ); |
| 37 | + |
| 38 | + expect(response.headers.get('server-timing')).toBe('sentry-route;desc="/files/:name(\\"a\\\\b\\")"'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('keeps an existing Server-Timing header', async () => { |
| 42 | + const middleware = sentryRemixMiddleware(matcherFor('/')); |
| 43 | + |
| 44 | + const response = await middleware( |
| 45 | + contextFor('http://x/', { accept: 'text/html' }), |
| 46 | + async () => new Response('', { headers: { 'server-timing': 'db;dur=5' } }), |
| 47 | + ); |
| 48 | + |
| 49 | + expect(response.headers.get('server-timing')).toBe('db;dur=5, sentry-route;desc="/"'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('adds nothing to a response the browser did not ask for as a document', async () => { |
| 53 | + const middleware = sentryRemixMiddleware(matcherFor('/api/items/:itemId')); |
| 54 | + |
| 55 | + const response = await middleware( |
| 56 | + contextFor('http://x/api/items/1', { accept: 'application/json' }), |
| 57 | + async () => new Response('{}'), |
| 58 | + ); |
| 59 | + |
| 60 | + expect(response.headers.has('server-timing')).toBe(false); |
| 61 | + }); |
| 62 | + |
| 63 | + it('adds nothing when no route matched', async () => { |
| 64 | + const middleware = sentryRemixMiddleware(matcherFor(undefined)); |
| 65 | + |
| 66 | + const response = await middleware( |
| 67 | + contextFor('http://x/nope', { accept: 'text/html' }), |
| 68 | + async () => new Response('Not Found', { status: 404 }), |
| 69 | + ); |
| 70 | + |
| 71 | + expect(response.headers.has('server-timing')).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('leaves a response with immutable headers alone, without failing the request', async () => { |
| 75 | + const middleware = sentryRemixMiddleware(matcherFor('/')); |
| 76 | + |
| 77 | + // `Response.error()` is the one constructor that yields immutable headers, like a passed through |
| 78 | + // `fetch()` response does. |
| 79 | + const response = await middleware(contextFor('http://x/', { accept: 'text/html' }), async () => Response.error()); |
| 80 | + |
| 81 | + expect(response.headers.has('server-timing')).toBe(false); |
| 82 | + }); |
| 83 | +}); |
0 commit comments