Skip to content
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

net/http/httptest: serve flag not read when not running as a test; remove flag in that case? #28619

Closed
pwfcurry opened this issue Nov 6, 2018 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@pwfcurry
Copy link

pwfcurry commented Nov 6, 2018

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

$ go version
go version go1.10.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users//Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users//go"
GORACE=""
GOROOT="/usr/local/Cellar/go@1.10/1.10.4/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go@1.10/1.10.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wj/3cmxv_m511z7cz93jw81kkv00000gp/T/go-build676852346=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func main() {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
	fmt.Println(server.URL)
}
go run main.go -httptest.server=localhost:12345

The URL printed is 127.0.0.1:random port.

However, importing "flag" and running flag.Parse() before creating the server will ensure flag is read as expected.

Surely the httptest server should ensure .Parse() has run (via .Parsed(), & run if not)?

What did you expect to see?

URL specified by flag should be used.

localhost:12345

What did you see instead?

Random port is used.

127.0.0.1:61234
@bradfitz
Copy link
Contributor

bradfitz commented Nov 6, 2018

httptest is meant for use in tests, and the testing package parses flags.

If anything, I'd prefer we didn't register that flag unless we were running as a test.

@bradfitz bradfitz changed the title httptest.serve flag not read net/http/httptest: serve flag not read when not running as a test; remove flag in that case? Nov 6, 2018
@bradfitz bradfitz added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 6, 2018
@bradfitz bradfitz self-assigned this Nov 6, 2018
@bradfitz bradfitz added this to the Go1.12 milestone Nov 6, 2018
@pwfcurry
Copy link
Author

pwfcurry commented Nov 7, 2018

Thanks @bradfitz, makes sense.

I was surprised that the host/port couldn't be passed as an optional parameter when creating a new test server - do you know if this was intentionally omitted?

@bradfitz
Copy link
Contributor

bradfitz commented Nov 7, 2018

I was surprised that the host/port couldn't be passed as an optional parameter when creating a new test server - do you know if this was intentionally omitted?

If you want to specify an explicit port, there's a whole package for running HTTP servers:

https://golang.org/pkg/net/http/

The httptest package is for making it easy to write tests, and in a test you shouldn't need to worry about little details like IPs or port numbers.

@gopherbot
Copy link

Change https://golang.org/cl/154217 mentions this issue: net/http/httptest: don't register a flag unless it looks like it's in use

@golang golang locked and limited conversation to collaborators Dec 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants