|
| 1 | +import { assertEquals, assertThrows } from 'jsr:@std/assert' |
| 2 | +import * as Core from '@core/index.ts' |
| 3 | +import * as Middleware from '@middleware/index.ts' |
| 4 | +import * as Routing from '@routing/index.ts' |
| 5 | + |
| 6 | +function createTestContext(url = 'http://localhost/', requestInit?: RequestInit): Core.Context { |
| 7 | + const request = new Request(url, requestInit) |
| 8 | + return new Core.Context(request, new URL(url), {}) |
| 9 | +} |
| 10 | + |
| 11 | +const echoWorkerUrl = new URL('../fixtures/echo_worker.ts', import.meta.url).href |
| 12 | + |
| 13 | +Deno.test('BodyLimit with negative limit returns 413', async () => { |
| 14 | + const middleware = Middleware.Mware.bodyLimit({ limit: -1 }) |
| 15 | + const ctx = createTestContext('http://localhost/', { |
| 16 | + method: 'POST', |
| 17 | + headers: new Headers({ |
| 18 | + Accept: 'application/json', |
| 19 | + 'Content-Length': '1' |
| 20 | + }), |
| 21 | + body: 'x' |
| 22 | + }) |
| 23 | + const next = async (): Promise<Response> => new Response('should not') |
| 24 | + const res = await middleware(ctx, next) |
| 25 | + assertEquals(res !== undefined, true) |
| 26 | + if (res) { |
| 27 | + assertEquals(res.status, 413) |
| 28 | + assertEquals(await res.text(), '') |
| 29 | + } |
| 30 | +}) |
| 31 | + |
| 32 | +Deno.test('Router constructor accepts poolSize 0 as 1', async () => { |
| 33 | + const router = new Routing.Router({ |
| 34 | + routesDir: './routes', |
| 35 | + worker: { scriptURL: echoWorkerUrl, poolSize: 0 } |
| 36 | + }) |
| 37 | + const handler = (router as unknown as { handler: unknown }).handler as { |
| 38 | + workerPool?: Core.Worker |
| 39 | + } |
| 40 | + assertEquals(handler.workerPool !== undefined, true) |
| 41 | + try { |
| 42 | + const result = await handler.workerPool!.run('ok') |
| 43 | + assertEquals(result, 'ok') |
| 44 | + } finally { |
| 45 | + handler.workerPool!.terminate() |
| 46 | + } |
| 47 | +}) |
| 48 | + |
| 49 | +Deno.test('Router constructor throws on invalid worker config', () => { |
| 50 | + assertThrows(() => { |
| 51 | + new Routing.Router({ |
| 52 | + routesDir: './routes', |
| 53 | + worker: { scriptURL: 'not-a-valid-worker-specifier', poolSize: 1 } |
| 54 | + }) |
| 55 | + }) |
| 56 | +}) |
| 57 | + |
| 58 | +Deno.test('Worker#createPool throws on invalid scriptURL', () => { |
| 59 | + assertThrows(() => Core.Worker.createPool({ scriptURL: 'not-a-valid-worker-specifier' })) |
| 60 | +}) |
0 commit comments