Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

148 wiersze
3.5 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 port
  15. import (
  16. "net/http"
  17. "net/url"
  18. "testing"
  19. _ "github.com/google/martian/header"
  20. "github.com/google/martian/martiantest"
  21. "github.com/google/martian/parse"
  22. "github.com/google/martian/proxyutil"
  23. )
  24. func TestFilterModifyRequest(t *testing.T) {
  25. tt := []struct {
  26. want bool
  27. url *url.URL
  28. port int
  29. }{
  30. {
  31. url: &url.URL{Scheme: "http", Host: "example.com"},
  32. port: 80,
  33. want: true,
  34. },
  35. {
  36. url: &url.URL{Scheme: "http", Host: "example.com:80"},
  37. port: 80,
  38. want: true,
  39. },
  40. {
  41. url: &url.URL{Scheme: "http", Host: "example.com"},
  42. port: 123,
  43. want: false,
  44. },
  45. {
  46. url: &url.URL{Scheme: "http", Host: "example.com:8080"},
  47. port: 123,
  48. want: false,
  49. },
  50. {
  51. url: &url.URL{Scheme: "https", Host: "example.com"},
  52. port: 443,
  53. want: true,
  54. },
  55. {
  56. url: &url.URL{Scheme: "https", Host: "example.com:443"},
  57. port: 443,
  58. want: true,
  59. },
  60. {
  61. url: &url.URL{Scheme: "https", Host: "example.com"},
  62. port: 123,
  63. want: false,
  64. },
  65. {
  66. url: &url.URL{Scheme: "https", Host: "example.com:8080"},
  67. port: 123,
  68. want: false,
  69. },
  70. }
  71. for i, tc := range tt {
  72. req, err := http.NewRequest("GET", tc.url.String(), nil)
  73. if err != nil {
  74. t.Fatalf("%d. NewRequest(): got %v, want no error", i, err)
  75. }
  76. mod := NewFilter(tc.port)
  77. tm := martiantest.NewModifier()
  78. mod.SetRequestModifier(tm)
  79. if err := mod.ModifyRequest(req); err != nil {
  80. t.Fatalf("%d. ModifyRequest(): got %q, want no error", i, err)
  81. }
  82. if tm.RequestModified() != tc.want {
  83. t.Errorf("%d. tm.RequestModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
  84. }
  85. }
  86. }
  87. func TestFilterFromJSON(t *testing.T) {
  88. msg := []byte(`{
  89. "port.Filter": {
  90. "scope": ["request", "response"],
  91. "port": 8080,
  92. "modifier": {
  93. "header.Modifier": {
  94. "scope": ["request", "response"],
  95. "name": "Mod-Run",
  96. "value": "true"
  97. }
  98. }
  99. }
  100. }`)
  101. r, err := parse.FromJSON(msg)
  102. if err != nil {
  103. t.Fatalf("FilterFromJSON(): got %v, want no error", err)
  104. }
  105. reqmod := r.RequestModifier()
  106. if reqmod == nil {
  107. t.Fatal("reqmod: got nil, want not nil")
  108. }
  109. req, err := http.NewRequest("GET", "https://example.com:8080", nil)
  110. if err != nil {
  111. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  112. }
  113. if err := reqmod.ModifyRequest(req); err != nil {
  114. t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
  115. }
  116. if got, want := req.Header.Get("Mod-Run"), "true"; got != want {
  117. t.Errorf("req.Header.Get(%q): got %q, want %q", "Mod-Run", got, want)
  118. }
  119. resmod := r.ResponseModifier()
  120. if resmod == nil {
  121. t.Fatalf("resmod: got nil, want not nil")
  122. }
  123. res := proxyutil.NewResponse(200, nil, req)
  124. if err := resmod.ModifyResponse(res); err != nil {
  125. t.Fatalf("resmod.ModifyResponse(): got %v, want no error", err)
  126. }
  127. if got, want := res.Header.Get("Mod-Run"), "true"; got != want {
  128. t.Errorf("res.Header.Get(%q): got %q, want %q", "Mod-Run", got, want)
  129. }
  130. }