2023-01-12 10:53:13 +01:00
|
|
|
import { HTTPException } from './http-exception'
|
|
|
|
|
2024-02-25 11:07:33 +01:00
|
|
|
describe('HTTPException', () => {
|
|
|
|
it('Should be 401 HTTP exception object', async () => {
|
2023-01-12 10:53:13 +01:00
|
|
|
// We should throw an exception if is not authorized
|
|
|
|
// because next handlers should not be fired.
|
2023-04-26 04:38:08 +02:00
|
|
|
const exception = new HTTPException(401, {
|
|
|
|
message: 'Unauthorized',
|
|
|
|
})
|
2024-02-25 11:07:33 +01:00
|
|
|
const res = exception.getResponse()
|
|
|
|
|
|
|
|
expect(res.status).toBe(401)
|
|
|
|
expect(await res.text()).toBe('Unauthorized')
|
2023-01-12 10:53:13 +01:00
|
|
|
expect(exception.status).toBe(401)
|
|
|
|
expect(exception.message).toBe('Unauthorized')
|
2024-02-25 11:07:33 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should be accessible to the object causing the exception', async () => {
|
|
|
|
// We should pass the cause of the error to the cause option
|
|
|
|
// because it makes debugging easier.
|
|
|
|
const error = new Error('Server Error')
|
|
|
|
const exception = new HTTPException(500, {
|
|
|
|
message: 'Internal Server Error',
|
|
|
|
cause: error,
|
|
|
|
})
|
2023-01-12 10:53:13 +01:00
|
|
|
const res = exception.getResponse()
|
2024-02-25 11:07:33 +01:00
|
|
|
|
|
|
|
expect(res.status).toBe(500)
|
|
|
|
expect(await res.text()).toBe('Internal Server Error')
|
|
|
|
expect(exception.status).toBe(500)
|
|
|
|
expect(exception.message).toBe('Internal Server Error')
|
|
|
|
expect(exception.cause).toBe(error)
|
2023-01-12 10:53:13 +01:00
|
|
|
})
|
2024-05-22 22:20:37 +02:00
|
|
|
|
|
|
|
it('Should prioritize the status code over the code in the response', async () => {
|
|
|
|
const exception = new HTTPException(400, {
|
|
|
|
res: new Response('An exception', {
|
|
|
|
status: 200,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
const res = exception.getResponse()
|
|
|
|
expect(res.status).toBe(400)
|
|
|
|
expect(await res.text()).toBe('An exception')
|
|
|
|
})
|
2023-01-12 10:53:13 +01:00
|
|
|
})
|