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.
 
 
 

86 lines
2.8 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. )
  19. func TestSetForwardHeaders(t *testing.T) {
  20. xfp := "X-Forwarded-Proto"
  21. xff := "X-Forwarded-For"
  22. xfh := "X-Forwarded-Host"
  23. xfu := "X-Forwarded-Url"
  24. m := NewForwardedModifier()
  25. req, err := http.NewRequest("GET", "http://martian.local?key=value", nil)
  26. if err != nil {
  27. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  28. }
  29. req.RemoteAddr = "10.0.0.1:8112"
  30. if m.ModifyRequest(req); err != nil {
  31. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  32. }
  33. if got, want := req.Header.Get(xfp), "http"; got != want {
  34. t.Errorf("req.Header.Get(%q): got %q, want %q", xfp, got, want)
  35. }
  36. if got, want := req.Header.Get(xff), "10.0.0.1"; got != want {
  37. t.Errorf("req.Header.Get(%q): got %q, want %q", xff, got, want)
  38. }
  39. if got, want := req.Header.Get(xfh), "martian.local"; got != want {
  40. t.Errorf("req.Header.Get(%q): got %q, want %q", xfh, got, want)
  41. }
  42. if got, want := req.Header.Get(xfu), "http://martian.local?key=value"; got != want {
  43. t.Errorf("req.Header.Get(%q): got %q, want %q", xfh, got, want)
  44. }
  45. // Test with existing X-Forwarded-For.
  46. req.RemoteAddr = "12.12.12.12"
  47. if m.ModifyRequest(req); err != nil {
  48. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  49. }
  50. if got, want := req.Header.Get(xff), "10.0.0.1, 12.12.12.12"; got != want {
  51. t.Errorf("req.Header.Get(%q): got %q, want %q", xff, got, want)
  52. }
  53. // Test that proto, host, and URL headers are preserved if already present.
  54. req, err = http.NewRequest("GET", "http://example.com/path?k=v", nil)
  55. if err != nil {
  56. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  57. }
  58. req.Header.Set(xfp, "https")
  59. req.Header.Set(xfh, "preserved.host.com")
  60. req.Header.Set(xfu, "https://preserved.host.com/foo?x=y")
  61. if m.ModifyRequest(req); err != nil {
  62. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  63. }
  64. if got, want := req.Header.Get(xfp), "https"; got != want {
  65. t.Errorf("req.Header.Get(%q): got %q, want %q", xfh, got, want)
  66. }
  67. if got, want := req.Header.Get(xfh), "preserved.host.com"; got != want {
  68. t.Errorf("req.Header.Get(%q): got %q, want %q", xfh, got, want)
  69. }
  70. if got, want := req.Header.Get(xfu), "https://preserved.host.com/foo?x=y"; got != want {
  71. t.Errorf("req.Header.Get(%q): got %q, want %q", xfh, got, want)
  72. }
  73. }