Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,17 @@ export class HttpResponse extends Macroable {
*/
send(body: any, generateEtag: boolean = this.#config.etag): void {
if (body instanceof Response) {
body.headers.forEach((value, key) => this.header(key, value))
body.headers.forEach((value, key) => {
if (key !== 'set-cookie') {
this.header(key, value)
}
})

const cookies = body.headers.getSetCookie()
if (cookies.length) {
this.append('set-cookie', cookies)
}

this.safeStatus(body.status)

if (body.body) {
Expand Down
43 changes: 43 additions & 0 deletions tests/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1608,4 +1608,47 @@ test.group('Response', (group) => {
assert.property(headers, 'x-powered-by')
assert.equal(headers['x-powered-by'], 'adonisjs')
})

test('preserve multiple Set-Cookie headers from a web-native Response', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new HttpResponseFactory().merge({ req, res, encryption, router }).create()
response.send(
new Response('ok', {
headers: [
['Set-Cookie', 'first=1; Path=/'],
['Set-Cookie', 'second=2; Path=/'],
],
})
)
response.finish()
})

const { headers } = await supertest(url).get('/').expect(200)
assert.deepEqual(headers['set-cookie'], ['first=1; Path=/', 'second=2; Path=/'])
})

test('merge Set-Cookie headers from a web-native Response with existing headers', async ({
assert,
}) => {
const { url } = await httpServer.create((req, res) => {
const response = new HttpResponseFactory().merge({ req, res, encryption, router }).create()
response.append('Set-Cookie', 'framework=1; Path=/')
response.send(
new Response('ok', {
headers: [
['Set-Cookie', 'native-a=1; Path=/'],
['Set-Cookie', 'native-b=2; Path=/'],
],
})
)
response.finish()
})

const { headers } = await supertest(url).get('/').expect(200)
assert.deepEqual(headers['set-cookie'], [
'framework=1; Path=/',
'native-a=1; Path=/',
'native-b=2; Path=/',
])
})
})