http2_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. func init() {
  21. DebugGoroutines = true
  22. flag.BoolVar(&VerboseLogs, "verboseh2", false, "Verbose HTTP/2 debug logging")
  23. }
  24. func TestSettingString(t *testing.T) {
  25. tests := []struct {
  26. s Setting
  27. want string
  28. }{
  29. {Setting{SettingMaxFrameSize, 123}, "[MAX_FRAME_SIZE = 123]"},
  30. }
  31. for i, tt := range tests {
  32. got := fmt.Sprint(tt.s)
  33. if got != tt.want {
  34. t.Errorf("%d. for %#v, string = %q; want %q", i, tt.s, got, tt.want)
  35. }
  36. }
  37. }
  38. type twriter struct {
  39. t testing.TB
  40. }
  41. func (w twriter) Write(p []byte) (n int, err error) {
  42. w.t.Logf("%s", p)
  43. return len(p), nil
  44. }
  45. // encodeHeader encodes headers and returns their HPACK bytes. headers
  46. // must contain an even number of key/value pairs. There may be
  47. // multiple pairs for keys (e.g. "cookie"). The :method, :path, and
  48. // :scheme headers default to GET, / and https.
  49. func encodeHeader(t *testing.T, headers ...string) []byte {
  50. pseudoCount := map[string]int{}
  51. if len(headers)%2 == 1 {
  52. panic("odd number of kv args")
  53. }
  54. keys := []string{":method", ":path", ":scheme"}
  55. vals := map[string][]string{
  56. ":method": {"GET"},
  57. ":path": {"/"},
  58. ":scheme": {"https"},
  59. }
  60. for len(headers) > 0 {
  61. k, v := headers[0], headers[1]
  62. headers = headers[2:]
  63. if _, ok := vals[k]; !ok {
  64. keys = append(keys, k)
  65. }
  66. if strings.HasPrefix(k, ":") {
  67. pseudoCount[k]++
  68. if pseudoCount[k] == 1 {
  69. vals[k] = []string{v}
  70. } else {
  71. // Allows testing of invalid headers w/ dup pseudo fields.
  72. vals[k] = append(vals[k], v)
  73. }
  74. } else {
  75. vals[k] = append(vals[k], v)
  76. }
  77. }
  78. var buf bytes.Buffer
  79. enc := hpack.NewEncoder(&buf)
  80. for _, k := range keys {
  81. for _, v := range vals[k] {
  82. if err := enc.WriteField(hpack.HeaderField{Name: k, Value: v}); err != nil {
  83. t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err)
  84. }
  85. }
  86. }
  87. return buf.Bytes()
  88. }
  89. // Verify that curl has http2.
  90. func requireCurl(t *testing.T) {
  91. out, err := dockerLogs(curl(t, "--version"))
  92. if err != nil {
  93. t.Skipf("failed to determine curl features; skipping test")
  94. }
  95. if !strings.Contains(string(out), "HTTP2") {
  96. t.Skip("curl doesn't support HTTP2; skipping test")
  97. }
  98. }
  99. func curl(t *testing.T, args ...string) (container string) {
  100. out, err := exec.Command("docker", append([]string{"run", "-d", "--net=host", "gohttp2/curl"}, args...)...).CombinedOutput()
  101. if err != nil {
  102. t.Skipf("Failed to run curl in docker: %v, %s", err, out)
  103. }
  104. return strings.TrimSpace(string(out))
  105. }
  106. type puppetCommand struct {
  107. fn func(w http.ResponseWriter, r *http.Request)
  108. done chan<- bool
  109. }
  110. type handlerPuppet struct {
  111. ch chan puppetCommand
  112. }
  113. func newHandlerPuppet() *handlerPuppet {
  114. return &handlerPuppet{
  115. ch: make(chan puppetCommand),
  116. }
  117. }
  118. func (p *handlerPuppet) act(w http.ResponseWriter, r *http.Request) {
  119. for cmd := range p.ch {
  120. cmd.fn(w, r)
  121. cmd.done <- true
  122. }
  123. }
  124. func (p *handlerPuppet) done() { close(p.ch) }
  125. func (p *handlerPuppet) do(fn func(http.ResponseWriter, *http.Request)) {
  126. done := make(chan bool)
  127. p.ch <- puppetCommand{fn, done}
  128. <-done
  129. }
  130. func dockerLogs(container string) ([]byte, error) {
  131. out, err := exec.Command("docker", "wait", container).CombinedOutput()
  132. if err != nil {
  133. return out, err
  134. }
  135. exitStatus, err := strconv.Atoi(strings.TrimSpace(string(out)))
  136. if err != nil {
  137. return out, errors.New("unexpected exit status from docker wait")
  138. }
  139. out, err = exec.Command("docker", "logs", container).CombinedOutput()
  140. exec.Command("docker", "rm", container).Run()
  141. if err == nil && exitStatus != 0 {
  142. err = fmt.Errorf("exit status %d", exitStatus)
  143. }
  144. return out, err
  145. }
  146. func kill(container string) {
  147. exec.Command("docker", "kill", container).Run()
  148. exec.Command("docker", "rm", container).Run()
  149. }