query_string_verifier_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 querystring
  15. import (
  16. "net/http"
  17. "testing"
  18. "github.com/google/martian"
  19. "github.com/google/martian/parse"
  20. "github.com/google/martian/verify"
  21. )
  22. func TestVerifyRequestPasses(t *testing.T) {
  23. v, err := NewVerifier("foo", "bar")
  24. if err != nil {
  25. t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "bar", err)
  26. }
  27. req, err := http.NewRequest("GET", "http://www.google.com?foo=baz&foo=bar", nil)
  28. if err != nil {
  29. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  30. }
  31. _, remove, err := martian.TestContext(req, nil, nil)
  32. if err != nil {
  33. t.Fatalf("TestContext(): got %v, want no error", err)
  34. }
  35. defer remove()
  36. if err := v.ModifyRequest(req); err != nil {
  37. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  38. }
  39. if err := v.VerifyRequests(); err != nil {
  40. t.Fatalf("VerifyRequests(): got %v, want no error", err)
  41. }
  42. }
  43. func TestVerifyEmptyValue(t *testing.T) {
  44. v, err := NewVerifier("foo", "")
  45. if err != nil {
  46. t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "", err)
  47. }
  48. req, err := http.NewRequest("GET", "http://www.google.com?foo=bar", nil)
  49. if err != nil {
  50. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  51. }
  52. _, remove, err := martian.TestContext(req, nil, nil)
  53. if err != nil {
  54. t.Fatalf("TestContext(): got %v, want no error", err)
  55. }
  56. defer remove()
  57. if err := v.ModifyRequest(req); err != nil {
  58. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  59. }
  60. if err := v.VerifyRequests(); err != nil {
  61. t.Fatalf("VerifyRequests(): got %v, want no error", err)
  62. }
  63. }
  64. func TestFailureWithMissingKey(t *testing.T) {
  65. v, err := NewVerifier("foo", "bar")
  66. if err != nil {
  67. t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "bar", err)
  68. }
  69. req, err := http.NewRequest("GET", "http://www.google.com?fizz=bar", nil)
  70. if err != nil {
  71. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  72. }
  73. _, remove, err := martian.TestContext(req, nil, nil)
  74. if err != nil {
  75. t.Fatalf("TestContext(): got %v, want no error", err)
  76. }
  77. defer remove()
  78. if err := v.ModifyRequest(req); err != nil {
  79. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  80. }
  81. merr, ok := v.VerifyRequests().(*martian.MultiError)
  82. if !ok {
  83. t.Fatal("VerifyRequests(): got nil, want *verify.MultiError")
  84. }
  85. errs := merr.Errors()
  86. if len(errs) != 1 {
  87. t.Fatalf("len(merr.Errors()): got %d, want 1", len(errs))
  88. }
  89. expectErr := "request(http://www.google.com?fizz=bar) param verification error: key foo not found"
  90. for i := range errs {
  91. if got, want := errs[i].Error(), expectErr; got != want {
  92. t.Errorf("%d. err.Error(): mismatched error output\ngot: %s\nwant: %s", i, got, want)
  93. }
  94. }
  95. }
  96. func TestFailureWithMultiFail(t *testing.T) {
  97. v, err := NewVerifier("foo", "bar")
  98. if err != nil {
  99. t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "bar", err)
  100. }
  101. req, err := http.NewRequest("GET", "http://www.google.com?foo=baz", nil)
  102. if err != nil {
  103. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  104. }
  105. _, remove, err := martian.TestContext(req, nil, nil)
  106. if err != nil {
  107. t.Fatalf("TestContext(): got %v, want no error", err)
  108. }
  109. defer remove()
  110. if err := v.ModifyRequest(req); err != nil {
  111. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  112. }
  113. if err := v.ModifyRequest(req); err != nil {
  114. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  115. }
  116. merr, ok := v.VerifyRequests().(*martian.MultiError)
  117. if !ok {
  118. t.Fatalf("VerifyRequests(): got nil, want *verify.MultiError")
  119. }
  120. errs := merr.Errors()
  121. if len(errs) != 2 {
  122. t.Fatalf("len(merr.Errors()): got %d, want 2", len(errs))
  123. }
  124. expectErr := "request(http://www.google.com?foo=baz) param verification error: got baz for key foo, want bar"
  125. for i := range errs {
  126. if got, want := errs[i].Error(), expectErr; got != want {
  127. t.Errorf("%d. err.Error(): mismatched error output\ngot: %s\nwant: %s", i,
  128. got, want)
  129. }
  130. }
  131. v.ResetRequestVerifications()
  132. if err := v.VerifyRequests(); err != nil {
  133. t.Fatalf("VerifyRequests(): got %v, want no error", err)
  134. }
  135. }
  136. func TestBadInputToConstructor(t *testing.T) {
  137. if _, err := NewVerifier("", "bar"); err == nil {
  138. t.Fatalf("NewVerifier(): no error returned for empty key")
  139. }
  140. }
  141. func TestVerifierFromJSON(t *testing.T) {
  142. msg := []byte(`{
  143. "querystring.Verifier": {
  144. "scope": ["request"],
  145. "name": "param",
  146. "value": "true"
  147. }
  148. }`)
  149. r, err := parse.FromJSON(msg)
  150. if err != nil {
  151. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  152. }
  153. reqmod := r.RequestModifier()
  154. if reqmod == nil {
  155. t.Fatal("reqmod: got nil, want not nil")
  156. }
  157. reqv, ok := reqmod.(verify.RequestVerifier)
  158. if !ok {
  159. t.Fatal("reqmod.(verify.RequestVerifier): got !ok, want ok")
  160. }
  161. req, err := http.NewRequest("GET", "http://example.com", nil)
  162. if err != nil {
  163. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  164. }
  165. _, remove, err := martian.TestContext(req, nil, nil)
  166. if err != nil {
  167. t.Fatalf("TestContext(): got %v, want no error", err)
  168. }
  169. defer remove()
  170. if err := reqv.ModifyRequest(req); err != nil {
  171. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  172. }
  173. if err := reqv.VerifyRequests(); err == nil {
  174. t.Error("VerifyRequests(): got nil, want not nil")
  175. }
  176. }