|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- // Copyright 2017 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 cookie
-
- import (
- "net/http"
- "testing"
-
- "github.com/google/martian/filter"
- _ "github.com/google/martian/header"
- "github.com/google/martian/martiantest"
- "github.com/google/martian/parse"
- "github.com/google/martian/proxyutil"
- )
-
- func TestFilterFromJSON(t *testing.T) {
- msg := []byte(`{
- "cookie.Filter": {
- "scope": ["request", "response"],
- "name": "martian-cookie",
- "value": "true",
- "modifier": {
- "header.Modifier" : {
- "scope": ["request", "response"],
- "name": "Martian-Testing",
- "value": "true"
- }
- },
- "else": {
- "header.Modifier" : {
- "scope": ["request", "response"],
- "name": "Martian-Testing",
- "value": "false"
- }
- }
- }
- }`)
-
- 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")
- }
-
- resmod := r.ResponseModifier()
- if resmod == nil {
- t.Fatal("resmod: got nil, want not nil")
- }
-
- for _, tc := range []struct {
- name string
- wantMatch bool
- cookie *http.Cookie
- }{
- {
- name: "matching name and value",
- wantMatch: true,
- cookie: &http.Cookie{
- Name: "martian-cookie",
- Value: "true",
- },
- },
- {
- name: "matching name with mismatched value",
- wantMatch: false,
- cookie: &http.Cookie{
- Name: "martian-cookie",
- Value: "false",
- },
- },
- {
- name: "missing cookie",
- wantMatch: false,
- },
- } {
- req, err := http.NewRequest("GET", "http://example.com", nil)
- if err != nil {
- t.Errorf("%s: http.NewRequest(): got %v, want no error", tc.name, err)
- continue
- }
- if tc.cookie != nil {
- req.AddCookie(tc.cookie)
- }
-
- if err := reqmod.ModifyRequest(req); err != nil {
- t.Errorf("%s: ModifyRequest(): got %v, want no error", tc.name, err)
- continue
- }
-
- want := "false"
- if tc.wantMatch {
- want = "true"
- }
- if got := req.Header.Get("Martian-Testing"); got != want {
- t.Errorf("%s: req.Header.Get(%q): got %q, want %q", "Martian-Testing", tc.name, got, want)
- continue
- }
-
- res := proxyutil.NewResponse(200, nil, req)
- if tc.cookie != nil {
- c := &http.Cookie{Name: tc.cookie.Name, Value: tc.cookie.Value}
- res.Header.Add("Set-Cookie", c.String())
- }
-
- if err := resmod.ModifyResponse(res); err != nil {
- t.Fatalf("ModifyResponse(): got %v, want no error", err)
- }
-
- if got := res.Header.Get("Martian-Testing"); got != want {
- t.Fatalf("res.Header.Get(%q): got %q, want %q", "Martian-Testing", got, want)
- }
-
- }
- }
-
- func TestFilterFromJSONWithoutElse(t *testing.T) {
- msg := []byte(`{
- "cookie.Filter": {
- "scope": ["request", "response"],
- "name": "martian-cookie",
- "value": "true",
- "modifier": {
- "header.Modifier" : {
- "scope": ["request", "response"],
- "name": "Martian-Testing",
- "value": "true"
- }
- }
- }
- }`)
- _, err := parse.FromJSON(msg)
- if err != nil {
- t.Fatalf("parse.FromJSON(): got %v, want no error", err)
- }
- }
-
- func TestRequestWhenTrueCondition(t *testing.T) {
- cm := NewMatcher(&http.Cookie{Name: "Martian-Testing", Value: "true"})
-
- tt := []struct {
- name string
- value string
- want bool
- }{
- {
- name: "Martian-Production",
- value: "true",
- want: false,
- },
- {
- name: "Martian-Testing",
- value: "true",
- want: true,
- },
- }
-
- for i, tc := range tt {
- tm := martiantest.NewModifier()
-
- f := filter.New()
- f.SetRequestCondition(cm)
- f.RequestWhenTrue(tm)
-
- req, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
-
- req.AddCookie(&http.Cookie{Name: tc.name, Value: tc.value})
-
- if err := f.ModifyRequest(req); err != nil {
- t.Fatalf("%d. ModifyRequest(): got %v, want no error", i, err)
- }
-
- if tm.RequestModified() != tc.want {
- t.Errorf("%d. tm.RequestModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
- }
- }
- }
-
- func TestRequestWhenFalse(t *testing.T) {
- cm := NewMatcher(&http.Cookie{Name: "Martian-Testing", Value: "true"})
- tt := []struct {
- name string
- value string
- want bool
- }{
- {
- name: "Martian-Production",
- value: "true",
- want: true,
- },
- {
- name: "Martian-Testing",
- value: "true",
- want: false,
- },
- }
-
- for i, tc := range tt {
- tm := martiantest.NewModifier()
-
- f := filter.New()
- f.SetRequestCondition(cm)
- f.RequestWhenFalse(tm)
-
- req, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
-
- req.AddCookie(&http.Cookie{Name: tc.name, Value: tc.value})
-
- if err := f.ModifyRequest(req); err != nil {
- t.Fatalf("%d. ModifyRequest(): got %v, want no error", i, err)
- }
-
- if tm.RequestModified() != tc.want {
- t.Errorf("%d. tm.RequestModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
- }
- }
- }
-
- func TestResponseWhenTrue(t *testing.T) {
- cm := NewMatcher(&http.Cookie{Name: "Martian-Testing", Value: "true"})
-
- tt := []struct {
- name string
- value string
- want bool
- }{
- {
- name: "Martian-Production",
- value: "true",
- want: false,
- },
- {
- name: "Martian-Testing",
- value: "true",
- want: true,
- },
- }
-
- for i, tc := range tt {
- tm := martiantest.NewModifier()
-
- f := filter.New()
- f.SetResponseCondition(cm)
- f.ResponseWhenTrue(tm)
-
- req, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
-
- res := proxyutil.NewResponse(200, nil, req)
-
- c := &http.Cookie{Name: tc.name, Value: tc.value}
- res.Header.Add("Set-Cookie", c.String())
-
- if err := f.ModifyResponse(res); err != nil {
- t.Fatalf("%d. ModifyResponse(): got %v, want no error", i, err)
- }
-
- if tm.ResponseModified() != tc.want {
- t.Errorf("%d. tm.ResponseModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
- }
- }
- }
-
- func TestResponseWhenFalse(t *testing.T) {
- cm := NewMatcher(&http.Cookie{Name: "Martian-Testing", Value: "true"})
-
- tt := []struct {
- name string
- value string
- want bool
- }{
- {
- name: "Martian-Production",
- value: "true",
- want: true,
- },
- {
- name: "Martian-Testing",
- value: "true",
- want: false,
- },
- }
-
- for i, tc := range tt {
- tm := martiantest.NewModifier()
-
- f := filter.New()
- f.SetResponseCondition(cm)
- f.ResponseWhenFalse(tm)
-
- req, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatalf("http.NewRequest(): got %v, want no error", err)
- }
-
- res := proxyutil.NewResponse(200, nil, req)
-
- c := &http.Cookie{Name: tc.name, Value: tc.value}
- res.Header.Add("Set-Cookie", c.String())
-
- if err := f.ModifyResponse(res); err != nil {
- t.Fatalf("%d. ModifyResponse(): got %v, want no error", i, err)
- }
-
- if tm.ResponseModified() != tc.want {
- t.Errorf("%d. tm.ResponseModified(): got %t, want %t", i, tm.RequestModified(), tc.want)
- }
- }
- }
|