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

feat(HttpResponse): add empty response helper for strict empty responses #1807

Closed
Closed
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
16 changes: 16 additions & 0 deletions src/core/HttpResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ it('creates a json response', async () => {
expect(await response.json()).toEqual({ firstName: 'John' })
})

it('creates an empty response with no-content status', async () => {
const response = HttpResponse.empty()

expect(response.status).toBe(204)
expect(response.statusText).toBe('No Content')
expect(response.body).toBeNull()
})

it('creates an empty response with overwritten status', async () => {
const response = HttpResponse.empty({ status: 201 })

expect(response.status).toBe(201)
expect(response.statusText).toBe('Created')
expect(response.body).toBeNull()
})

it('creates an xml response', async () => {
const response = HttpResponse.xml('<user name="John" />')

Expand Down
14 changes: 14 additions & 0 deletions src/core/HttpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ export class HttpResponse extends Response {
) as StrictResponse<BodyType>
}

/**
* Create a `Response` with an empty body and status code 204 by default.
* @example
* HttpResponse.empty()
* HttpResponse.empty({ status: 201 })
*/
static empty(init?: HttpResponseInit): StrictResponse<null> {
const noContentInit: HttpResponseInit = init
? { ...init, status: init.status ?? 204 }
: { status: 204 }
const responseInit = normalizeResponseInit(noContentInit)
return new HttpResponse(null, responseInit) as StrictResponse<null>
}

/**
* Create a `Response` with a `Content-Type: "application/xml"` body.
* @example
Expand Down
10 changes: 10 additions & 0 deletions test/browser/rest-api/response/body/body-empty.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { http, HttpResponse } from 'msw'
import { setupWorker } from 'msw/browser'

const worker = setupWorker(
http.get('/empty', () => {
return HttpResponse.empty()
}),
)

worker.start()
14 changes: 14 additions & 0 deletions test/browser/rest-api/response/body/body-empty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, expect } from '../../../playwright.extend'

test('responds with a empty response body', async ({ loadExample, fetch }) => {
await loadExample(require.resolve('./body-empty.mocks.ts'))

const res = await fetch('/empty')
const headers = await res.allHeaders()

expect(res.status()).toBe(204)
expect(headers).not.toHaveProperty('content-type')
await expect(res.json()).rejects.toThrowError(
'No data found for resource with given identifier',
)
})
29 changes: 29 additions & 0 deletions test/node/rest-api/response/body-empty.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @jest-environment node
*/
import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

const server = setupServer(
http.get('http://localhost/empty', () => {
return HttpResponse.empty()
}),
)

beforeAll(() => {
server.listen()
})

afterAll(() => {
server.close()
})

test('responds with an empty response body', async () => {
const res = await fetch('http://localhost/empty')
const body = await res.buffer()

expect(res.status).toBe(204)
expect(res.headers.get('content-type')).toBeNull()
expect(body.byteLength).toBe(0)
})
10 changes: 10 additions & 0 deletions test/typings/rest.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,13 @@ http.get<never, never, string | string[]>('/user', () =>
http.get<never, never, { label: boolean }>('/user', () =>
HttpResponse.json({ label: true }),
)

// Empty response body requires a strict response
http.get<never, never, null>(
'/user',
// @ts-expect-error HttpResponse is not StrictResponse<null>
() => new HttpResponse(),
)

// Empty response can be used for en empty response body
http.get<never, never, null>('/user', () => HttpResponse.empty())
Loading