|
| 1 | +import { describe, it, expect, beforeEach } from 'vitest' |
| 2 | +import { createStartHandler } from '../src' |
| 3 | +import { currentHandlers } from './mocks/router-entry' |
| 4 | + |
| 5 | + |
| 6 | +const spaFallback = async () => |
| 7 | + new Response('<!doctype html><div>spa</div>', { |
| 8 | + status: 200, |
| 9 | + headers: { 'Content-Type': 'text/html' }, |
| 10 | + }) |
| 11 | + |
| 12 | +function makeApp() { |
| 13 | + return createStartHandler(async () => await spaFallback()) |
| 14 | +} |
| 15 | +beforeEach(() => { |
| 16 | + Object.keys(currentHandlers).forEach(key => delete currentHandlers[key]) |
| 17 | +}) |
| 18 | + |
| 19 | +describe('createStartHandler — server route HTTP method handling', function () { |
| 20 | + it('should return 404 JSON for GET when only POST is defined (no SPA fallback)', async function () { |
| 21 | + currentHandlers.POST = () => new Response('ok', { status: 200 }) |
| 22 | + const app = makeApp() |
| 23 | + |
| 24 | + const res = await app( |
| 25 | + new Request('http://localhost/api/test-no-get', { method: 'GET' }), |
| 26 | + ) |
| 27 | + |
| 28 | + expect(res.status).toBe(404) |
| 29 | + expect(res.headers.get('content-type')).toMatch(/application\/json/i) |
| 30 | + const txt = await res.text() |
| 31 | + expect(txt).toContain('Not Found') |
| 32 | + expect(txt.toLowerCase().startsWith('<!doctype html>')).toBe(false) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should return 200 for POST and execute the route handler', async function () { |
| 36 | + currentHandlers.POST = () => new Response('ok', { status: 200 }) |
| 37 | + const app = makeApp() |
| 38 | + |
| 39 | + const res = await app( |
| 40 | + new Request('http://localhost/api/test-no-get', { method: 'POST' }), |
| 41 | + ) |
| 42 | + |
| 43 | + expect(res.status).toBe(200) |
| 44 | + expect(await res.text()).toBe('ok') |
| 45 | + }) |
| 46 | + |
| 47 | + it('should return 404 for HEAD when GET is not defined', async function () { |
| 48 | + currentHandlers.POST = () => new Response('ok', { status: 200 }) |
| 49 | + const app = makeApp() |
| 50 | + |
| 51 | + const res = await app( |
| 52 | + new Request('http://localhost/api/test-no-get', { method: 'HEAD' }), |
| 53 | + ) |
| 54 | + |
| 55 | + expect(res.status).toBe(404) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should use GET handler when HEAD is requested and GET exists', async function () { |
| 59 | + currentHandlers.GET = () => new Response('hello', { status: 200 }) |
| 60 | + const app = makeApp() |
| 61 | + |
| 62 | + const res = await app( |
| 63 | + new Request('http://localhost/api/has-get', { method: 'HEAD' }), |
| 64 | + ) |
| 65 | + |
| 66 | + expect(res.status).toBe(200) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should execute ANY handler for unsupported methods (e.g., PUT)', async function () { |
| 70 | + currentHandlers.ANY = () => new Response('ok-any', { status: 200 }) |
| 71 | + const app = makeApp() |
| 72 | + |
| 73 | + const res = await app( |
| 74 | + new Request('http://localhost/api/any', { method: 'PUT' }), |
| 75 | + ) |
| 76 | + |
| 77 | + expect(res.status).toBe(200) |
| 78 | + expect(await res.text()).toBe('ok-any') |
| 79 | + }) |
| 80 | +}) |
| 81 | + |
0 commit comments