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.
 
 
 

345 lines
9.0 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package proxyutil
  15. import (
  16. "net/http"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestRequestHeader(t *testing.T) {
  21. req, err := http.NewRequest("GET", "http://example.com", nil)
  22. if err != nil {
  23. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  24. }
  25. h := RequestHeader(req)
  26. tt := []struct {
  27. name string
  28. value string
  29. }{
  30. {
  31. name: "Host",
  32. value: "example.com",
  33. },
  34. {
  35. name: "Test-Header",
  36. value: "true",
  37. },
  38. {
  39. name: "Content-Length",
  40. value: "100",
  41. },
  42. {
  43. name: "Transfer-Encoding",
  44. value: "chunked",
  45. },
  46. }
  47. for i, tc := range tt {
  48. if err := h.Set(tc.name, tc.value); err != nil {
  49. t.Errorf("%d. h.Set(%q, %q): got %v, want no error", i, tc.name, tc.value, err)
  50. }
  51. }
  52. if got, want := req.Host, "example.com"; got != want {
  53. t.Errorf("req.Host: got %q, want %q", got, want)
  54. }
  55. if got, want := req.Header.Get("Test-Header"), "true"; got != want {
  56. t.Errorf("req.Header.Get(%q): got %q, want %q", "Test-Header", got, want)
  57. }
  58. if got, want := req.ContentLength, int64(100); got != want {
  59. t.Errorf("req.ContentLength: got %d, want %d", got, want)
  60. }
  61. if got, want := req.TransferEncoding, []string{"chunked"}; !reflect.DeepEqual(got, want) {
  62. t.Errorf("req.TransferEncoding: got %v, want %v", got, want)
  63. }
  64. if got, want := len(h.Map()), 4; got != want {
  65. t.Errorf("h.Map(): got %d entries, want %d entries", got, want)
  66. }
  67. for n, vs := range h.Map() {
  68. var want string
  69. switch n {
  70. case "Host":
  71. want = "example.com"
  72. case "Content-Length":
  73. want = "100"
  74. case "Transfer-Encoding":
  75. want = "chunked"
  76. case "Test-Header":
  77. want = "true"
  78. default:
  79. t.Errorf("h.Map(): got unexpected %s header", n)
  80. }
  81. if got := vs[0]; got != want {
  82. t.Errorf("h.Map(): got %s header with value %s, want value %s", n, got, want)
  83. }
  84. }
  85. for i, tc := range tt {
  86. got, ok := h.All(tc.name)
  87. if !ok {
  88. t.Errorf("%d. h.All(%q): got false, want true", i, tc.name)
  89. }
  90. if want := []string{tc.value}; !reflect.DeepEqual(got, want) {
  91. t.Errorf("%d. h.All(%q): got %v, want %v", i, tc.name, got, want)
  92. }
  93. if got, want := h.Get(tc.name), tc.value; got != want {
  94. t.Errorf("%d. h.Get(%q): got %q, want %q", i, tc.name, got, want)
  95. }
  96. h.Del(tc.name)
  97. }
  98. if got, want := req.Host, ""; got != want {
  99. t.Errorf("req.Host: got %q, want %q", got, want)
  100. }
  101. if got, want := req.Header.Get("Test-Header"), ""; got != want {
  102. t.Errorf("req.Header.Get(%q): got %q, want %q", "Test-Header", got, want)
  103. }
  104. if got, want := req.ContentLength, int64(-1); got != want {
  105. t.Errorf("req.ContentLength: got %d, want %d", got, want)
  106. }
  107. if got := req.TransferEncoding; got != nil {
  108. t.Errorf("req.TransferEncoding: got %v, want nil", got)
  109. }
  110. for i, tc := range tt {
  111. if got, want := h.Get(tc.name), ""; got != want {
  112. t.Errorf("%d. h.Get(%q): got %q, want %q", i, tc.name, got, want)
  113. }
  114. got, ok := h.All(tc.name)
  115. if ok {
  116. t.Errorf("%d. h.All(%q): got ok, want !ok", i, tc.name)
  117. }
  118. if got != nil {
  119. t.Errorf("%d. h.All(%q): got %v, want nil", i, tc.name, got)
  120. }
  121. }
  122. }
  123. func TestRequestHeaderAdd(t *testing.T) {
  124. req, err := http.NewRequest("GET", "http://example.com", nil)
  125. if err != nil {
  126. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  127. }
  128. req.Host = "" // Set to empty so add may overwrite.
  129. h := RequestHeader(req)
  130. tt := []struct {
  131. name string
  132. values []string
  133. errOnSecondValue bool
  134. }{
  135. {
  136. name: "Host",
  137. values: []string{"example.com", "invalid.com"},
  138. errOnSecondValue: true,
  139. },
  140. {
  141. name: "Test-Header",
  142. values: []string{"first", "second"},
  143. },
  144. {
  145. name: "Content-Length",
  146. values: []string{"100", "101"},
  147. errOnSecondValue: true,
  148. },
  149. {
  150. name: "Transfer-Encoding",
  151. values: []string{"chunked", "gzip"},
  152. },
  153. }
  154. for i, tc := range tt {
  155. if err := h.Add(tc.name, tc.values[0]); err != nil {
  156. t.Errorf("%d. h.Add(%q, %q): got %v, want no error", i, tc.name, tc.values[0], err)
  157. }
  158. if err := h.Add(tc.name, tc.values[1]); err != nil && !tc.errOnSecondValue {
  159. t.Errorf("%d. h.Add(%q, %q): got %v, want no error", i, tc.name, tc.values[1], err)
  160. }
  161. }
  162. if got, want := req.Host, "example.com"; got != want {
  163. t.Errorf("req.Host: got %q, want %q", got, want)
  164. }
  165. if got, want := req.Header["Test-Header"], []string{"first", "second"}; !reflect.DeepEqual(got, want) {
  166. t.Errorf("req.Header[%q]: got %v, want %v", "Test-Header", got, want)
  167. }
  168. if got, want := req.ContentLength, int64(100); got != want {
  169. t.Errorf("req.ContentLength: got %d, want %d", got, want)
  170. }
  171. if got, want := req.TransferEncoding, []string{"chunked", "gzip"}; !reflect.DeepEqual(got, want) {
  172. t.Errorf("req.TransferEncoding: got %v, want %v", got, want)
  173. }
  174. }
  175. func TestResponseHeader(t *testing.T) {
  176. res := NewResponse(200, nil, nil)
  177. h := ResponseHeader(res)
  178. tt := []struct {
  179. name string
  180. value string
  181. }{
  182. {
  183. name: "Test-Header",
  184. value: "true",
  185. },
  186. {
  187. name: "Content-Length",
  188. value: "100",
  189. },
  190. {
  191. name: "Transfer-Encoding",
  192. value: "chunked",
  193. },
  194. }
  195. for i, tc := range tt {
  196. if err := h.Set(tc.name, tc.value); err != nil {
  197. t.Errorf("%d. h.Set(%q, %q): got %v, want no error", i, tc.name, tc.value, err)
  198. }
  199. }
  200. if got, want := res.Header.Get("Test-Header"), "true"; got != want {
  201. t.Errorf("res.Header.Get(%q): got %q, want %q", "Test-Header", got, want)
  202. }
  203. if got, want := res.ContentLength, int64(100); got != want {
  204. t.Errorf("res.ContentLength: got %d, want %d", got, want)
  205. }
  206. if got, want := res.TransferEncoding, []string{"chunked"}; !reflect.DeepEqual(got, want) {
  207. t.Errorf("res.TransferEncoding: got %v, want %v", got, want)
  208. }
  209. if got, want := len(h.Map()), 3; got != want {
  210. t.Errorf("h.Map(): got %d entries, want %d entries", got, want)
  211. }
  212. for n, vs := range h.Map() {
  213. var want string
  214. switch n {
  215. case "Content-Length":
  216. want = "100"
  217. case "Transfer-Encoding":
  218. want = "chunked"
  219. case "Test-Header":
  220. want = "true"
  221. default:
  222. t.Errorf("h.Map(): got unexpected %s header", n)
  223. }
  224. if got := vs[0]; got != want {
  225. t.Errorf("h.Map(): got %s header with value %s, want value %s", n, got, want)
  226. }
  227. }
  228. for i, tc := range tt {
  229. got, ok := h.All(tc.name)
  230. if !ok {
  231. t.Errorf("%d. h.All(%q): got false, want true", i, tc.name)
  232. }
  233. if want := []string{tc.value}; !reflect.DeepEqual(got, want) {
  234. t.Errorf("%d. h.All(%q): got %v, want %v", i, tc.name, got, want)
  235. }
  236. if got, want := h.Get(tc.name), tc.value; got != want {
  237. t.Errorf("%d. h.Get(%q): got %q, want %q", i, tc.name, got, want)
  238. }
  239. h.Del(tc.name)
  240. }
  241. if got, want := res.Header.Get("Test-Header"), ""; got != want {
  242. t.Errorf("res.Header.Get(%q): got %q, want %q", "Test-Header", got, want)
  243. }
  244. if got, want := res.ContentLength, int64(-1); got != want {
  245. t.Errorf("res.ContentLength: got %d, want %d", got, want)
  246. }
  247. if got := res.TransferEncoding; got != nil {
  248. t.Errorf("res.TransferEncoding: got %v, want nil", got)
  249. }
  250. for i, tc := range tt {
  251. if got, want := h.Get(tc.name), ""; got != want {
  252. t.Errorf("%d. h.Get(%q): got %q, want %q", i, tc.name, got, want)
  253. }
  254. got, ok := h.All(tc.name)
  255. if ok {
  256. t.Errorf("%d. h.All(%q): got ok, want !ok", i, tc.name)
  257. }
  258. if got != nil {
  259. t.Errorf("%d. h.All(%q): got %v, want nil", i, tc.name, got)
  260. }
  261. }
  262. }
  263. func TestResponseHeaderAdd(t *testing.T) {
  264. res := NewResponse(200, nil, nil)
  265. h := ResponseHeader(res)
  266. tt := []struct {
  267. name string
  268. values []string
  269. errOnSecondValue bool
  270. }{
  271. {
  272. name: "Test-Header",
  273. values: []string{"first", "second"},
  274. },
  275. {
  276. name: "Content-Length",
  277. values: []string{"100", "101"},
  278. errOnSecondValue: true,
  279. },
  280. {
  281. name: "Transfer-Encoding",
  282. values: []string{"chunked", "gzip"},
  283. },
  284. }
  285. for i, tc := range tt {
  286. if err := h.Add(tc.name, tc.values[0]); err != nil {
  287. t.Errorf("%d. h.Add(%q, %q): got %v, want no error", i, tc.name, tc.values[0], err)
  288. }
  289. if err := h.Add(tc.name, tc.values[1]); err != nil && !tc.errOnSecondValue {
  290. t.Errorf("%d. h.Add(%q, %q): got %v, want no error", i, tc.name, tc.values[1], err)
  291. }
  292. }
  293. if got, want := res.Header["Test-Header"], []string{"first", "second"}; !reflect.DeepEqual(got, want) {
  294. t.Errorf("res.Header[%q]: got %v, want %v", "Test-Header", got, want)
  295. }
  296. if got, want := res.ContentLength, int64(100); got != want {
  297. t.Errorf("res.ContentLength: got %d, want %d", got, want)
  298. }
  299. if got, want := res.TransferEncoding, []string{"chunked", "gzip"}; !reflect.DeepEqual(got, want) {
  300. t.Errorf("res.TransferEncoding: got %v, want %v", got, want)
  301. }
  302. }