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 <agniva.quicksilver@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go
index 9a4c369..6331351 100644
--- a/src/net/http/roundtrip_js.go
+++ b/src/net/http/roundtrip_js.go
@@ -11,9 +11,7 @@
 	"fmt"
 	"io"
 	"io/ioutil"
-	"os"
 	"strconv"
-	"strings"
 	"syscall/js"
 )
 
@@ -43,9 +41,11 @@
 // 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 @@
 
 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 {