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.
 
 
 

357 lines
8.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 martianurl
  15. import (
  16. "errors"
  17. "net/http"
  18. "net/url"
  19. "testing"
  20. "github.com/google/martian/martiantest"
  21. "github.com/google/martian/parse"
  22. "github.com/google/martian/proxyutil"
  23. "github.com/google/martian/verify"
  24. _ "github.com/google/martian/header"
  25. )
  26. func TestFilterModifyRequest(t *testing.T) {
  27. tt := []struct {
  28. want bool
  29. match string
  30. url *url.URL
  31. }{
  32. {
  33. match: "https://www.example.com",
  34. url: &url.URL{Scheme: "https"},
  35. want: true,
  36. },
  37. {
  38. match: "http://www.martian.local",
  39. url: &url.URL{Host: "*.martian.local"},
  40. want: true,
  41. },
  42. {
  43. match: "http://www.example.com/test",
  44. url: &url.URL{Path: "/test"},
  45. want: true,
  46. },
  47. {
  48. match: "http://www.example.com?test=true",
  49. url: &url.URL{RawQuery: "test=true"},
  50. want: true,
  51. },
  52. {
  53. match: "http://www.example.com#test",
  54. url: &url.URL{Fragment: "test"},
  55. want: true,
  56. },
  57. {
  58. match: "https://martian.local/test?test=true#test",
  59. url: &url.URL{
  60. Scheme: "https",
  61. Host: "martian.local",
  62. Path: "/test",
  63. RawQuery: "test=true",
  64. Fragment: "test",
  65. },
  66. want: true,
  67. },
  68. {
  69. match: "https://www.example.com",
  70. url: &url.URL{Scheme: "http"},
  71. want: false,
  72. },
  73. {
  74. match: "http://www.martian.external",
  75. url: &url.URL{Host: "www.martian.local"},
  76. want: false,
  77. },
  78. {
  79. match: "http://www.example.com/testing",
  80. url: &url.URL{Path: "/test"},
  81. want: false,
  82. },
  83. {
  84. match: "http://www.example.com?test=false",
  85. url: &url.URL{RawQuery: "test=true"},
  86. want: false,
  87. },
  88. {
  89. match: "http://www.example.com#test",
  90. url: &url.URL{Fragment: "testing"},
  91. want: false,
  92. },
  93. }
  94. for i, tc := range tt {
  95. req, err := http.NewRequest("GET", tc.match, nil)
  96. if err != nil {
  97. t.Fatalf("%d. NewRequest(): got %v, want no error", i, err)
  98. }
  99. mod := NewFilter(tc.url)
  100. tm := martiantest.NewModifier()
  101. mod.SetRequestModifier(tm)
  102. if err := mod.ModifyRequest(req); err != nil {
  103. t.Fatalf("%d. ModifyRequest(): got %q, want no error", i, err)
  104. }
  105. if tm.RequestModified() != tc.want {
  106. t.Errorf("%d. tm.RequestModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
  107. }
  108. }
  109. }
  110. func TestFilterModifyResponse(t *testing.T) {
  111. tt := []struct {
  112. want bool
  113. match string
  114. url *url.URL
  115. }{
  116. {
  117. match: "https://www.example.com",
  118. url: &url.URL{Scheme: "https"},
  119. want: true,
  120. },
  121. {
  122. match: "http://www.martian.local",
  123. url: &url.URL{Host: "www.martian.local"},
  124. want: true,
  125. },
  126. {
  127. match: "http://www.example.com/test",
  128. url: &url.URL{Path: "/test"},
  129. want: true,
  130. },
  131. {
  132. match: "http://www.example.com?test=true",
  133. url: &url.URL{RawQuery: "test=true"},
  134. want: true,
  135. },
  136. {
  137. match: "http://www.example.com#test",
  138. url: &url.URL{Fragment: "test"},
  139. want: true,
  140. },
  141. {
  142. match: "https://martian.local/test?test=true#test",
  143. url: &url.URL{
  144. Scheme: "https",
  145. Host: "martian.local",
  146. Path: "/test",
  147. RawQuery: "test=true",
  148. Fragment: "test",
  149. },
  150. want: true,
  151. },
  152. {
  153. match: "https://www.example.com",
  154. url: &url.URL{Scheme: "http"},
  155. want: false,
  156. },
  157. {
  158. match: "http://www.martian.external",
  159. url: &url.URL{Host: "www.martian.local"},
  160. want: false,
  161. },
  162. {
  163. match: "http://www.example.com/testing",
  164. url: &url.URL{Path: "/test"},
  165. want: false,
  166. },
  167. {
  168. match: "http://www.example.com?test=false",
  169. url: &url.URL{RawQuery: "test=true"},
  170. want: false,
  171. },
  172. {
  173. match: "http://www.example.com#test",
  174. url: &url.URL{Fragment: "testing"},
  175. want: false,
  176. },
  177. }
  178. for i, tc := range tt {
  179. req, err := http.NewRequest("GET", tc.match, nil)
  180. if err != nil {
  181. t.Fatalf("%d. NewRequest(): got %v, want no error", i, err)
  182. }
  183. res := proxyutil.NewResponse(200, nil, req)
  184. mod := NewFilter(tc.url)
  185. tm := martiantest.NewModifier()
  186. mod.SetResponseModifier(tm)
  187. if err := mod.ModifyResponse(res); err != nil {
  188. t.Fatalf("%d. ModifyResponse(): got %q, want no error", i, err)
  189. }
  190. if tm.ResponseModified() != tc.want {
  191. t.Errorf("tm.ResponseModified(): got %t, want %t", tm.ResponseModified(), tc.want)
  192. }
  193. }
  194. }
  195. func TestFilterFromJSON(t *testing.T) {
  196. msg := []byte(`{
  197. "url.Filter": {
  198. "scope": ["request", "response"],
  199. "scheme": "https",
  200. "modifier": {
  201. "header.Modifier": {
  202. "scope": ["request", "response"],
  203. "name": "Mod-Run",
  204. "value": "true"
  205. }
  206. },
  207. "else": {
  208. "header.Modifier": {
  209. "scope": ["request", "response"],
  210. "name": "Else-Run",
  211. "value": "true"
  212. }
  213. }
  214. }
  215. }`)
  216. r, err := parse.FromJSON(msg)
  217. if err != nil {
  218. t.Fatalf("FilterFromJSON(): got %v, want no error", err)
  219. }
  220. reqmod := r.RequestModifier()
  221. if reqmod == nil {
  222. t.Fatal("reqmod: got nil, want not nil")
  223. }
  224. req, err := http.NewRequest("GET", "https://martian.test", nil)
  225. if err != nil {
  226. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  227. }
  228. if err := reqmod.ModifyRequest(req); err != nil {
  229. t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
  230. }
  231. if got, want := req.Header.Get("Mod-Run"), "true"; got != want {
  232. t.Errorf("req.Header.Get(%q): got %q, want %q", "Mod-Run", got, want)
  233. }
  234. resmod := r.ResponseModifier()
  235. if resmod == nil {
  236. t.Fatalf("resmod: got nil, want not nil")
  237. }
  238. res := proxyutil.NewResponse(200, nil, req)
  239. if err := resmod.ModifyResponse(res); err != nil {
  240. t.Fatalf("resmod.ModifyResponse(): got %v, want no error", err)
  241. }
  242. if got, want := res.Header.Get("Mod-Run"), "true"; got != want {
  243. t.Errorf("res.Header.Get(%q): got %q, want %q", "Mod-Run", got, want)
  244. }
  245. // test else conditional modifier with scheme of http
  246. req, err = http.NewRequest("GET", "http://martian.test", nil)
  247. if err != nil {
  248. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  249. }
  250. if err := reqmod.ModifyRequest(req); err != nil {
  251. t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
  252. }
  253. if got, want := req.Header.Get("Mod-Run"), ""; got != want {
  254. t.Errorf("req.Header.Get(%q): got %q, want %q", "Mod-Run", got, want)
  255. }
  256. if got, want := req.Header.Get("Else-Run"), "true"; got != want {
  257. t.Errorf("req.Header.Get(%q): got %q, want %q", "Mod-Run", got, want)
  258. }
  259. }
  260. func TestPassThroughVerifyRequests(t *testing.T) {
  261. u := &url.URL{Host: "www.martian.local"}
  262. f := NewFilter(u)
  263. if err := f.VerifyRequests(); err != nil {
  264. t.Fatalf("VerifyRequest(): got %v, want no error", err)
  265. }
  266. tv := &verify.TestVerifier{
  267. RequestError: errors.New("verify request failure"),
  268. }
  269. f.SetRequestModifier(tv)
  270. if got, want := f.VerifyRequests().Error(), "verify request failure"; got != want {
  271. t.Fatalf("VerifyRequests(): got %s, want %s", got, want)
  272. }
  273. }
  274. func TestPassThroughVerifyResponses(t *testing.T) {
  275. u := &url.URL{Host: "www.martian.local"}
  276. f := NewFilter(u)
  277. if err := f.VerifyResponses(); err != nil {
  278. t.Fatalf("VerifyResponses(): got %v, want no error", err)
  279. }
  280. tv := &verify.TestVerifier{
  281. ResponseError: errors.New("verify response failure"),
  282. }
  283. f.SetResponseModifier(tv)
  284. if got, want := f.VerifyResponses().Error(), "verify response failure"; got != want {
  285. t.Fatalf("VerifyResponses(): got %s, want %s", got, want)
  286. }
  287. }
  288. func TestResets(t *testing.T) {
  289. u := &url.URL{Host: "www.martian.local"}
  290. f := NewFilter(u)
  291. tv := &verify.TestVerifier{
  292. ResponseError: errors.New("verify response failure"),
  293. }
  294. f.SetResponseModifier(tv)
  295. tv = &verify.TestVerifier{
  296. RequestError: errors.New("verify request failure"),
  297. }
  298. f.SetRequestModifier(tv)
  299. if err := f.VerifyRequests(); err == nil {
  300. t.Fatal("VerifyRequests(): got nil, want error")
  301. }
  302. if err := f.VerifyResponses(); err == nil {
  303. t.Fatal("VerifyResponses(): got nil, want error")
  304. }
  305. f.ResetRequestVerifications()
  306. f.ResetResponseVerifications()
  307. if err := f.VerifyRequests(); err != nil {
  308. t.Errorf("VerifyRequests(): got %v, want no error", err)
  309. }
  310. if err := f.VerifyResponses(); err != nil {
  311. t.Errorf("VerifyResponses(): got %v, want no error", err)
  312. }
  313. }