http2_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "bytes"
  10. "errors"
  11. "flag"
  12. "fmt"
  13. "net/http"
  14. "os/exec"
  15. "strconv"
  16. "strings"
  17. "testing"
  18. "github.com/bradfitz/http2/hpack"
  19. )
  20. var knownFailing = flag.Bool("known_failing", false, "Run known-failing tests.")
  21. func condSkipFailingTest(t *testing.T) {
  22. if !*knownFailing {
  23. t.Skip("Skipping known-failing test without --known_failing")
  24. }
  25. }
  26. func init() {
  27. DebugGoroutines = true
  28. flag.BoolVar(&VerboseLogs, "verboseh2", false, "Verbose HTTP/2 debug logging")
  29. }
  30. func TestSettingString(t *testing.T) {
  31. tests := []struct {
  32. s Setting
  33. want string
  34. }{
  35. {Setting{SettingMaxFrameSize, 123}, "[MAX_FRAME_SIZE = 123]"},
  36. }
  37. for i, tt := range tests {
  38. got := fmt.Sprint(tt.s)
  39. if got != tt.want {
  40. t.Errorf("%d. for %#v, string = %q; want %q", i, tt.s, got, tt.want)
  41. }
  42. }
  43. }
  44. type twriter struct {
  45. t testing.TB
  46. st *serverTester // optional
  47. }
  48. func (w twriter) Write(p []byte) (n int, err error) {
  49. if w.st != nil {
  50. ps := string(p)
  51. for _, phrase := range w.st.logFilter {
  52. if strings.Contains(ps, phrase) {
  53. return len(p), nil // no logging
  54. }
  55. }
  56. }
  57. w.t.Logf("%s", p)
  58. return len(p), nil
  59. }
  60. // like encodeHeader, but don't add implicit psuedo headers.
  61. func encodeHeaderNoImplicit(t *testing.T, headers ...string) []byte {
  62. var buf bytes.Buffer
  63. enc := hpack.NewEncoder(&buf)
  64. for len(headers) > 0 {
  65. k, v := headers[0], headers[1]
  66. headers = headers[2:]
  67. if err := enc.WriteField(hpack.HeaderField{Name: k, Value: v}); err != nil {
  68. t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err)
  69. }
  70. }
  71. return buf.Bytes()
  72. }
  73. // Verify that curl has http2.
  74. func requireCurl(t *testing.T) {
  75. out, err := dockerLogs(curl(t, "--version"))
  76. if err != nil {
  77. t.Skipf("failed to determine curl features; skipping test")
  78. }
  79. if !strings.Contains(string(out), "HTTP2") {
  80. t.Skip("curl doesn't support HTTP2; skipping test")
  81. }
  82. }
  83. func curl(t *testing.T, args ...string) (container string) {
  84. out, err := exec.Command("docker", append([]string{"run", "-d", "--net=host", "gohttp2/curl"}, args...)...).CombinedOutput()
  85. if err != nil {
  86. t.Skipf("Failed to run curl in docker: %v, %s", err, out)
  87. }
  88. return strings.TrimSpace(string(out))
  89. }
  90. type puppetCommand struct {
  91. fn func(w http.ResponseWriter, r *http.Request)
  92. done chan<- bool
  93. }
  94. type handlerPuppet struct {
  95. ch chan puppetCommand
  96. }
  97. func newHandlerPuppet() *handlerPuppet {
  98. return &handlerPuppet{
  99. ch: make(chan puppetCommand),
  100. }
  101. }
  102. func (p *handlerPuppet) act(w http.ResponseWriter, r *http.Request) {
  103. for cmd := range p.ch {
  104. cmd.fn(w, r)
  105. cmd.done <- true
  106. }
  107. }
  108. func (p *handlerPuppet) done() { close(p.ch) }
  109. func (p *handlerPuppet) do(fn func(http.ResponseWriter, *http.Request)) {
  110. done := make(chan bool)
  111. p.ch <- puppetCommand{fn, done}
  112. <-done
  113. }
  114. func dockerLogs(container string) ([]byte, error) {
  115. out, err := exec.Command("docker", "wait", container).CombinedOutput()
  116. if err != nil {
  117. return out, err
  118. }
  119. exitStatus, err := strconv.Atoi(strings.TrimSpace(string(out)))
  120. if err != nil {
  121. return out, errors.New("unexpected exit status from docker wait")
  122. }
  123. out, err = exec.Command("docker", "logs", container).CombinedOutput()
  124. exec.Command("docker", "rm", container).Run()
  125. if err == nil && exitStatus != 0 {
  126. err = fmt.Errorf("exit status %d", exitStatus)
  127. }
  128. return out, err
  129. }
  130. func kill(container string) {
  131. exec.Command("docker", "kill", container).Run()
  132. exec.Command("docker", "rm", container).Run()
  133. }