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: Using NewTLSServer is not intuitive #18411

Closed
johanbrandhorst opened this issue Dec 22, 2016 · 4 comments
Closed

net/http/httptest: Using NewTLSServer is not intuitive #18411

johanbrandhorst opened this issue Dec 22, 2016 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@johanbrandhorst
Copy link
Member

johanbrandhorst commented Dec 22, 2016

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

go1.7.4

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

Linux/amd64

What did you do?

I tried to figure out how to use httptest.NewTLSServer and found it cumbersome. I submitted an example of the boilerplate required in my CL to improve documentation, https://go-review.googlesource.com/#/c/34639/, but the consensus is that this API should be improved for 1.9 instead of patching poor API with an example in documentation.

Here's an example of a working NewTLSServer in use:

func ExampleNewTLSServer() {
	ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, client")
	}))
	defer ts.Close()

	cert, err := x509.ParseCertificate(ts.TLS.Certificates[0].Certificate[0])
	if err != nil {
		log.Fatal(err)
	}

	certpool := x509.NewCertPool()
	certpool.AddCert(cert)

	client := http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				RootCAs: certpool,
			},
		},
	}

	res, err := client.Get(ts.URL)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	greeting, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", greeting)
	// Output: Hello, client
}

What did you expect to see?

An easy to use TLS server for HTTPS testing.

What did you see instead?

A server that requires accessing and parsing TLS config internals for proper TLS authentication to work.

@bradfitz bradfitz added this to the Go1.9 milestone Dec 22, 2016
@bradfitz bradfitz added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 22, 2016
@johanbrandhorst
Copy link
Member Author

I will look at this as an expansion to my CL https://go-review.googlesource.com/#/c/34639/

@gopherbot
Copy link

CL https://golang.org/cl/34639 mentions this issue.

@cmazakas
Copy link

cmazakas commented Aug 8, 2017

This needs to be added to the official httptest docs on the Go site. It's amazingly helpful!

@johanbrandhorst
Copy link
Member Author

@LeonineKing1199 in Go 1.9 it will be much easier to use the httptest.NewTLSServer, so the docs will not be updated for the old behaviour.

See https://tip.golang.org/pkg/net/http/httptest/#example_NewTLSServer for an example of the new testing API coming in 1.9.

@golang golang locked and limited conversation to collaborators Aug 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants