Skip to content

Commit

Permalink
fix: end response stream on mocked 204 responses (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
pacop authored Nov 26, 2023
1 parent 50d95e9 commit 9e8ceb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export function createMiddleware(
mockedResponse.body as ReadableStream,
)
stream.pipe(res)
} else {
res.end()
}
},
onPassthroughResponse() {
Expand Down
12 changes: 12 additions & 0 deletions test/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const httpServer = new HttpServer((app) => {
// so that any matching request is resolved from the mocks.
app.use(
createMiddleware(
http.post('/users', () => {
return new HttpResponse(null, { status: 204 })
}),

http.get('/user', () => {
return HttpResponse.json(
{ firstName: 'John' },
Expand Down Expand Up @@ -53,6 +57,14 @@ it('returns the mocked response when requesting the middleware', async () => {
}
})

it('returns the mocked 204 with empty body', async () => {
const res = await fetch(httpServer.http.url('/users'), { method: 'POST' })

expect(res.status).toEqual(204)
expect(res.ok).toBeTruthy()
expect(res.bodyUsed).toBeFalsy()
})

it('returns the original response given no matching request handler', async () => {
const res = await fetch(httpServer.http.url('/book'))
const text = await res.text()
Expand Down

0 comments on commit 9e8ceb4

Please sign in to comment.