https://github.com/ballercat/mockttp-multi-header-issue
repro of multiple values in a header (like `set-cookie`)
https://github.com/ballercat/mockttp-multi-header-issue
Last synced: 4 months ago
JSON representation
repro of multiple values in a header (like `set-cookie`)
- Host: GitHub
- URL: https://github.com/ballercat/mockttp-multi-header-issue
- Owner: ballercat
- Created: 2023-10-23T20:03:19.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-24T16:50:12.000Z (almost 3 years ago)
- Last Synced: 2025-01-28T08:48:45.649Z (over 1 year ago)
- Language: JavaScript
- Size: 94.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Multi Headers issue with CORS enabled
```
yarn
yarn test
```
One test should pass and one should fail. Both test are identical except that the
failing spec is enabling cors via `mockServer.getLocal({ cors: true })`
The problem is that the way headers are passed to `writeHead` is broken when there
is already an existing header in the `ServerResponse`. In these cases (like when cors is on),
`writeHead` will run `setHeader` on each entry and simply mangle headers that are intended to
be multi values/arrays.
Here is the failing spec
```js
test.serial("multi-headers, cors on (this will fail)", async (t) => {
let serverCookieHeader;
const proxy = mockttp.getLocal({ debug: true, cors: true });
await proxy.start();
proxy
.forAnyRequest()
.forHost("multi-headers-mockttp-test.com")
.thenForwardTo("http://localhost:42069", {
beforeResponse(res) {
serverCookieHeader = res.headers["set-cookie"];
},
});
const app = express();
app.use((req, res, next) => {
res.cookie("bar", "bar");
next();
});
app.get("*", (_, res) => {
res.cookie("foo", "foo");
res.status(200).send("OK");
});
const server = http.createServer(app);
await promise(server.listen.bind(server), 42069);
const opts = { agent: new HttpsProxyAgent(proxy.url) };
const res = await fetch("http://multi-headers-mockttp-test.com/", opts);
await proxy.stop();
t.is(res.headers.get("set-cookie"), "bar=bar; Path=/, foo=foo; Path=/");
t.is(res.headers.get("set-cookie"), serverCookieHeader.join(", "));
await new Promise((res, rej) => {
server.close((error) => {
if (error) {
rej(error);
} else {
server.unref();
res();
}
});
});
});
```