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: add HttpResponse.html() #2140

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/core/HttpResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,36 @@ describe('HttpResponse.xml()', () => {
})
})

describe('HttpResponse.html()', () => {
it('creates an html response', async () => {
const response = HttpResponse.html('<p class="author">Jane Doe</p>')

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('<p class="author">Jane Doe</p>')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/html',
})
})

it('allows overriding the "Content-Type" response header', async () => {
const response = HttpResponse.html('<p class="author">Jane Doe</p>', {
headers: {
'Content-Type': 'text/html; charset=utf-8',
},
})

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('<p class="author">Jane Doe</p>')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/html; charset=utf-8',
})
})
})

it('creates an array buffer response', async () => {
const buffer = new TextEncoder().encode('hello world')
const response = HttpResponse.arrayBuffer(buffer)
Expand Down
19 changes: 19 additions & 0 deletions src/core/HttpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ export class HttpResponse extends Response {
return new HttpResponse(body, responseInit)
}

/**
* Create a `Response` with a `Content-Type: "text/html"` body.
* @example
* HttpResponse.html(`<p class="author">Jane Doe</p>`)
* HttpResponse.html(`<main id="abc-123">Main text</main>`, { status: 201 })
*/
static html<BodyType extends string>(
body?: BodyType | null,
init?: HttpResponseInit,
): Response {
const responseInit = normalizeResponseInit(init)

if (!responseInit.headers.has('Content-Type')) {
responseInit.headers.set('Content-Type', 'text/html')
}

return new HttpResponse(body, responseInit)
}

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

const worker = setupWorker(
http.get('/user', () => {
return HttpResponse.html(`
<p class="user" id="abc-123">
Jane Doe
</p>`)
}),
)

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

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

const res = await fetch('/user')
const status = res.status()
const headers = await res.allHeaders()
const text = await res.text()

expect(status).toBe(200)
expect(headers['content-type']).toBe('text/html')
expect(text).toEqual(`
<p class="user" id="abc-123">
Jane Doe
</p>`)
})
34 changes: 34 additions & 0 deletions test/node/rest-api/response/body-html.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @vitest-environment node
*/
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

const server = setupServer(
http.get('http://localhost/html', () => {
return HttpResponse.html(`
<p class="user" id="abc-123">
Jane Doe
</p>`)
}),
)

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

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

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

expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('text/html')
expect(text).toEqual(`
<p class="user" id="abc-123">
Jane Doe
</p>`)
})