Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion tests/core/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import http from 'node:http'
import { join } from 'node:path'
import { renderFile } from 'eta'
import { makeFetch } from 'supertest-fetch'
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { App, type Request, type Response } from '../../packages/app/src/index'
import { View } from '../../packages/app/src/view'
import * as req from '../../packages/req/src'
import type { RouterMethod } from '../../packages/router/src'
import { InitAppAndTest } from '../../test_helpers/initAppAndTest'

Expand Down Expand Up @@ -52,6 +53,23 @@ describe('Testing App', () => {

await fetch('/').expect(500, 'Ouch, you hurt me on / page.')
})
it('Custom onError handles unexpected route parameter errors', async () => {
const error = new Error('Unable to parse route parameters')
const getURLParams = vi.spyOn(req, 'getURLParams').mockImplementationOnce(() => {
throw error
})
const app = new App({
onError: (err, _req, res) => res.status(500).end((err as Error).message)
})

app.get('/:id', (_req, res) => res.end('matched'))

try {
await makeFetch(app.listen())('/value').expect(500, error.message)
} finally {
getURLParams.mockRestore()
}
})

it('App works with HTTP 1.1', async () => {
const app = new App()
Expand Down
7 changes: 7 additions & 0 deletions tests/core/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,4 +794,11 @@ describe('Request properties', () => {

await fetch('/', { headers: { 'If-None-Match': etag, 'Cache-Control': 'max-age=3600' } }).expectStatus(304)
})
it('req.stale returns the inverse of req.fresh', async () => {
const { fetch } = InitAppAndTest((req, res) => {
res.json({ fresh: req.fresh, stale: req.stale })
})

await fetch('/').expect(200, { fresh: false, stale: true })
})
})
5 changes: 5 additions & 0 deletions tests/modules/negotiator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ describe('Negotiator', () => {
const negotiator = new Negotiator(createRequest({ accept: 'text/html;level' }))
expect(negotiator.mediaTypes()).toEqual(['text/html'])
})

it('should match parameters without values', () => {
const negotiator = new Negotiator(createRequest({ accept: 'text/html;level' }))
expect(negotiator.mediaTypes(['text/html;level'])).toEqual(['text/html;level'])
})
})

describe('edge cases', () => {
Expand Down