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.
 
 
 

80 rivejä
2.3 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 header
  15. import (
  16. "net/http"
  17. "testing"
  18. "github.com/google/martian/proxyutil"
  19. )
  20. func TestRemoveHopByHopHeaders(t *testing.T) {
  21. m := NewHopByHopModifier()
  22. req, err := http.NewRequest("GET", "/", nil)
  23. if err != nil {
  24. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  25. }
  26. req.Header = http.Header{
  27. // Additional hop-by-hop headers are listed in the
  28. // Connection header.
  29. "Connection": []string{
  30. "X-Connection",
  31. "X-Hop-By-Hop, close",
  32. },
  33. // RFC hop-by-hop headers.
  34. "Keep-Alive": []string{},
  35. "Proxy-Authenticate": []string{},
  36. "Proxy-Authorization": []string{},
  37. "Te": []string{},
  38. "Trailer": []string{},
  39. "Transfer-Encoding": []string{},
  40. "Upgrade": []string{},
  41. "Proxy-Connection": []string{},
  42. // Hop-by-hop headers listed in the Connection header.
  43. "X-Connection": []string{},
  44. "X-Hop-By-Hop": []string{},
  45. // End-to-end header that should not be removed.
  46. "X-End-To-End": []string{},
  47. }
  48. if err := m.ModifyRequest(req); err != nil {
  49. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  50. }
  51. if got, want := len(req.Header), 1; got != want {
  52. t.Fatalf("len(req.Header): got %d, want %d", got, want)
  53. }
  54. if _, ok := req.Header["X-End-To-End"]; !ok {
  55. t.Errorf("req.Header[%q]: got !ok, want ok", "X-End-To-End")
  56. }
  57. res := proxyutil.NewResponse(200, nil, req)
  58. res.Header = req.Header
  59. if err := m.ModifyResponse(res); err != nil {
  60. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  61. }
  62. if got, want := len(res.Header), 1; got != want {
  63. t.Fatalf("len(res.Header): got %d, want %d", got, want)
  64. }
  65. if _, ok := res.Header["X-End-To-End"]; !ok {
  66. t.Errorf("res.Header[%q]: got !ok, want ok", "X-End-To-End")
  67. }
  68. }