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(context): set headers values correctly #1808

Merged
merged 2 commits into from
Dec 12, 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
11 changes: 3 additions & 8 deletions deno_dist/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,11 @@ export class Context<
})
}

// Return Response immediately if arg is ResponseInit.
if (arg && typeof arg !== 'number') {
const res = new Response(data, arg)
const contentType = this.#preparedHeaders?.['content-type']
if (contentType) {
res.headers.set('content-type', contentType)
}
return res
this.res = new Response(data, arg)
}

const status = arg ?? this.#status
let status = typeof arg === 'number' ? arg : this.#status
this.#preparedHeaders ??= {}

this.#headers ??= new Headers()
Expand All @@ -255,6 +249,7 @@ export class Context<
for (const [k, v] of Object.entries(this.#preparedHeaders)) {
this.#headers.set(k, v)
}
status = this.#res.status
}

headers ??= {}
Expand Down
10 changes: 10 additions & 0 deletions src/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ describe('Context header', () => {
const res = c.text('foo')
expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)
})

it('Should set header values if the #this.headers is set and the arg is ResponseInit', async () => {
c.header('foo', 'bar')
const res = c.body('foo', {
headers: {
'Content-Type': 'text/plain',
},
})
expect(res.headers.get('foo')).toBe('bar')
})
})

describe('Pass a ResponseInit to respond methods', () => {
Expand Down
11 changes: 3 additions & 8 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,11 @@ export class Context<
})
}

// Return Response immediately if arg is ResponseInit.
if (arg && typeof arg !== 'number') {
const res = new Response(data, arg)
const contentType = this.#preparedHeaders?.['content-type']
if (contentType) {
res.headers.set('content-type', contentType)
}
return res
this.res = new Response(data, arg)
}

const status = arg ?? this.#status
let status = typeof arg === 'number' ? arg : this.#status
this.#preparedHeaders ??= {}

this.#headers ??= new Headers()
Expand All @@ -255,6 +249,7 @@ export class Context<
for (const [k, v] of Object.entries(this.#preparedHeaders)) {
this.#headers.set(k, v)
}
status = this.#res.status
}

headers ??= {}
Expand Down
Loading