Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Limit duplicated headers to Set-Cookie
Browse files Browse the repository at this point in the history
Revert mockoon/mockoon#318. Allowing duplicated headers was causing some bugs with the pre-flight OPTIONS calls and breaking the route headers precedence over environment headers.
Limiting deplicated headers to the Set-Cookie header seems to be a reasonable solution, considering it's a non-standard behavior.

See mockoon/mockoon#480
  • Loading branch information
Guillaume committed May 19, 2021
1 parent e78ec42 commit 4e6784e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mockoon/commons-server",
"description": "Mockoon's commons server library. Used in Mockoon desktop application and CLI.",
"version": "2.2.0",
"version": "2.2.1",
"author": {
"name": "Guillaume Monnet",
"email": "hi@255kb.dev",
Expand Down
29 changes: 14 additions & 15 deletions src/libs/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ export class MockoonServer extends (EventEmitter as new () => TypedEmitter<Serve
*/
private setHeaders(headers: Header[], target: any, request: Request) {
headers.forEach((header: Header) => {
const isSetCookie = header.key.toLowerCase() === 'set-cookie';
const parsedHeaderValue = this.parseHeader(header, request);

if (parsedHeaderValue === null) {
Expand All @@ -573,26 +574,24 @@ export class MockoonServer extends (EventEmitter as new () => TypedEmitter<Serve

if (target.set) {
// for express.Response
if (header.key.toLowerCase() === 'content-type') {
target.set(header.key, parsedHeaderValue);
} else {
if (isSetCookie) {
target.append(header.key, parsedHeaderValue);
} else {
target.set(header.key, parsedHeaderValue);
}
} else if (target.setHeader) {
// for proxy http.OutgoingMessage
target.setHeader(
header.key,
this.appendHeaderValue(
target.getHeader(header.key),
parsedHeaderValue
)
);
// for proxy http.OutgoingMessage | ClientRequest
target.setHeader(header.key, parsedHeaderValue);
} else {
// for http.IncomingMessage
target.headers[header.key] = this.appendHeaderValue(
target.headers[header.key],
parsedHeaderValue
);
if (isSetCookie) {
target.headers[header.key] = this.appendHeaderValue(
target.headers[header.key],
parsedHeaderValue
);
} else {
target.headers[header.key] = parsedHeaderValue;
}
}
});
}
Expand Down

0 comments on commit 4e6784e

Please sign in to comment.