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.
 
 
 

103 lines
2.7 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/parse"
  19. "github.com/google/martian/proxyutil"
  20. )
  21. func TestCopyModifier(t *testing.T) {
  22. m := NewCopyModifier("Original", "Copy")
  23. req, err := http.NewRequest("GET", "http://example.com", nil)
  24. if err != nil {
  25. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  26. }
  27. req.Header.Set("Original", "test")
  28. if err := m.ModifyRequest(req); err != nil {
  29. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  30. }
  31. if got, want := req.Header.Get("Copy"), "test"; got != want {
  32. t.Errorf("req.Header.Get(%q): got %q, want %q", "Copy", got, want)
  33. }
  34. res := proxyutil.NewResponse(200, nil, req)
  35. res.Header.Set("Original", "test")
  36. if err := m.ModifyResponse(res); err != nil {
  37. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  38. }
  39. if got, want := res.Header.Get("Copy"), "test"; got != want {
  40. t.Errorf("res.Header.Get(%q): got %q, want %q", "Copy", got, want)
  41. }
  42. }
  43. func TestCopyModifierFromJSON(t *testing.T) {
  44. msg := []byte(`{
  45. "header.Copy": {
  46. "from": "Original",
  47. "to": "Copy",
  48. "scope": ["request", "response"]
  49. }
  50. }`)
  51. r, err := parse.FromJSON(msg)
  52. if err != nil {
  53. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  54. }
  55. req, err := http.NewRequest("GET", "http://example.com", nil)
  56. if err != nil {
  57. t.Fatalf("http.NewRequest(): got %q, want no error", err)
  58. }
  59. req.Header.Set("Original", "test")
  60. reqmod := r.RequestModifier()
  61. if reqmod == nil {
  62. t.Fatal("reqmod: got nil, want not nil")
  63. }
  64. if err := reqmod.ModifyRequest(req); err != nil {
  65. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  66. }
  67. if got, want := req.Header.Get("Copy"), "test"; got != want {
  68. t.Errorf("req.Header.Get(%q): got %q, want %q", "Copy", got, want)
  69. }
  70. resmod := r.ResponseModifier()
  71. if resmod == nil {
  72. t.Fatal("resmod: got nil, want not nil")
  73. }
  74. res := proxyutil.NewResponse(200, nil, req)
  75. res.Header.Set("Original", "test")
  76. if err := resmod.ModifyResponse(res); err != nil {
  77. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  78. }
  79. if got, want := res.Header.Get("Copy"), "test"; got != want {
  80. t.Errorf("res.Header.Get(%q): got %q, want %q", "Copy", got, want)
  81. }
  82. }