github.com/google/martian/v3@v3.3.3/header/via_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 header
    16  
    17  import (
    18  	"net/http"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/google/martian/v3"
    23  	"github.com/google/martian/v3/proxyutil"
    24  )
    25  
    26  func TestViaModifier(t *testing.T) {
    27  	m := NewViaModifier("martian")
    28  	req, err := http.NewRequest("GET", "/", nil)
    29  	if err != nil {
    30  		t.Fatalf("http.NewRequest(): got %v, want no error", err)
    31  	}
    32  	res := proxyutil.NewResponse(200, nil, req)
    33  
    34  	ctx, remove, err := martian.TestContext(req, nil, nil)
    35  	if err != nil {
    36  		t.Fatalf("martian.TestContext(): got %v, want no error", err)
    37  	}
    38  	defer remove()
    39  
    40  	if err := m.ModifyRequest(req); err != nil {
    41  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
    42  	}
    43  	if got, want := req.Header.Get("Via"), "1.1 martian"; !strings.HasPrefix(got, want) {
    44  		t.Errorf("req.Header.Get(%q): got %q, want prefixed with %q", "Via", got, want)
    45  	}
    46  
    47  	if err := m.ModifyResponse(res); err != nil {
    48  		t.Fatalf("ModifyResponse(): got %v, want no error", err)
    49  	}
    50  
    51  	req.Header.Set("Via", "1.0\talpha\t(martian)")
    52  	if err := m.ModifyRequest(req); err != nil {
    53  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
    54  	}
    55  	if got, want := req.Header.Get("Via"), "1.0\talpha\t(martian), 1.1 martian"; !strings.HasPrefix(got, want) {
    56  		t.Errorf("req.Header.Get(%q): got %q, want prefixed with %q", "Via", got, want)
    57  	}
    58  
    59  	m.SetBoundary("boundary")
    60  	req.Header.Set("Via", "1.0\talpha\t(martian), 1.1 martian-boundary, 1.1 beta")
    61  	if err := m.ModifyRequest(req); err == nil {
    62  		t.Fatal("ModifyRequest(): got nil, want request loop error")
    63  	}
    64  	if !ctx.SkippingRoundTrip() {
    65  		t.Errorf("ctx.SkippingRoundTrip(): got false, want true")
    66  	}
    67  
    68  	if err := m.ModifyResponse(res); err == nil {
    69  		t.Fatal("ModifyResponse(): got nil, want request loop error")
    70  	}
    71  	if got, want := res.StatusCode, 400; got != want {
    72  		t.Errorf("res.StatusCode: got %d, want %d", got, want)
    73  	}
    74  	if got, want := res.Status, http.StatusText(400); got != want {
    75  		t.Errorf("res.Status: got %q, want %q", got, want)
    76  	}
    77  }