Skip to content

net/http: improve useFakeNetwork check to allow browsers to run tests in wasm #32289

Closed
@agnivade

Description

@agnivade

What version of Go are you using (go version)?

$ go version
go version devel +1531192272 Mon May 27 17:58:39 2019 +0000 linux/amd64

net/http/rountrip_js.go has this code:

// 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")
}

This code has gone through several iterations. First it was:

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"

Then, it was changed to:

return len(os.Args) > 0 && path.Base(os.Args[0]) == "node"

And finally,

return len(os.Args) > 0 && strings.HasSuffix(os.Args[0], ".test")

The latest version will always return true if any code is invoked with go test. That prevents wasm codepath from being taken if the test is being run using a binary (by replacing the -exec flag) which spins up a browser, and loads the wasm file in it and captures the logs. Essentially a test environment for wasm code inside the browser.

I stumbled onto this while writing a tool like that. The current workaround from my end is to rename the binary to .wasm if it ends with .test. But ideally, this should be fixed in the code.

@johanbrandhorst @neelance @bradfitz

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.arch-wasmWebAssembly issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions