Skip to content

Commit

Permalink
net/http: enable WASM fetch where supported
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
johanbrandhorst authored and bradfitz committed May 30, 2019
1 parent 16796af commit 220552f
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/net/http/roundtrip_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"syscall/js"
)

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 220552f

Please sign in to comment.