/http/header_test.go

http://github.com/petar/GoHTTP · Go · 81 lines · 74 code · 4 blank · 3 comment · 3 complexity · 64bc31cfa0119ebdfe7fe489b10cd01e MD5 · raw file

  1. // Copyright 2011 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. package http
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. var headerWriteTests = []struct {
  10. h Header
  11. exclude map[string]bool
  12. expected string
  13. }{
  14. {Header{}, nil, ""},
  15. {
  16. Header{
  17. "Content-Type": {"text/html; charset=UTF-8"},
  18. "Content-Length": {"0"},
  19. },
  20. nil,
  21. "Content-Length: 0\r\nContent-Type: text/html; charset=UTF-8\r\n",
  22. },
  23. {
  24. Header{
  25. "Content-Length": {"0", "1", "2"},
  26. },
  27. nil,
  28. "Content-Length: 0\r\nContent-Length: 1\r\nContent-Length: 2\r\n",
  29. },
  30. {
  31. Header{
  32. "Expires": {"-1"},
  33. "Content-Length": {"0"},
  34. "Content-Encoding": {"gzip"},
  35. },
  36. map[string]bool{"Content-Length": true},
  37. "Content-Encoding: gzip\r\nExpires: -1\r\n",
  38. },
  39. {
  40. Header{
  41. "Expires": {"-1"},
  42. "Content-Length": {"0", "1", "2"},
  43. "Content-Encoding": {"gzip"},
  44. },
  45. map[string]bool{"Content-Length": true},
  46. "Content-Encoding: gzip\r\nExpires: -1\r\n",
  47. },
  48. {
  49. Header{
  50. "Expires": {"-1"},
  51. "Content-Length": {"0"},
  52. "Content-Encoding": {"gzip"},
  53. },
  54. map[string]bool{"Content-Length": true, "Expires": true, "Content-Encoding": true},
  55. "",
  56. },
  57. {
  58. Header{
  59. "Nil": nil,
  60. "Empty": {},
  61. "Blank": {""},
  62. "Double-Blank": {"", ""},
  63. },
  64. nil,
  65. "Blank: \r\nDouble-Blank: \r\nDouble-Blank: \r\n",
  66. },
  67. }
  68. func TestHeaderWrite(t *testing.T) {
  69. var buf bytes.Buffer
  70. for i, test := range headerWriteTests {
  71. test.h.WriteSubset(&buf, test.exclude)
  72. if buf.String() != test.expected {
  73. t.Errorf("#%d:\n got: %q\nwant: %q", i, buf.String(), test.expected)
  74. }
  75. buf.Reset()
  76. }
  77. }