http2_test.go 3.8 KB

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