]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: use fake Transport network when running in Node
authorJohan Brandhorst <johan.brandhorst@gmail.com>
Thu, 31 May 2018 10:12:32 +0000 (10:12 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 31 May 2018 15:13:02 +0000 (15:13 +0000)
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: 43d26af7bc716b7a01dd8f47d7a2c2a2df549489
GitHub-Pull-Request: golang/go#25663
Reviewed-on: https://go-review.googlesource.com/115495
Reviewed-by: Agniva De Sarker <agniva.quicksilver@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/http/roundtrip_js.go

index e60b7368df72751074410db03dc810da68149642..277fc7ed3bbb55d37277216e32c790c9b78fa159 100644 (file)
@@ -11,14 +11,15 @@ import (
        "fmt"
        "io"
        "io/ioutil"
-       "net"
+       "os"
+       "path"
        "strconv"
        "syscall/js"
 )
 
 // RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
 func (*Transport) RoundTrip(req *Request) (*Response, error) {
-       if useFakeNetwork(req) {
+       if useFakeNetwork() {
                return t.roundTrip(req)
        }
        headers := js.Global.Get("Headers").New()
@@ -135,15 +136,8 @@ func (*Transport) RoundTrip(req *Request) (*Response, error) {
 
 // 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(req *Request) bool {
-       host, _, err := net.SplitHostPort(req.Host)
-       if err != nil {
-               host = req.Host
-       }
-       if ip := net.ParseIP(host); ip != nil {
-               return ip.IsLoopback(ip)
-       }
-       return host == "localhost"
+func useFakeNetwork() bool {
+       return len(os.Args) > 0 && path.Base(os.Args[0]) == "node"
 }
 
 // streamReader implements an io.ReadCloser wrapper for ReadableStream.