Skip to content

Commit 08e2e88

Browse files
johanbrandhorstbradfitz
authored andcommitted
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. Updates #25506 Change-Id: I8bc3c6808fa29293b7ac5f77b186140c4ed90b51 GitHub-Last-Rev: 43d26af GitHub-Pull-Request: #25663 Reviewed-on: https://go-review.googlesource.com/115495 Reviewed-by: Agniva De Sarker <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 33cd4fb commit 08e2e88

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/net/http/roundtrip_js.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import (
1111
"fmt"
1212
"io"
1313
"io/ioutil"
14-
"net"
14+
"os"
15+
"path"
1516
"strconv"
1617
"syscall/js"
1718
)
1819

1920
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
2021
func (*Transport) RoundTrip(req *Request) (*Response, error) {
21-
if useFakeNetwork(req) {
22+
if useFakeNetwork() {
2223
return t.roundTrip(req)
2324
}
2425
headers := js.Global.Get("Headers").New()
@@ -135,15 +136,8 @@ func (*Transport) RoundTrip(req *Request) (*Response, error) {
135136

136137
// useFakeNetwork is used to determine whether the request is made
137138
// 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"
139+
func useFakeNetwork() bool {
140+
return len(os.Args) > 0 && path.Base(os.Args[0]) == "node"
147141
}
148142

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

0 commit comments

Comments
 (0)