github.com/google/martian/v3@v3.3.3/querystring/query_string_modifier_test.go (about)

     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  
    15  package querystring
    16  
    17  import (
    18  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/google/martian/v3/parse"
    22  )
    23  
    24  func TestNewQueryStringModifier(t *testing.T) {
    25  	mod := NewModifier("testing", "true")
    26  
    27  	req, err := http.NewRequest("GET", "/", nil)
    28  	if err != nil {
    29  		t.Fatalf("NewRequest(): got %v, want no error", err)
    30  	}
    31  
    32  	if err := mod.ModifyRequest(req); err != nil {
    33  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
    34  	}
    35  	if got, want := req.URL.Query().Get("testing"), "true"; got != want {
    36  		t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "testing", got, want)
    37  	}
    38  }
    39  
    40  func TestQueryStringModifierQueryExists(t *testing.T) {
    41  	mod := NewModifier("testing", "true")
    42  
    43  	req, err := http.NewRequest("GET", "/?testing=false", nil)
    44  	if err != nil {
    45  		t.Fatalf("NewRequest(): got %v, want no error", err)
    46  	}
    47  
    48  	if err := mod.ModifyRequest(req); err != nil {
    49  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
    50  	}
    51  	if got, want := req.URL.Query().Get("testing"), "true"; got != want {
    52  		t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "testing", got, want)
    53  	}
    54  }
    55  
    56  func TestQueryStringModifierQueryExistsMultipleKeys(t *testing.T) {
    57  	mod := NewModifier("testing", "true")
    58  
    59  	req, err := http.NewRequest("GET", "/?testing=false&testing=foo&foo=bar", nil)
    60  	if err != nil {
    61  		t.Fatalf("NewRequest(): got %v, want no error", err)
    62  	}
    63  
    64  	if err := mod.ModifyRequest(req); err != nil {
    65  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
    66  	}
    67  	if got, want := req.URL.Query().Get("testing"), "true"; got != want {
    68  		t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "testing", got, want)
    69  	}
    70  	if got, want := req.URL.Query().Get("foo"), "bar"; got != want {
    71  		t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "testing", got, want)
    72  	}
    73  }
    74  
    75  func TestModifierFromJSON(t *testing.T) {
    76  	msg := []byte(`
    77  	{
    78  		"querystring.Modifier": {
    79        "scope": ["request"],
    80  			"name": "param",
    81  			"value": "true"
    82  		}
    83  	}`)
    84  
    85  	r, err := parse.FromJSON(msg)
    86  	if err != nil {
    87  		t.Fatalf("parse.FromJSON(): got %v, want no error", err)
    88  	}
    89  
    90  	req, err := http.NewRequest("GET", "http://martian.test", nil)
    91  	if err != nil {
    92  		t.Fatalf("http.NewRequest(): got %q, want no error", err)
    93  	}
    94  
    95  	reqmod := r.RequestModifier()
    96  
    97  	if reqmod == nil {
    98  		t.Fatalf("reqmod: got nil, want not nil")
    99  	}
   100  
   101  	if err := reqmod.ModifyRequest(req); err != nil {
   102  		t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
   103  	}
   104  
   105  	if got, want := req.URL.Query().Get("param"), "true"; got != want {
   106  		t.Errorf("req.URL.Query().Get(%q): got %q, want %q", "param", got, want)
   107  	}
   108  }