123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- // Copyright 2015 Google Inc. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package querystring
- import (
- "net/http"
- "testing"
- "github.com/google/martian"
- "github.com/google/martian/parse"
- "github.com/google/martian/verify"
- )
- func TestVerifyRequestPasses(t *testing.T) {
- v, err := NewVerifier("foo", "bar")
- if err != nil {
- t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "bar", err)
- }
- req, err := http.NewRequest("GET", "http://www.google.com?foo=baz&foo=bar", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
- _, remove, err := martian.TestContext(req, nil, nil)
- if err != nil {
- t.Fatalf("TestContext(): got %v, want no error", err)
- }
- defer remove()
- if err := v.ModifyRequest(req); err != nil {
- t.Fatalf("ModifyRequest(): got %v, want no error", err)
- }
- if err := v.VerifyRequests(); err != nil {
- t.Fatalf("VerifyRequests(): got %v, want no error", err)
- }
- }
- func TestVerifyEmptyValue(t *testing.T) {
- v, err := NewVerifier("foo", "")
- if err != nil {
- t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "", err)
- }
- req, err := http.NewRequest("GET", "http://www.google.com?foo=bar", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
- _, remove, err := martian.TestContext(req, nil, nil)
- if err != nil {
- t.Fatalf("TestContext(): got %v, want no error", err)
- }
- defer remove()
- if err := v.ModifyRequest(req); err != nil {
- t.Fatalf("ModifyRequest(): got %v, want no error", err)
- }
- if err := v.VerifyRequests(); err != nil {
- t.Fatalf("VerifyRequests(): got %v, want no error", err)
- }
- }
- func TestFailureWithMissingKey(t *testing.T) {
- v, err := NewVerifier("foo", "bar")
- if err != nil {
- t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "bar", err)
- }
- req, err := http.NewRequest("GET", "http://www.google.com?fizz=bar", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
- _, remove, err := martian.TestContext(req, nil, nil)
- if err != nil {
- t.Fatalf("TestContext(): got %v, want no error", err)
- }
- defer remove()
- if err := v.ModifyRequest(req); err != nil {
- t.Fatalf("ModifyRequest(): got %v, want no error", err)
- }
- merr, ok := v.VerifyRequests().(*martian.MultiError)
- if !ok {
- t.Fatal("VerifyRequests(): got nil, want *verify.MultiError")
- }
- errs := merr.Errors()
- if len(errs) != 1 {
- t.Fatalf("len(merr.Errors()): got %d, want 1", len(errs))
- }
- expectErr := "request(http://www.google.com?fizz=bar) param verification error: key foo not found"
- for i := range errs {
- if got, want := errs[i].Error(), expectErr; got != want {
- t.Errorf("%d. err.Error(): mismatched error output\ngot: %s\nwant: %s", i, got, want)
- }
- }
- }
- func TestFailureWithMultiFail(t *testing.T) {
- v, err := NewVerifier("foo", "bar")
- if err != nil {
- t.Fatalf("NewVerifier(%q, %q): got %v, want no error", "foo", "bar", err)
- }
- req, err := http.NewRequest("GET", "http://www.google.com?foo=baz", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
- _, remove, err := martian.TestContext(req, nil, nil)
- if err != nil {
- t.Fatalf("TestContext(): got %v, want no error", err)
- }
- defer remove()
- if err := v.ModifyRequest(req); err != nil {
- t.Fatalf("ModifyRequest(): got %v, want no error", err)
- }
- if err := v.ModifyRequest(req); err != nil {
- t.Fatalf("ModifyRequest(): got %v, want no error", err)
- }
- merr, ok := v.VerifyRequests().(*martian.MultiError)
- if !ok {
- t.Fatalf("VerifyRequests(): got nil, want *verify.MultiError")
- }
- errs := merr.Errors()
- if len(errs) != 2 {
- t.Fatalf("len(merr.Errors()): got %d, want 2", len(errs))
- }
- expectErr := "request(http://www.google.com?foo=baz) param verification error: got baz for key foo, want bar"
- for i := range errs {
- if got, want := errs[i].Error(), expectErr; got != want {
- t.Errorf("%d. err.Error(): mismatched error output\ngot: %s\nwant: %s", i,
- got, want)
- }
- }
- v.ResetRequestVerifications()
- if err := v.VerifyRequests(); err != nil {
- t.Fatalf("VerifyRequests(): got %v, want no error", err)
- }
- }
- func TestBadInputToConstructor(t *testing.T) {
- if _, err := NewVerifier("", "bar"); err == nil {
- t.Fatalf("NewVerifier(): no error returned for empty key")
- }
- }
- func TestVerifierFromJSON(t *testing.T) {
- msg := []byte(`{
- "querystring.Verifier": {
- "scope": ["request"],
- "name": "param",
- "value": "true"
- }
- }`)
- r, err := parse.FromJSON(msg)
- if err != nil {
- t.Fatalf("parse.FromJSON(): got %v, want no error", err)
- }
- reqmod := r.RequestModifier()
- if reqmod == nil {
- t.Fatal("reqmod: got nil, want not nil")
- }
- reqv, ok := reqmod.(verify.RequestVerifier)
- if !ok {
- t.Fatal("reqmod.(verify.RequestVerifier): got !ok, want ok")
- }
- req, err := http.NewRequest("GET", "http://example.com", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
- _, remove, err := martian.TestContext(req, nil, nil)
- if err != nil {
- t.Fatalf("TestContext(): got %v, want no error", err)
- }
- defer remove()
- if err := reqv.ModifyRequest(req); err != nil {
- t.Fatalf("ModifyRequest(): got %v, want no error", err)
- }
- if err := reqv.VerifyRequests(); err == nil {
- t.Error("VerifyRequests(): got nil, want not nil")
- }
- }
|