Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

89 řádky
2.6 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. "fmt"
  17. "io"
  18. "net/http"
  19. "strings"
  20. "testing"
  21. )
  22. func TestNewResponse(t *testing.T) {
  23. req, err := http.NewRequest("GET", "http://www.example.com", nil)
  24. if err != nil {
  25. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  26. }
  27. req.Close = true
  28. res := NewResponse(200, nil, req)
  29. if got, want := res.StatusCode, 200; got != want {
  30. t.Errorf("res.StatusCode: got %d, want %d", got, want)
  31. }
  32. if got, want := res.Status, "200 OK"; got != want {
  33. t.Errorf("res.Status: got %q, want %q", got, want)
  34. }
  35. if !res.Close {
  36. t.Error("res.Close: got false, want true")
  37. }
  38. if got, want := res.Proto, "HTTP/1.1"; got != want {
  39. t.Errorf("res.Proto: got %q, want %q", got, want)
  40. }
  41. if got, want := res.ProtoMajor, 1; got != want {
  42. t.Errorf("res.ProtoMajor: got %d, want %d", got, want)
  43. }
  44. if got, want := res.ProtoMinor, 1; got != want {
  45. t.Errorf("res.ProtoMinor: got %d, want %d", got, want)
  46. }
  47. if res.Header == nil {
  48. t.Error("res.Header: got nil, want header")
  49. }
  50. if _, ok := res.Body.(io.ReadCloser); !ok {
  51. t.Error("res.Body.(io.ReadCloser): got !ok, want ok")
  52. }
  53. if got, want := res.Request, req; got != want {
  54. t.Errorf("res.Request: got %v, want %v", got, want)
  55. }
  56. }
  57. func TestWarning(t *testing.T) {
  58. hdr := http.Header{}
  59. err := fmt.Errorf("modifier error")
  60. Warning(hdr, err)
  61. if got, want := len(hdr["Warning"]), 1; got != want {
  62. t.Fatalf("len(hdr[%q]): got %d, want %d", "Warning", got, want)
  63. }
  64. want := `199 "martian" "modifier error"`
  65. if got := hdr["Warning"][0]; !strings.HasPrefix(got, want) {
  66. t.Errorf("hdr[%q][0]: got %q, want to have prefix %q", "Warning", got, want)
  67. }
  68. hdr.Set("Date", "Mon, 02 Jan 2006 15:04:05 GMT")
  69. Warning(hdr, err)
  70. if got, want := len(hdr["Warning"]), 2; got != want {
  71. t.Fatalf("len(hdr[%q]): got %d, want %d", "Warning", got, want)
  72. }
  73. want = `199 "martian" "modifier error" "Mon, 02 Jan 2006 15:04:05 GMT"`
  74. if got := hdr["Warning"][1]; got != want {
  75. t.Errorf("hdr[%q][1]: got %q, want %q", "Warning", got, want)
  76. }
  77. }