Skip to content

Commit

Permalink
Handle remaining branch + rename test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
neg4n committed Dec 2, 2023
1 parent f020f61 commit d267980
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/next-api-compose/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,39 @@ describe("composed route handler's http functionality", () => {
expect(response.body.error).toBe('foo')
})


it("should abort (halt) further middleware and handler execution with no error scenario when shared error handler is provided", async () => {
function haltingMiddleware() {
return new Response(JSON.stringify({ foo: 'bar' }))
}

const { GET } = compose(
{
GET: [
[haltingMiddleware],
() => {
return new Response(JSON.stringify({ foo: 'bar' }))
}
]
},
{
sharedErrorHandler: {
handler: (_method, error) => {
return new Response(JSON.stringify({ error: error.message }), {
status: 500
})
}
}
}
)

const app = createTestServer(GET)
const response = await request(app).get('/')

expect(response.status).toBe(200)
expect(response.body.foo).toBe('bar')
})

it('should correctly execute handler without middleware chain provided', async () => {
const { GET } = compose({
GET: (request) => {
Expand Down Expand Up @@ -174,7 +207,7 @@ describe("composed route handler's http functionality", () => {
expect(response.body.foo).toBe('foobar')
})

it('should abort further middleware execution and return the response if a middleware returns a Response instance.', async () => {
it('should abort (halt) further middleware and handler execution and return the response if a middleware returns a Response instance.', async () => {
function abortMiddleware(request) {
request.foo = 'bar'
return new Response(JSON.stringify({ foo: request.foo }), { status: 418 })
Expand Down

0 comments on commit d267980

Please sign in to comment.