http2_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  5. // Licensed under the same terms as Go itself:
  6. // https://code.google.com/p/go/source/browse/LICENSE
  7. package http2
  8. import (
  9. "io"
  10. "net/http"
  11. "net/http/httptest"
  12. "os/exec"
  13. "strings"
  14. "sync/atomic"
  15. "testing"
  16. )
  17. func TestServer(t *testing.T) {
  18. requireCurl(t)
  19. ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. io.WriteString(w, "Hello, test.")
  21. }))
  22. ConfigureServer(ts.Config, &Server{})
  23. ts.TLS = ts.Config.TLSConfig // the httptest.Server has its own copy of this TLS config
  24. ts.StartTLS()
  25. defer ts.Close()
  26. var gotConn int32
  27. testHookOnConn = func() { atomic.StoreInt32(&gotConn, 1) }
  28. t.Logf("Running curl on %s", ts.URL)
  29. out, err := curl(t, "--silent", "--http2", "--insecure", "-v", ts.URL).CombinedOutput()
  30. if err != nil {
  31. t.Fatalf("Error fetching with curl: %v, %s", err, out)
  32. }
  33. t.Logf("Got: %s", out)
  34. if atomic.LoadInt32(&gotConn) == 0 {
  35. t.Error("never saw an http2 connection")
  36. }
  37. }
  38. // Verify that curl has http2.
  39. func requireCurl(t *testing.T) {
  40. out, err := curl(t, "--version").CombinedOutput()
  41. if err != nil {
  42. t.Skipf("failed to determine curl features; skipping test")
  43. }
  44. if !strings.Contains(string(out), "HTTP2") {
  45. t.Skip("curl doesn't support HTTP2; skipping test")
  46. }
  47. }
  48. func curl(t *testing.T, args ...string) *exec.Cmd {
  49. return exec.Command("docker", append([]string{"run", "--net=host", "gohttp2/curl"}, args...)...)
  50. }