github.com/google/martian/v3@v3.3.3/auth/auth_filter_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 auth
    16  
    17  import (
    18  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/google/martian/v3"
    22  	"github.com/google/martian/v3/martiantest"
    23  	"github.com/google/martian/v3/proxyutil"
    24  )
    25  
    26  func TestFilter(t *testing.T) {
    27  	f := NewFilter()
    28  	if f.RequestModifier("id") != nil {
    29  		t.Fatalf("f.RequestModifier(%q): got reqmod, want nil", "id")
    30  	}
    31  	if f.ResponseModifier("id") != nil {
    32  		t.Fatalf("f.ResponseModifier(%q): got resmod, want nil", "id")
    33  	}
    34  
    35  	tm := martiantest.NewModifier()
    36  	f.SetRequestModifier("id", tm)
    37  	f.SetResponseModifier("id", tm)
    38  
    39  	if f.RequestModifier("id") != tm {
    40  		t.Errorf("f.RequestModifier(%q): got nil, want martiantest.Modifier", "id")
    41  	}
    42  	if f.ResponseModifier("id") != tm {
    43  		t.Errorf("f.ResponseModifier(%q): got nil, want martiantest.Modifier", "id")
    44  	}
    45  }
    46  
    47  func TestModifyRequest(t *testing.T) {
    48  	f := NewFilter()
    49  
    50  	tm := martiantest.NewModifier()
    51  	f.SetRequestModifier("id", tm)
    52  
    53  	req, err := http.NewRequest("GET", "http://example.com", nil)
    54  	if err != nil {
    55  		t.Fatalf("NewRequest(): got %v, want no error", err)
    56  	}
    57  
    58  	// No ID, auth required.
    59  	f.SetAuthRequired(true)
    60  
    61  	ctx, remove, err := martian.TestContext(req, nil, nil)
    62  	if err != nil {
    63  		t.Fatalf("martian.TestContext(): got %v, want no error", err)
    64  	}
    65  	defer remove()
    66  
    67  	if err := f.ModifyRequest(req); err != nil {
    68  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
    69  	}
    70  
    71  	actx := FromContext(ctx)
    72  	if actx.Error() == nil {
    73  		t.Error("actx.Error(): got nil, want error")
    74  	}
    75  	if tm.RequestModified() {
    76  		t.Error("tm.RequestModified(): got true, want false")
    77  	}
    78  	tm.Reset()
    79  
    80  	// No ID, auth not required.
    81  	f.SetAuthRequired(false)
    82  	actx.SetError(nil)
    83  
    84  	if err := f.ModifyRequest(req); err != nil {
    85  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
    86  	}
    87  
    88  	if actx.Error() != nil {
    89  		t.Errorf("actx.Error(): got %v, want no error", err)
    90  	}
    91  	if tm.RequestModified() {
    92  		t.Error("tm.RequestModified(): got true, want false")
    93  	}
    94  
    95  	// Valid ID.
    96  	actx.SetError(nil)
    97  	actx.SetID("id")
    98  
    99  	if err := f.ModifyRequest(req); err != nil {
   100  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
   101  	}
   102  	if actx.Error() != nil {
   103  		t.Errorf("actx.Error(): got %v, want no error", actx.Error())
   104  	}
   105  	if !tm.RequestModified() {
   106  		t.Error("tm.RequestModified(): got false, want true")
   107  	}
   108  }
   109  
   110  func TestModifyResponse(t *testing.T) {
   111  	f := NewFilter()
   112  
   113  	tm := martiantest.NewModifier()
   114  	f.SetResponseModifier("id", tm)
   115  
   116  	req, err := http.NewRequest("GET", "http://example.com", nil)
   117  	if err != nil {
   118  		t.Fatalf("http.NewRequest(): got %v, want no error", err)
   119  	}
   120  	res := proxyutil.NewResponse(200, nil, req)
   121  
   122  	// No ID, auth required.
   123  	f.SetAuthRequired(true)
   124  
   125  	ctx, remove, err := martian.TestContext(req, nil, nil)
   126  	if err != nil {
   127  		t.Fatalf("martian.TestContext(): got %v, want no error", err)
   128  	}
   129  	defer remove()
   130  
   131  	if err := f.ModifyResponse(res); err != nil {
   132  		t.Fatalf("ModifyResponse(): got %v, want no error", err)
   133  	}
   134  
   135  	actx := FromContext(ctx)
   136  	if actx.Error() == nil {
   137  		t.Error("actx.Error(): got nil, want error")
   138  	}
   139  	if tm.ResponseModified() {
   140  		t.Error("tm.RequestModified(): got true, want false")
   141  	}
   142  
   143  	// No ID, no auth required.
   144  	f.SetAuthRequired(false)
   145  	actx.SetError(nil)
   146  
   147  	if err := f.ModifyResponse(res); err != nil {
   148  		t.Fatalf("ModifyResponse(): got %v, want no error", err)
   149  	}
   150  	if tm.ResponseModified() {
   151  		t.Error("tm.ResponseModified(): got true, want false")
   152  	}
   153  
   154  	// Valid ID.
   155  	actx.SetID("id")
   156  
   157  	if err := f.ModifyResponse(res); err != nil {
   158  		t.Fatalf("ModifyResponse(): got %v, want no error", err)
   159  	}
   160  	if !tm.ResponseModified() {
   161  		t.Error("tm.ResponseModified(): got false, want true")
   162  	}
   163  }