Skip to content

net/http: use fake Transport network when running in Node #25663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/net/http/roundtrip_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down