From 220552f6624f530079b81ba866cd1ffb2f240370 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst Date: Tue, 28 May 2019 21:51:54 +0100 Subject: [PATCH] net/http: enable WASM fetch where supported The existing check was introduced to allow tests to pass on WASM without an environment where the fetch RoundTripper could run. However, the check now prohibits the use of the Fetch RoundTripper in all WASM tests, even where the RoundTripper could run. The new change should only disable the RoundTripper when used in an environment without fetch. Fixes #32289 Change-Id: I30d2e0dbcb0e64d4b1a46b583f7e984c2a57d5e5 Reviewed-on: https://go-review.googlesource.com/c/go/+/179118 Run-TryBot: Agniva De Sarker TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/net/http/roundtrip_js.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go index 9a4c369d66..6331351a83 100644 --- a/src/net/http/roundtrip_js.go +++ b/src/net/http/roundtrip_js.go @@ -11,9 +11,7 @@ import ( "fmt" "io" "io/ioutil" - "os" "strconv" - "strings" "syscall/js" ) @@ -43,9 +41,11 @@ const jsFetchCreds = "js.fetch:credentials" // Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters const jsFetchRedirect = "js.fetch:redirect" +var useFakeNetwork = js.Global().Get("fetch") == js.Undefined() + // RoundTrip implements the RoundTripper interface using the WHATWG Fetch API. func (t *Transport) RoundTrip(req *Request) (*Response, error) { - if useFakeNetwork() { + if useFakeNetwork { return t.roundTrip(req) } @@ -182,12 +182,6 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) { var errClosed = errors.New("net/http: reader is closed") -// useFakeNetwork is used to determine whether the request is made -// by a test and should be made to use the fake in-memory network. -func useFakeNetwork() bool { - return len(os.Args) > 0 && strings.HasSuffix(os.Args[0], ".test") -} - // streamReader implements an io.ReadCloser wrapper for ReadableStream. // See https://fetch.spec.whatwg.org/#readablestream for more information. type streamReader struct { -- 2.50.0