Spaces:
Running
Running
File size: 1,893 Bytes
d4b85c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import { it, expect } from 'vitest'
import { kResponsePromise, RequestController } from './RequestController'
it('creates a pending response promise on construction', () => {
const controller = new RequestController(new Request('http://localhost'))
expect(controller[kResponsePromise]).toBeInstanceOf(Promise)
expect(controller[kResponsePromise].state).toBe('pending')
})
it('resolves the response promise with the response provided to "respondWith"', async () => {
const controller = new RequestController(new Request('http://localhost'))
controller.respondWith(new Response('hello world'))
const response = (await controller[kResponsePromise]) as Response
expect(response).toBeInstanceOf(Response)
expect(response.status).toBe(200)
expect(await response.text()).toBe('hello world')
})
it('resolves the response promise with the error provided to "errorWith"', async () => {
const controller = new RequestController(new Request('http://localhost'))
const error = new Error('Oops!')
controller.errorWith(error)
await expect(controller[kResponsePromise]).resolves.toEqual(error)
})
it('throws when calling "respondWith" multiple times', () => {
const controller = new RequestController(new Request('http://localhost'))
controller.respondWith(new Response('hello world'))
expect(() => {
controller.respondWith(new Response('second response'))
}).toThrow(
'Failed to respond to the "GET http://localhost/" request: the "request" event has already been handled.'
)
})
it('throws when calling "errorWith" multiple times', () => {
const controller = new RequestController(new Request('http://localhost'))
controller.errorWith(new Error('Oops!'))
expect(() => {
controller.errorWith(new Error('second error'))
}).toThrow(
'Failed to error the "GET http://localhost/" request: the "request" event has already been handled.'
)
})
|