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

net/http: only disable fetch in test #69289

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions lib/wasm/wasm_exec_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ globalThis.crypto ??= require("crypto");

require("./wasm_exec");

// Disable fetch API in tests so that
// RoundTrip ends up talking over the same fake
// network the HTTP servers currently use in
// various tests and examples.
// See net/http/roundtrip_js.go.
// See go.dev/issue/57613.
if (process.argv[2].endsWith(".test")) {
globalThis["JSFetchDisabledInTest"] = true;
}

const go = new Go();
go.argv = process.argv.slice(2);
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
Expand Down
6 changes: 2 additions & 4 deletions src/net/http/roundtrip_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"io"
"net/http/internal/ascii"
"strconv"
"strings"
"syscall/js"
)

Expand Down Expand Up @@ -47,14 +46,13 @@ const jsFetchRedirect = "js.fetch:redirect"
var jsFetchMissing = js.Global().Get("fetch").IsUndefined()

// jsFetchDisabled controls whether the use of Fetch API is disabled.
// It's set to true when we detect we're running in Node.js, so that
// It's set to true when we detect we're running in test, so that
// RoundTrip ends up talking over the same fake network the HTTP servers
// currently use in various tests and examples. See go.dev/issue/57613.
//
// TODO(go.dev/issue/60810): See if it's viable to test the Fetch API
// code path.
var jsFetchDisabled = js.Global().Get("process").Type() == js.TypeObject &&
strings.HasPrefix(js.Global().Get("process").Get("argv0").String(), "node")
var jsFetchDisabled = !js.Global().Get("JSFetchDisabledInTest").IsUndefined()

// RoundTrip implements the [RoundTripper] interface using the WHATWG Fetch API.
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
Expand Down