Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: end response stream on mocked 204 responses #37

Merged
merged 1 commit into from
Nov 26, 2023
Merged
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
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