|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { createRequestEvent } from './request-event'; |
| 3 | +import { RedirectMessage } from './redirect-handler'; |
| 4 | +import type { ServerRequestEvent, QwikSerializer } from './types'; |
| 5 | + |
| 6 | +const mockQwikSerializer: QwikSerializer = { |
| 7 | + _deserializeData: vi.fn(), |
| 8 | + _serializeData: vi.fn(), |
| 9 | + _verifySerializable: vi.fn(), |
| 10 | +}; |
| 11 | + |
| 12 | +function createMockServerRequestEvent(url = 'http://localhost:3000/test'): ServerRequestEvent { |
| 13 | + const mockRequest = new Request(url); |
| 14 | + |
| 15 | + return { |
| 16 | + mode: 'server', |
| 17 | + url: new URL(url), |
| 18 | + locale: undefined, |
| 19 | + platform: {}, |
| 20 | + request: mockRequest, |
| 21 | + env: { |
| 22 | + get: vi.fn(), |
| 23 | + }, |
| 24 | + getClientConn: vi.fn(() => ({ ip: '127.0.0.1' })), |
| 25 | + getWritableStream: vi.fn(() => { |
| 26 | + const writer = { |
| 27 | + write: vi.fn(), |
| 28 | + close: vi.fn(), |
| 29 | + }; |
| 30 | + return { |
| 31 | + getWriter: () => writer, |
| 32 | + locked: false, |
| 33 | + pipeTo: vi.fn(), |
| 34 | + } as any; |
| 35 | + }), |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function createMockRequestEvent(url = 'http://localhost:3000/test') { |
| 40 | + const serverRequestEv = createMockServerRequestEvent(url); |
| 41 | + return createRequestEvent(serverRequestEv, null, [], true, '/', mockQwikSerializer, vi.fn()); |
| 42 | +} |
| 43 | + |
| 44 | +describe('request-event redirect', () => { |
| 45 | + it('should not cache redirects by default', () => { |
| 46 | + const requestEv = createMockRequestEvent(); |
| 47 | + |
| 48 | + requestEv.headers.set('Cache-Control', 'max-age=3600, public'); |
| 49 | + |
| 50 | + const result = requestEv.redirect(301, '/new-location'); |
| 51 | + |
| 52 | + expect(result).toBeInstanceOf(RedirectMessage); |
| 53 | + expect(requestEv.headers.get('Location')).toBe('/new-location'); |
| 54 | + expect(requestEv.headers.get('Cache-Control')).toBeNull(); |
| 55 | + expect(requestEv.status()).toBe(301); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should set Cache-Control to no-store for redirects with status > 301', () => { |
| 59 | + const requestEv = createMockRequestEvent(); |
| 60 | + |
| 61 | + const result = requestEv.redirect(307, '/new-location'); |
| 62 | + |
| 63 | + expect(result).toBeInstanceOf(RedirectMessage); |
| 64 | + expect(requestEv.headers.get('Location')).toBe('/new-location'); |
| 65 | + expect(requestEv.headers.get('Cache-Control')).toBe('no-store'); |
| 66 | + expect(requestEv.status()).toBe(307); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should fix invalid redirect URLs with multiple slashes', () => { |
| 70 | + const requestEv = createMockRequestEvent(); |
| 71 | + |
| 72 | + const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 73 | + |
| 74 | + const result = requestEv.redirect(302, '/path//with///multiple////slashes'); |
| 75 | + |
| 76 | + expect(result).toBeInstanceOf(RedirectMessage); |
| 77 | + expect(requestEv.headers.get('Location')).toBe('/path/with/multiple/slashes'); |
| 78 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 79 | + 'Redirect URL /path//with///multiple////slashes is invalid, fixing to /path/with/multiple/slashes' |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should throw error when trying to redirect after headers are sent', () => { |
| 84 | + const requestEv = createMockRequestEvent(); |
| 85 | + |
| 86 | + // Trigger getWritableStream to simulate headers being sent |
| 87 | + requestEv.getWritableStream(); |
| 88 | + |
| 89 | + expect(() => { |
| 90 | + requestEv.redirect(302, '/should-fail'); |
| 91 | + }).toThrow('Response already sent'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments