github.com/google/martian/v3@v3.3.3/header/hopbyhop_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  	"testing"
    20  
    21  	"github.com/google/martian/v3/proxyutil"
    22  )
    23  
    24  func TestRemoveHopByHopHeaders(t *testing.T) {
    25  	m := NewHopByHopModifier()
    26  	req, err := http.NewRequest("GET", "/", nil)
    27  	if err != nil {
    28  		t.Fatalf("http.NewRequest(): got %v, want no error", err)
    29  	}
    30  	req.Header = http.Header{
    31  		// Additional hop-by-hop headers are listed in the
    32  		// Connection header.
    33  		"Connection": []string{
    34  			"X-Connection",
    35  			"X-Hop-By-Hop, close",
    36  		},
    37  
    38  		// RFC hop-by-hop headers.
    39  		"Keep-Alive":          []string{},
    40  		"Proxy-Authenticate":  []string{},
    41  		"Proxy-Authorization": []string{},
    42  		"Te":                []string{},
    43  		"Trailer":           []string{},
    44  		"Transfer-Encoding": []string{},
    45  		"Upgrade":           []string{},
    46  		"Proxy-Connection":  []string{},
    47  
    48  		// Hop-by-hop headers listed in the Connection header.
    49  		"X-Connection": []string{},
    50  		"X-Hop-By-Hop": []string{},
    51  
    52  		// End-to-end header that should not be removed.
    53  		"X-End-To-End": []string{},
    54  	}
    55  
    56  	if err := m.ModifyRequest(req); err != nil {
    57  		t.Fatalf("ModifyRequest(): got %v, want no error", err)
    58  	}
    59  
    60  	if got, want := len(req.Header), 1; got != want {
    61  		t.Fatalf("len(req.Header): got %d, want %d", got, want)
    62  	}
    63  	if _, ok := req.Header["X-End-To-End"]; !ok {
    64  		t.Errorf("req.Header[%q]: got !ok, want ok", "X-End-To-End")
    65  	}
    66  
    67  	res := proxyutil.NewResponse(200, nil, req)
    68  	res.Header = req.Header
    69  	if err := m.ModifyResponse(res); err != nil {
    70  		t.Fatalf("ModifyResponse(): got %v, want no error", err)
    71  	}
    72  
    73  	if got, want := len(res.Header), 1; got != want {
    74  		t.Fatalf("len(res.Header): got %d, want %d", got, want)
    75  	}
    76  	if _, ok := res.Header["X-End-To-End"]; !ok {
    77  		t.Errorf("res.Header[%q]: got !ok, want ok", "X-End-To-End")
    78  	}
    79  }