Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Miniflare 3] Allow GET requests with bodies in entry worker #677

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions packages/miniflare/src/workers/core/entry.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ function getUserRequest(
url = new URL(path, upstreamUrl);
}

request = new Request(url, {
method: request.method,
headers: request.headers,
cf: request.cf ?? env[CoreBindings.JSON_CF_BLOB],
redirect: request.redirect,
body: request.body,
});
// Note when constructing new `Request`s from `request`, we must always pass
// `request` as is to the `new Request()` constructor. Whilst prohibited by
// the `Request` API spec, `GET` requests are allowed to have bodies. If
// `Content-Length` or `Transfer-Encoding` are specified, `workerd` will give
// the request a (potentially empty) body. Passing a bodied-GET-request
// through to the `new Request()` constructor should throw, but `workerd` has
// special handling to allow this if a `Request` instance is passed.
// See https://github.com/cloudflare/workerd/issues/1122 for more details.
request = new Request(url, request);
if (request.cf === undefined) {
request = new Request(request, { cf: env[CoreBindings.JSON_CF_BLOB] });
}
request.headers.delete(CoreHeaders.ORIGINAL_URL);
return request;
}
Expand Down
43 changes: 43 additions & 0 deletions packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import http from "http";
import { AddressInfo } from "net";
import path from "path";
import { Writable } from "stream";
import { json } from "stream/consumers";
import util from "util";
import {
D1Database,
Expand Down Expand Up @@ -403,6 +404,48 @@ test("Miniflare: custom outbound service", async (t) => {
});
});

test("Miniflare: can send GET request with body", async (t) => {
// https://github.com/cloudflare/workerd/issues/1122
const mf = new Miniflare({
compatibilityDate: "2023-08-01",
modules: true,
script: `export default {
async fetch(request) {
return Response.json({
cf: request.cf,
contentLength: request.headers.get("Content-Length"),
hasBody: request.body !== null,
});
}
}`,
cf: { key: "value" },
});
t.teardown(() => mf.dispose());

// Can't use `dispatchFetch()` here as `fetch()` prohibits `GET` requests
// with bodies/`Content-Length: 0` headers
const url = await mf.ready;
function get(opts: http.RequestOptions = {}): Promise<http.IncomingMessage> {
return new Promise((resolve, reject) => {
http.get(url, opts, resolve).on("error", reject);
});
}

let res = await get();
t.deepEqual(await json(res), {
cf: { key: "value" },
contentLength: null,
hasBody: false,
});

res = await get({ headers: { "content-length": "0" } });
t.deepEqual(await json(res), {
cf: { key: "value" },
contentLength: "0",
hasBody: true,
});
});

test("Miniflare: fetch mocking", async (t) => {
const fetchMock = createFetchMock();
fetchMock.disableNetConnect();
Expand Down