Skip to content

Commit

Permalink
[Miniflare 2] Fix behaviour mismatch in Request constructor (#546)
Browse files Browse the repository at this point in the history
* Fix behaviour mismatch in Request constructor

* Add test for init vs input cf override
  • Loading branch information
DaniFoldi authored Mar 23, 2023
1 parent f05a97e commit f9ca91f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/standards/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ export class Request extends Body<BaseRequest> {
init?.body as { [kContentLength]?: number }
)?.[kContentLength];

const cf = input instanceof Request ? input.#cf : init?.cf;
const cf = input instanceof Request ? init?.cf ?? input.#cf : init?.cf;
if (input instanceof BaseRequest && !init) {
// For cloning
super(input);
Expand Down
24 changes: 24 additions & 0 deletions packages/core/test/standards/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Body,
IncomingRequestCfProperties,
Request,
RequestInitCfProperties,
Response,
_getBodyLength,
_getURLList,
Expand Down Expand Up @@ -475,6 +476,29 @@ test("Request: supports non-standard properties", (t) => {
// Check cf has been cloned
t.not(req.cf, cf);
});
test("Request: cf defaults to input.cf", (t) => {
const req = new Request("http://localhost", {
method: "POST",
cf,
});
const req2 = new Request(req);
t.deepEqual(req.cf, req2.cf);
// Check cf has been cloned
t.not(req.cf, req2.cf);
});
test("Request: init.cf overrides input.cf", (t) => {
const req = new Request("http://localhost", {
method: "POST",
cf,
});
const req2 = new Request(req, {
cf: {
cacheKey: "test",
},
});
t.notDeepEqual(req.cf, req2.cf);
t.is((req2.cf as RequestInitCfProperties).cacheKey, "test");
});
test("Request: doesn't detach ArrayBuffers", async (t) => {
// Check with ArrayBuffer
const buffer = utf8Encode("test1").buffer;
Expand Down

0 comments on commit f9ca91f

Please sign in to comment.