Hello gophers,
I'm trying to set up an httptest example that also uses HTTP/2 and it's way
harder then I expected. As an example I wrote the following test using Go
1.8:
package main
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"golang.org/x/net/http2"
)
func TestHTTP2(t *testing.T) {
f := func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor != 2 {
t.Errorf("request not using HTTP/2: %s", r.Proto)
}
fmt.Fprintln(w, "ok")
}
s := httptest.NewUnstartedServer(http.HandlerFunc(f))
// NewTLSServer didn't work, maybe try manually enabling HTTP/2 on server?
if err := http2.ConfigureServer(s.Config, nil); err != nil {
t.Fatalf("configure server: %v", err)
}
s.StartTLS()
defer s.Close()
if !strings.HasPrefix(s.URL, "https://") {
t.Fatalf("server is not listening over HTTPS: %s", s.URL)
}
trans := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Set set a custom TLSClientConfig, must manually enable HTTP/2 on
transport.
if err := http2.ConfigureTransport(trans); err != nil {
t.Fatalf("configure transport: %v", err)
}
client := &http.Client{Transport: trans}
resp, err := client.Get(s.URL)
if err != nil {
t.Errorf("get: %v", err)
}
defer resp.Body.Close()
}
However, despite this, the client is still using HTTP/1.1
$ go test -v
=== RUN TestHTTP2
--- FAIL: TestHTTP2 (0.00s)
hijack_test.go:17: request not using HTTP/2: HTTP/1.1
FAIL
exit status 1
FAIL _/home/eric/work/hijack 0.005s
Anyone have an idea of how to enable HTTP/2 with the httptest package?
Thanks,
Eric
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.