hpack_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2014 The Go Authors.
  2. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  3. // Licensed under the same terms as Go itself:
  4. // https://code.google.com/p/go/source/browse/LICENSE
  5. package hpack
  6. import (
  7. "bufio"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. )
  13. func TestStaticTable(t *testing.T) {
  14. fromSpec := `
  15. +-------+-----------------------------+---------------+
  16. | 1 | :authority | |
  17. | 2 | :method | GET |
  18. | 3 | :method | POST |
  19. | 4 | :path | / |
  20. | 5 | :path | /index.html |
  21. | 6 | :scheme | http |
  22. | 7 | :scheme | https |
  23. | 8 | :status | 200 |
  24. | 9 | :status | 204 |
  25. | 10 | :status | 206 |
  26. | 11 | :status | 304 |
  27. | 12 | :status | 400 |
  28. | 13 | :status | 404 |
  29. | 14 | :status | 500 |
  30. | 15 | accept-charset | |
  31. | 16 | accept-encoding | gzip, deflate |
  32. | 17 | accept-language | |
  33. | 18 | accept-ranges | |
  34. | 19 | accept | |
  35. | 20 | access-control-allow-origin | |
  36. | 21 | age | |
  37. | 22 | allow | |
  38. | 23 | authorization | |
  39. | 24 | cache-control | |
  40. | 25 | content-disposition | |
  41. | 26 | content-encoding | |
  42. | 27 | content-language | |
  43. | 28 | content-length | |
  44. | 29 | content-location | |
  45. | 30 | content-range | |
  46. | 31 | content-type | |
  47. | 32 | cookie | |
  48. | 33 | date | |
  49. | 34 | etag | |
  50. | 35 | expect | |
  51. | 36 | expires | |
  52. | 37 | from | |
  53. | 38 | host | |
  54. | 39 | if-match | |
  55. | 40 | if-modified-since | |
  56. | 41 | if-none-match | |
  57. | 42 | if-range | |
  58. | 43 | if-unmodified-since | |
  59. | 44 | last-modified | |
  60. | 45 | link | |
  61. | 46 | location | |
  62. | 47 | max-forwards | |
  63. | 48 | proxy-authenticate | |
  64. | 49 | proxy-authorization | |
  65. | 50 | range | |
  66. | 51 | referer | |
  67. | 52 | refresh | |
  68. | 53 | retry-after | |
  69. | 54 | server | |
  70. | 55 | set-cookie | |
  71. | 56 | strict-transport-security | |
  72. | 57 | transfer-encoding | |
  73. | 58 | user-agent | |
  74. | 59 | vary | |
  75. | 60 | via | |
  76. | 61 | www-authenticate | |
  77. +-------+-----------------------------+---------------+
  78. `
  79. bs := bufio.NewScanner(strings.NewReader(fromSpec))
  80. re := regexp.MustCompile(`\| (\d+)\s+\| (\S+)\s*\| (\S(.*\S)?)?\s+\|`)
  81. for bs.Scan() {
  82. l := bs.Text()
  83. if !strings.Contains(l, "|") {
  84. continue
  85. }
  86. m := re.FindStringSubmatch(l)
  87. if m == nil {
  88. continue
  89. }
  90. i, err := strconv.Atoi(m[1])
  91. if err != nil {
  92. t.Errorf("Bogus integer on line %q", l)
  93. continue
  94. }
  95. if i < 1 || i > len(staticTable) {
  96. t.Errorf("Bogus index %d on line %q", i, l)
  97. continue
  98. }
  99. if got, want := staticTable[i-1].Name, m[2]; got != want {
  100. t.Errorf("header index %d name = %q; want %q", i, got, want)
  101. }
  102. if got, want := staticTable[i-1].Value, m[3]; got != want {
  103. t.Errorf("header index %d value = %q; want %q", i, got, want)
  104. }
  105. }
  106. if err := bs.Err(); err != nil {
  107. t.Error(err)
  108. }
  109. }
  110. func TestHeaderTableAt(t *testing.T) {
  111. var ht headerTable
  112. if got, want := ht.at(2), (HeaderField{":method", "GET"}); got != want {
  113. t.Errorf("at(2) = %q; want %q", got, want)
  114. }
  115. ht.add(HeaderField{"foo", "bar"})
  116. if got, want := ht.at(1), (HeaderField{"foo", "bar"}); got != want {
  117. t.Errorf("at(1) = %q; want %q", got, want)
  118. }
  119. if got, want := ht.at(3), (HeaderField{":method", "GET"}); got != want {
  120. t.Errorf("at(3) = %q; want %q", got, want)
  121. }
  122. if got, want := ht.at(62), (HeaderField{"www-authenticate", ""}); got != want {
  123. t.Errorf("at(62) = %q; want %q", got, want)
  124. }
  125. }