github.com/google/martian/v3@v3.3.3/martian_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 martian
    16  
    17  import (
    18  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/google/martian/v3/proxyutil"
    22  )
    23  
    24  func TestModifierFuncs(t *testing.T) {
    25  	reqmod := RequestModifierFunc(
    26  		func(req *http.Request) error {
    27  			req.Header.Set("Request-Modified", "true")
    28  			return nil
    29  		})
    30  
    31  	resmod := ResponseModifierFunc(
    32  		func(res *http.Response) error {
    33  			res.Header.Set("Response-Modified", "true")
    34  			return nil
    35  		})
    36  
    37  	req, err := http.NewRequest("GET", "http://example.com", nil)
    38  	if err != nil {
    39  		t.Fatalf("http.NewRequest(): got %v, want no error", err)
    40  	}
    41  
    42  	if err := reqmod.ModifyRequest(req); err != nil {
    43  		t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
    44  	}
    45  	if got, want := req.Header.Get("Request-Modified"), "true"; got != want {
    46  		t.Errorf("req.Header.Get(%q): got %q, want %q", "Request-Modified", got, want)
    47  	}
    48  
    49  	res := proxyutil.NewResponse(200, nil, req)
    50  
    51  	if err := resmod.ModifyResponse(res); err != nil {
    52  		t.Fatalf("resmod.ModifyResponse(): got %v, want no error", err)
    53  	}
    54  	if got, want := res.Header.Get("Response-Modified"), "true"; got != want {
    55  		t.Errorf("res.Header.Get(%q): got %q, want %q", "Response-Modified", got, want)
    56  	}
    57  }