Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { createServerEntry, getRequestUrl } from '../src/request-response'
3β
4describe('server entry pathname validation', () => {
5 it.each([
6 '/malformed/%E0%A4',
7 '/malformed/%80',
8 '/malformed/%FF',
9 '/malformed/%',
10 '/malformed/%2',
11 '/malformed/%GG',
12 ])('rejects %s before calling the entry', async (pathname) => {
13 const fetch = vi.fn(() => new Response('unused'))
14 const entry = createServerEntry({ fetch })
15β
16 const response = await entry.fetch(
17 new Request(`http://localhost${pathname}`),
18 )
19β
20 expect(response.status).toBe(400)
21 expect(response.statusText).toBe('Bad Request')
22 expect(response.body).toBeNull()
23 expect(fetch).not.toHaveBeenCalled()
24 })
25β
26 it.each([
27 '/plain',
28 '/%2Fdocs',
29 '/%25',
30 '/%5Cdocs',
31 '/%E2%9C%93',
32 '/%252Fdocs',
33 '/%2Fdocs?searchParam=%E0%A4',
34 '/plain?searchParam=%GG',
35 ])('preserves the pathname and search of %s', async (path) => {
36 const fetch = vi.fn(() => {
37 const url = getRequestUrl()
38 return new Response(url.pathname + url.search)
39 })
40 const entry = createServerEntry({ fetch })
41β
42 const response = await entry.fetch(new Request(`http://localhost${path}`))
43β
44 expect(response.status).toBe(200)
45 await expect(response.text()).resolves.toBe(path)
46 expect(fetch).toHaveBeenCalledTimes(1)
47 })
48})