Skip to content

Commit 2432022

Browse files
authored
fix(miniflare): api proxy preserve multiple Set-Cookie headers (#10683)
1 parent 6ff41a6 commit 2432022

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

.changeset/hungry-crews-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
fix: api proxy preserve multiple Set-Cookie headers

packages/miniflare/src/workers/core/devalue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export function createHTTPReducers(
149149
): ReducersRevivers {
150150
return {
151151
Headers(val) {
152-
if (val instanceof impl.Headers) return Object.fromEntries(val);
152+
if (val instanceof impl.Headers) return [...val.entries()];
153153
},
154154
Request(val) {
155155
if (val instanceof impl.Request) {
@@ -169,7 +169,7 @@ export function createHTTPRevivers<RS>(
169169
return {
170170
Headers(value) {
171171
assert(typeof value === "object" && value !== null);
172-
return new impl.Headers(value as Record<string, string>);
172+
return new impl.Headers(value as string[][]);
173173
},
174174
Request(value) {
175175
assert(Array.isArray(value));

packages/miniflare/test/workers/core/serialize.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import test from "ava";
22
import { parse, stringify } from "devalue";
33
import {
4+
createHTTPReducers,
5+
createHTTPRevivers,
46
structuredSerializableReducers,
57
structuredSerializableRevivers,
68
} from "miniflare";
9+
import { NODE_PLATFORM_IMPL } from "../../../src/plugins/core/proxy/types";
710

811
test("serialize RegExp object consisting of only ascii chars", (t) => {
912
const input = new RegExp(/HelloWorld/);
@@ -24,3 +27,24 @@ test("serialize RegExp object containing non-ascii chars", (t) => {
2427
const deserialized = parse(serialized, structuredSerializableRevivers);
2528
t.deepEqual(deserialized, input);
2629
});
30+
31+
test("serialize Headers object consisting of multiple Set-Cookie headers", (t) => {
32+
const impl = NODE_PLATFORM_IMPL;
33+
34+
const headers = new impl.Headers([
35+
["content-type", "application/json"],
36+
["authorization", "Bearer token"],
37+
]);
38+
headers.append("Set-Cookie", "cookie1=value_for_cookie_1; Path=/; HttpOnly;");
39+
headers.append("Set-Cookie", "cookie2=value_for_cookie_2; Path=/; HttpOnly;");
40+
41+
const serialized = stringify(headers, createHTTPReducers(impl));
42+
const deserialized = parse(serialized, createHTTPRevivers(impl));
43+
t.true(deserialized instanceof impl.Headers);
44+
t.is(deserialized.get("content-type"), "application/json");
45+
t.is(deserialized.get("authorization"), "Bearer token");
46+
t.deepEqual(deserialized.getSetCookie(), [
47+
"cookie1=value_for_cookie_1; Path=/; HttpOnly;",
48+
"cookie2=value_for_cookie_2; Path=/; HttpOnly;",
49+
]);
50+
});

0 commit comments

Comments
 (0)