25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

240 lines
5.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 martianurl
  15. import (
  16. "net/http"
  17. "regexp"
  18. "testing"
  19. "github.com/google/martian"
  20. _ "github.com/google/martian/header"
  21. "github.com/google/martian/parse"
  22. "github.com/google/martian/proxyutil"
  23. )
  24. func TestRegexFilterModifyRequest(t *testing.T) {
  25. tt := []struct {
  26. want bool
  27. match string
  28. regstr string
  29. }{
  30. {
  31. match: "https://www.example.com",
  32. regstr: "https://.*",
  33. want: true,
  34. },
  35. {
  36. match: "http://www.example.com/subpath",
  37. regstr: ".*www.example.com.*",
  38. want: true,
  39. },
  40. {
  41. match: "https://www.example.com/subpath",
  42. regstr: ".*www.example.com.*",
  43. want: true,
  44. },
  45. {
  46. match: "http://www.example.com/test",
  47. regstr: ".*/test",
  48. want: true,
  49. },
  50. {
  51. match: "http://www.example.com?test=true",
  52. regstr: ".*test=true.*",
  53. want: true,
  54. },
  55. {
  56. match: "http://www.example.com#test",
  57. regstr: ".*test.*",
  58. want: true,
  59. },
  60. {
  61. match: "https://martian.local/test?test=true#test",
  62. regstr: "https://martian.local/test\\?test=true#test",
  63. want: true,
  64. },
  65. {
  66. match: "http://www.youtube.com/get_tags?tagone=yes",
  67. regstr: ".*www.youtube.com/get_tags\\?.*",
  68. want: true,
  69. },
  70. {
  71. match: "https://www.example.com",
  72. regstr: "http://.*",
  73. want: false,
  74. },
  75. {
  76. match: "http://www.martian.external",
  77. regstr: ".*www.martian.local.*",
  78. want: false,
  79. },
  80. {
  81. match: "http://www.example.com/testing",
  82. regstr: ".*/test$",
  83. want: false,
  84. },
  85. {
  86. match: "http://www.example.com?test=false",
  87. regstr: ".*test=true.*",
  88. want: false,
  89. },
  90. {
  91. match: "http://www.example.com#test",
  92. regstr: ".*#testing.*",
  93. want: false,
  94. },
  95. {
  96. match: "https://martian.local/test?test=true#test",
  97. // "\\\\" was the old way of adding a backslash in SAVR
  98. regstr: "https://martian.local/test\\\\?test=true#test",
  99. want: false,
  100. },
  101. {
  102. match: "http://www.youtube.com/get_tags/nope",
  103. regstr: ".*www.youtube.com/get_ad_tags\\?.*",
  104. want: false,
  105. },
  106. }
  107. for i, tc := range tt {
  108. req, err := http.NewRequest("GET", tc.match, nil)
  109. if err != nil {
  110. t.Fatalf("%d. NewRequest(): got %v, want no error", i, err)
  111. }
  112. regex, err := regexp.Compile(tc.regstr)
  113. if err != nil {
  114. t.Fatalf("%d. regexp.Compile(): got %v, want no error", i, err)
  115. }
  116. var modRun bool
  117. mod := NewRegexFilter(regex)
  118. mod.SetRequestModifier(martian.RequestModifierFunc(
  119. func(*http.Request) error {
  120. modRun = true
  121. return nil
  122. }))
  123. if err := mod.ModifyRequest(req); err != nil {
  124. t.Fatalf("%d. ModifyRequest(): got %q, want no error", i, err)
  125. }
  126. if modRun != tc.want {
  127. t.Errorf("%d. modRun: got %t, want %t", i, modRun, tc.want)
  128. }
  129. }
  130. }
  131. // The matching functionality is already tested above, so this just tests response setting.
  132. func TestRegexFilterModifyResponse(t *testing.T) {
  133. tt := []struct {
  134. want bool
  135. match string
  136. regstr string
  137. }{
  138. {
  139. match: "https://www.example.com",
  140. regstr: ".*www.example.com.*",
  141. want: true,
  142. },
  143. {
  144. match: "http://www.martian.external",
  145. regstr: ".*www.martian.local.*",
  146. want: false,
  147. },
  148. }
  149. for i, tc := range tt {
  150. req, err := http.NewRequest("GET", tc.match, nil)
  151. if err != nil {
  152. t.Fatalf("%d. NewRequest(): got %v, want no error", i, err)
  153. }
  154. res := proxyutil.NewResponse(200, nil, req)
  155. regex, err := regexp.Compile(tc.regstr)
  156. if err != nil {
  157. t.Fatalf("%d. regexp.Compile(): got %v, want no error", i, err)
  158. }
  159. var modRun bool
  160. mod := NewRegexFilter(regex)
  161. mod.SetResponseModifier(martian.ResponseModifierFunc(
  162. func(*http.Response) error {
  163. modRun = true
  164. return nil
  165. }))
  166. if err := mod.ModifyResponse(res); err != nil {
  167. t.Fatalf("%d. ModifyResponse(): got %q, want no error", i, err)
  168. }
  169. if modRun != tc.want {
  170. t.Errorf("%d. modRun: got %t, want %t", i, modRun, tc.want)
  171. }
  172. }
  173. }
  174. func TestRegexFilterFromJSON(t *testing.T) {
  175. rawMsg := `
  176. {
  177. "url.RegexFilter": {
  178. "scope": ["request", "response"],
  179. "regex": ".*martian.test.*",
  180. "modifier": {
  181. "header.Modifier": {
  182. "name": "Martian-Test",
  183. "value": "true",
  184. "scope": ["request", "response"]
  185. }
  186. }
  187. }
  188. }`
  189. r, err := parse.FromJSON([]byte(rawMsg))
  190. if err != nil {
  191. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  192. }
  193. reqmod := r.RequestModifier()
  194. if reqmod == nil {
  195. t.Errorf("reqmod: got nil, want not nil")
  196. }
  197. req, err := http.NewRequest("GET", "https://martian.test", nil)
  198. if err != nil {
  199. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  200. }
  201. if err := reqmod.ModifyRequest(req); err != nil {
  202. t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
  203. }
  204. if got, want := req.Header.Get("Martian-Test"), "true"; got != want {
  205. t.Errorf("req.Header.Get(%q): got %q, want %q", "Martian-Test", got, want)
  206. }
  207. resmod := r.ResponseModifier()
  208. if resmod == nil {
  209. t.Fatalf("resmod: got nil, want not nil")
  210. }
  211. res := proxyutil.NewResponse(200, nil, req)
  212. if err := resmod.ModifyResponse(res); err != nil {
  213. t.Fatalf("resmod.ModifyResponse(): got %v, want no error", err)
  214. }
  215. if got, want := res.Header.Get("Martian-Test"), "true"; got != want {
  216. t.Errorf("res.Header.Get(%q): got %q, want %q", "Martian-Test", got, want)
  217. }
  218. }