Skip to content

Commit 2ed8445

Browse files
net/http: use fake Transport network when running in Node
Replaces the existing local loopback check with a check to see whether the program is being interpreted by Node. This means tests that are run with Node will use the fake network while still allowing users who are using js/wasm to talk to local networks.
1 parent fffb3a5 commit 2ed8445

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

src/net/http/roundtrip_js.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
"fmt"
1212
"io"
1313
"io/ioutil"
14-
"net"
14+
"os"
1515
"strconv"
1616
"syscall/js"
1717
)
1818

1919
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
2020
func (*Transport) RoundTrip(req *Request) (*Response, error) {
21-
if useFakeNetwork(req) {
21+
if useFakeNetwork() {
2222
return t.roundTrip(req)
2323
}
2424
headers := js.Global.Get("Headers").New()
@@ -135,15 +135,8 @@ func (*Transport) RoundTrip(req *Request) (*Response, error) {
135135

136136
// useFakeNetwork is used to determine whether the request is made
137137
// by a test and should be made to use the fake in-memory network.
138-
func useFakeNetwork(req *Request) bool {
139-
host, _, err := net.SplitHostPort(req.Host)
140-
if err != nil {
141-
host = req.Host
142-
}
143-
if ip := net.ParseIP(host); ip != nil {
144-
return ip.IsLoopback(ip)
145-
}
146-
return host == "localhost"
138+
func useFakeNetwork() bool {
139+
return len(os.Args) > 0 && os.Args[0] == "node"
147140
}
148141

149142
// streamReader implements an io.ReadCloser wrapper for ReadableStream.

0 commit comments

Comments
 (0)