Skip to content

Commit

Permalink
fix: allow overriding "Content-Type" in HttpResponse static methods (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Nov 7, 2023
1 parent 2af274c commit a037e3a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
87 changes: 71 additions & 16 deletions src/core/HttpResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,33 @@ it('creates a plain response', async () => {
expect(Object.fromEntries(response.headers.entries())).toEqual({})
})

it('creates a text response', async () => {
const response = HttpResponse.text('hello world', { status: 201 })
describe('HttpResponse.text()', () => {
it('creates a text response', async () => {
const response = HttpResponse.text('hello world', { status: 201 })

expect(response.status).toBe(201)
expect(response.statusText).toBe('Created')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('hello world')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/plain',
expect(response.status).toBe(201)
expect(response.statusText).toBe('Created')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('hello world')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/plain',
})
})

it('allows overriding the "Content-Type" response header', async () => {
const response = HttpResponse.text('hello world', {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
},
})

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('hello world')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/plain; charset=utf-8',
})
})
})

Expand Down Expand Up @@ -97,17 +115,54 @@ describe('HttpResponse.json()', () => {
'content-type': 'application/json',
})
})

it('allows overriding the "Content-Type" response header', async () => {
const response = HttpResponse.json(
{ a: 1 },
{
headers: {
'Content-Type': 'application/hal+json',
},
},
)

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.json()).toEqual({ a: 1 })
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'application/hal+json',
})
})
})

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

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('<user name="John" />')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/xml',
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('<user name="John" />')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/xml',
})
})

it('allows overriding the "Content-Type" response header', async () => {
const response = HttpResponse.xml('<user name="John" />', {
headers: {
'Content-Type': 'text/xml; charset=utf-8',
},
})

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('<user name="John" />')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/xml; charset=utf-8',
})
})
})

Expand Down
18 changes: 15 additions & 3 deletions src/core/HttpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export class HttpResponse extends Response {
init?: HttpResponseInit,
): StrictResponse<BodyType> {
const responseInit = normalizeResponseInit(init)
responseInit.headers.set('Content-Type', 'text/plain')

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

return new HttpResponse(body, responseInit) as StrictResponse<BodyType>
}

Expand All @@ -67,7 +71,11 @@ export class HttpResponse extends Response {
init?: HttpResponseInit,
): StrictResponse<BodyType> {
const responseInit = normalizeResponseInit(init)
responseInit.headers.set('Content-Type', 'application/json')

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

return new HttpResponse(
JSON.stringify(body),
responseInit,
Expand All @@ -85,7 +93,11 @@ export class HttpResponse extends Response {
init?: HttpResponseInit,
): Response {
const responseInit = normalizeResponseInit(init)
responseInit.headers.set('Content-Type', 'text/xml')

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

return new HttpResponse(body, responseInit)
}

Expand Down

0 comments on commit a037e3a

Please sign in to comment.