You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

139 lines
4.6 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package header
  7. import (
  8. "net/http"
  9. "reflect"
  10. "testing"
  11. "time"
  12. )
  13. var getHeaderListTests = []struct {
  14. s string
  15. l []string
  16. }{
  17. {s: `a`, l: []string{`a`}},
  18. {s: `a, b , c `, l: []string{`a`, `b`, `c`}},
  19. {s: `a,, b , , c `, l: []string{`a`, `b`, `c`}},
  20. {s: `a,b,c`, l: []string{`a`, `b`, `c`}},
  21. {s: ` a b, c d `, l: []string{`a b`, `c d`}},
  22. {s: `"a, b, c", d `, l: []string{`"a, b, c"`, "d"}},
  23. {s: `","`, l: []string{`","`}},
  24. {s: `"\""`, l: []string{`"\""`}},
  25. {s: `" "`, l: []string{`" "`}},
  26. }
  27. func TestGetHeaderList(t *testing.T) {
  28. for _, tt := range getHeaderListTests {
  29. header := http.Header{"Foo": {tt.s}}
  30. if l := ParseList(header, "foo"); !reflect.DeepEqual(tt.l, l) {
  31. t.Errorf("ParseList for %q = %q, want %q", tt.s, l, tt.l)
  32. }
  33. }
  34. }
  35. var parseValueAndParamsTests = []struct {
  36. s string
  37. value string
  38. params map[string]string
  39. }{
  40. {`text/html`, "text/html", map[string]string{}},
  41. {`text/html `, "text/html", map[string]string{}},
  42. {`text/html ; `, "text/html", map[string]string{}},
  43. {`tExt/htMl`, "text/html", map[string]string{}},
  44. {`tExt/htMl; fOO=";"; hellO=world`, "text/html", map[string]string{
  45. "hello": "world",
  46. "foo": `;`,
  47. }},
  48. {`text/html; foo=bar, hello=world`, "text/html", map[string]string{"foo": "bar"}},
  49. {`text/html ; foo=bar `, "text/html", map[string]string{"foo": "bar"}},
  50. {`text/html ;foo=bar `, "text/html", map[string]string{"foo": "bar"}},
  51. {`text/html; foo="b\ar"`, "text/html", map[string]string{"foo": "bar"}},
  52. {`text/html; foo="bar\"baz\"qux"`, "text/html", map[string]string{"foo": `bar"baz"qux`}},
  53. {`text/html; foo="b,ar"`, "text/html", map[string]string{"foo": "b,ar"}},
  54. {`text/html; foo="b;ar"`, "text/html", map[string]string{"foo": "b;ar"}},
  55. {`text/html; FOO="bar"`, "text/html", map[string]string{"foo": "bar"}},
  56. {`form-data; filename="file.txt"; name=file`, "form-data", map[string]string{"filename": "file.txt", "name": "file"}},
  57. }
  58. func TestParseValueAndParams(t *testing.T) {
  59. for _, tt := range parseValueAndParamsTests {
  60. header := http.Header{"Content-Type": {tt.s}}
  61. value, params := ParseValueAndParams(header, "Content-Type")
  62. if value != tt.value {
  63. t.Errorf("%q, value=%q, want %q", tt.s, value, tt.value)
  64. }
  65. if !reflect.DeepEqual(params, tt.params) {
  66. t.Errorf("%q, param=%#v, want %#v", tt.s, params, tt.params)
  67. }
  68. }
  69. }
  70. var parseTimeValidTests = []string{
  71. "Sun, 06 Nov 1994 08:49:37 GMT",
  72. "Sunday, 06-Nov-94 08:49:37 GMT",
  73. "Sun Nov 6 08:49:37 1994",
  74. }
  75. var parseTimeInvalidTests = []string{
  76. "junk",
  77. }
  78. func TestParseTime(t *testing.T) {
  79. expected := time.Date(1994, 11, 6, 8, 49, 37, 0, time.UTC)
  80. for _, s := range parseTimeValidTests {
  81. header := http.Header{"Date": {s}}
  82. actual := ParseTime(header, "Date")
  83. if actual != expected {
  84. t.Errorf("GetTime(%q)=%v, want %v", s, actual, expected)
  85. }
  86. }
  87. for _, s := range parseTimeInvalidTests {
  88. header := http.Header{"Date": {s}}
  89. actual := ParseTime(header, "Date")
  90. if !actual.IsZero() {
  91. t.Errorf("GetTime(%q) did not return zero", s)
  92. }
  93. }
  94. }
  95. var parseAcceptTests = []struct {
  96. s string
  97. expected []AcceptSpec
  98. }{
  99. {"text/html", []AcceptSpec{{"text/html", 1}}},
  100. {"text/html; q=0", []AcceptSpec{{"text/html", 0}}},
  101. {"text/html; q=0.0", []AcceptSpec{{"text/html", 0}}},
  102. {"text/html; q=1", []AcceptSpec{{"text/html", 1}}},
  103. {"text/html; q=1.0", []AcceptSpec{{"text/html", 1}}},
  104. {"text/html; q=0.1", []AcceptSpec{{"text/html", 0.1}}},
  105. {"text/html;q=0.1", []AcceptSpec{{"text/html", 0.1}}},
  106. {"text/html, text/plain", []AcceptSpec{{"text/html", 1}, {"text/plain", 1}}},
  107. {"text/html; q=0.1, text/plain", []AcceptSpec{{"text/html", 0.1}, {"text/plain", 1}}},
  108. {"iso-8859-5, unicode-1-1;q=0.8,iso-8859-1", []AcceptSpec{{"iso-8859-5", 1}, {"unicode-1-1", 0.8}, {"iso-8859-1", 1}}},
  109. {"iso-8859-1", []AcceptSpec{{"iso-8859-1", 1}}},
  110. {"*", []AcceptSpec{{"*", 1}}},
  111. {"da, en-gb;q=0.8, en;q=0.7", []AcceptSpec{{"da", 1}, {"en-gb", 0.8}, {"en", 0.7}}},
  112. {"da, q, en-gb;q=0.8", []AcceptSpec{{"da", 1}, {"q", 1}, {"en-gb", 0.8}}},
  113. {"image/png, image/*;q=0.5", []AcceptSpec{{"image/png", 1}, {"image/*", 0.5}}},
  114. // bad cases
  115. {"value1; q=0.1.2", []AcceptSpec{{"value1", 0.1}}},
  116. {"da, en-gb;q=foo", []AcceptSpec{{"da", 1}}},
  117. }
  118. func TestParseAccept(t *testing.T) {
  119. for _, tt := range parseAcceptTests {
  120. header := http.Header{"Accept": {tt.s}}
  121. actual := ParseAccept(header, "Accept")
  122. if !reflect.DeepEqual(actual, tt.expected) {
  123. t.Errorf("ParseAccept(h, %q)=%v, want %v", tt.s, actual, tt.expected)
  124. }
  125. }
  126. }