您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

332 行
7.3 KiB

  1. // Copyright 2017 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 cookie
  15. import (
  16. "net/http"
  17. "testing"
  18. "github.com/google/martian/filter"
  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 TestFilterFromJSON(t *testing.T) {
  25. msg := []byte(`{
  26. "cookie.Filter": {
  27. "scope": ["request", "response"],
  28. "name": "martian-cookie",
  29. "value": "true",
  30. "modifier": {
  31. "header.Modifier" : {
  32. "scope": ["request", "response"],
  33. "name": "Martian-Testing",
  34. "value": "true"
  35. }
  36. },
  37. "else": {
  38. "header.Modifier" : {
  39. "scope": ["request", "response"],
  40. "name": "Martian-Testing",
  41. "value": "false"
  42. }
  43. }
  44. }
  45. }`)
  46. r, err := parse.FromJSON(msg)
  47. if err != nil {
  48. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  49. }
  50. reqmod := r.RequestModifier()
  51. if reqmod == nil {
  52. t.Fatal("reqmod: got nil, want not nil")
  53. }
  54. resmod := r.ResponseModifier()
  55. if resmod == nil {
  56. t.Fatal("resmod: got nil, want not nil")
  57. }
  58. for _, tc := range []struct {
  59. name string
  60. wantMatch bool
  61. cookie *http.Cookie
  62. }{
  63. {
  64. name: "matching name and value",
  65. wantMatch: true,
  66. cookie: &http.Cookie{
  67. Name: "martian-cookie",
  68. Value: "true",
  69. },
  70. },
  71. {
  72. name: "matching name with mismatched value",
  73. wantMatch: false,
  74. cookie: &http.Cookie{
  75. Name: "martian-cookie",
  76. Value: "false",
  77. },
  78. },
  79. {
  80. name: "missing cookie",
  81. wantMatch: false,
  82. },
  83. } {
  84. req, err := http.NewRequest("GET", "http://example.com", nil)
  85. if err != nil {
  86. t.Errorf("%s: http.NewRequest(): got %v, want no error", tc.name, err)
  87. continue
  88. }
  89. if tc.cookie != nil {
  90. req.AddCookie(tc.cookie)
  91. }
  92. if err := reqmod.ModifyRequest(req); err != nil {
  93. t.Errorf("%s: ModifyRequest(): got %v, want no error", tc.name, err)
  94. continue
  95. }
  96. want := "false"
  97. if tc.wantMatch {
  98. want = "true"
  99. }
  100. if got := req.Header.Get("Martian-Testing"); got != want {
  101. t.Errorf("%s: req.Header.Get(%q): got %q, want %q", "Martian-Testing", tc.name, got, want)
  102. continue
  103. }
  104. res := proxyutil.NewResponse(200, nil, req)
  105. if tc.cookie != nil {
  106. c := &http.Cookie{Name: tc.cookie.Name, Value: tc.cookie.Value}
  107. res.Header.Add("Set-Cookie", c.String())
  108. }
  109. if err := resmod.ModifyResponse(res); err != nil {
  110. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  111. }
  112. if got := res.Header.Get("Martian-Testing"); got != want {
  113. t.Fatalf("res.Header.Get(%q): got %q, want %q", "Martian-Testing", got, want)
  114. }
  115. }
  116. }
  117. func TestFilterFromJSONWithoutElse(t *testing.T) {
  118. msg := []byte(`{
  119. "cookie.Filter": {
  120. "scope": ["request", "response"],
  121. "name": "martian-cookie",
  122. "value": "true",
  123. "modifier": {
  124. "header.Modifier" : {
  125. "scope": ["request", "response"],
  126. "name": "Martian-Testing",
  127. "value": "true"
  128. }
  129. }
  130. }
  131. }`)
  132. _, err := parse.FromJSON(msg)
  133. if err != nil {
  134. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  135. }
  136. }
  137. func TestRequestWhenTrueCondition(t *testing.T) {
  138. cm := NewMatcher(&http.Cookie{Name: "Martian-Testing", Value: "true"})
  139. tt := []struct {
  140. name string
  141. value string
  142. want bool
  143. }{
  144. {
  145. name: "Martian-Production",
  146. value: "true",
  147. want: false,
  148. },
  149. {
  150. name: "Martian-Testing",
  151. value: "true",
  152. want: true,
  153. },
  154. }
  155. for i, tc := range tt {
  156. tm := martiantest.NewModifier()
  157. f := filter.New()
  158. f.SetRequestCondition(cm)
  159. f.RequestWhenTrue(tm)
  160. req, err := http.NewRequest("GET", "/", nil)
  161. if err != nil {
  162. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  163. }
  164. req.AddCookie(&http.Cookie{Name: tc.name, Value: tc.value})
  165. if err := f.ModifyRequest(req); err != nil {
  166. t.Fatalf("%d. ModifyRequest(): got %v, want no error", i, err)
  167. }
  168. if tm.RequestModified() != tc.want {
  169. t.Errorf("%d. tm.RequestModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
  170. }
  171. }
  172. }
  173. func TestRequestWhenFalse(t *testing.T) {
  174. cm := NewMatcher(&http.Cookie{Name: "Martian-Testing", Value: "true"})
  175. tt := []struct {
  176. name string
  177. value string
  178. want bool
  179. }{
  180. {
  181. name: "Martian-Production",
  182. value: "true",
  183. want: true,
  184. },
  185. {
  186. name: "Martian-Testing",
  187. value: "true",
  188. want: false,
  189. },
  190. }
  191. for i, tc := range tt {
  192. tm := martiantest.NewModifier()
  193. f := filter.New()
  194. f.SetRequestCondition(cm)
  195. f.RequestWhenFalse(tm)
  196. req, err := http.NewRequest("GET", "/", nil)
  197. if err != nil {
  198. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  199. }
  200. req.AddCookie(&http.Cookie{Name: tc.name, Value: tc.value})
  201. if err := f.ModifyRequest(req); err != nil {
  202. t.Fatalf("%d. ModifyRequest(): got %v, want no error", i, err)
  203. }
  204. if tm.RequestModified() != tc.want {
  205. t.Errorf("%d. tm.RequestModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
  206. }
  207. }
  208. }
  209. func TestResponseWhenTrue(t *testing.T) {
  210. cm := NewMatcher(&http.Cookie{Name: "Martian-Testing", Value: "true"})
  211. tt := []struct {
  212. name string
  213. value string
  214. want bool
  215. }{
  216. {
  217. name: "Martian-Production",
  218. value: "true",
  219. want: false,
  220. },
  221. {
  222. name: "Martian-Testing",
  223. value: "true",
  224. want: true,
  225. },
  226. }
  227. for i, tc := range tt {
  228. tm := martiantest.NewModifier()
  229. f := filter.New()
  230. f.SetResponseCondition(cm)
  231. f.ResponseWhenTrue(tm)
  232. req, err := http.NewRequest("GET", "/", nil)
  233. if err != nil {
  234. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  235. }
  236. res := proxyutil.NewResponse(200, nil, req)
  237. c := &http.Cookie{Name: tc.name, Value: tc.value}
  238. res.Header.Add("Set-Cookie", c.String())
  239. if err := f.ModifyResponse(res); err != nil {
  240. t.Fatalf("%d. ModifyResponse(): got %v, want no error", i, err)
  241. }
  242. if tm.ResponseModified() != tc.want {
  243. t.Errorf("%d. tm.ResponseModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
  244. }
  245. }
  246. }
  247. func TestResponseWhenFalse(t *testing.T) {
  248. cm := NewMatcher(&http.Cookie{Name: "Martian-Testing", Value: "true"})
  249. tt := []struct {
  250. name string
  251. value string
  252. want bool
  253. }{
  254. {
  255. name: "Martian-Production",
  256. value: "true",
  257. want: true,
  258. },
  259. {
  260. name: "Martian-Testing",
  261. value: "true",
  262. want: false,
  263. },
  264. }
  265. for i, tc := range tt {
  266. tm := martiantest.NewModifier()
  267. f := filter.New()
  268. f.SetResponseCondition(cm)
  269. f.ResponseWhenFalse(tm)
  270. req, err := http.NewRequest("GET", "/", nil)
  271. if err != nil {
  272. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  273. }
  274. res := proxyutil.NewResponse(200, nil, req)
  275. c := &http.Cookie{Name: tc.name, Value: tc.value}
  276. res.Header.Add("Set-Cookie", c.String())
  277. if err := f.ModifyResponse(res); err != nil {
  278. t.Fatalf("%d. ModifyResponse(): got %v, want no error", i, err)
  279. }
  280. if tm.ResponseModified() != tc.want {
  281. t.Errorf("%d. tm.ResponseModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
  282. }
  283. }
  284. }