Skip to content

Commit

Permalink
feat: add HttpResponse.html() static method (#2140)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Zakharchenko <[email protected]>
  • Loading branch information
scruffymongrel and kettanaito authored Aug 28, 2024
1 parent 40b17fd commit 8c5580a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
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>`)
})

0 comments on commit 8c5580a

Please sign in to comment.