forwardemail / forwardemail/superagent

[bug] Setting cookies does not take effect

Open
#1,793 0 comments 0 reactions 0 assignees View on GitHub
Bug
Dominant language
JavaScript
Stars
16.6k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

## Describe the bug

**Node.js version:**
v16.20.2
**OS version:**
macOS 14.2.1
**Description:**

I use `set('Cookie',[xxx])`, but the cookie was not added to the request header
```
const request = require('supertest');
const server = require('./server.js');

const agent = request.agent(server);

const postRes = await agent.post('/verify').set('Cookie', [`csrf-token=xxxxx`]);
```

## Actual behavior

Cookie is not added to the request header

## Expected behavior

Cookies can be added to request headers

## Code to reproduce

My code is as follows,The first time I set cookie, it can be added to the request header correctly.But the second time I set cookie, it was not added to the request header.
```
const request = require('supertest');
const server = require('./server.js');

const agent = request.agent(server);
const postRes = await agent.post('/verify').set('Cookie', [`csrf-token=xxxxx`]);
const postRes1 = await agent.post('/verify').set('Cookie', [`csrf-token=xxxxx`]);
```
I briefly checked this problem, It's because my server returned the `set-cookie` response header. Then the second request will only bring the cookie returned by the first request, but not the cookie I set.like this
```
Cookie: token=xxx
```

I hope that both the cookies returned by the first request and the cookies I set are added to the response headers correctly. like this

```
Cookie: csrf-token=xxxxx;token=xxx
```

I checked the source code

https://github.com/ladjs/superagent/blob/1c8338b2e0a3b8f604d08acc7f3cbe305be1e571/src/node/index.js#L856

![image](https://github.com/ladjs/superagent/assets/29347231/57344077-8e17-493b-9be9-786db861fd5d)

`hasOwn(this._header, 'cookie')` return `false`, because of my `set('Cookie',[xxx])` it will be added as `header['Cookie']=[xxx]`,instead of `header['cookie']=xxx`

If I write like this, it will add the cookie to the header correctly
```
const postRes = await agent.post('/verify').set('Cookie', [`csrf-token=xxxxx`]);
const postRes1 = await agent.post('/verify').set('cookie', `csrf-token=xxxxx`);
```

I don't know if this is a bug or if it's by design

## Checklist

- [x] I have searched through GitHub issues for similar issues.
- [x] I have completely read through the README and documentation.
- [x] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.