github.com/google/martian/v3@v3.3.3/header/framing_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  	"reflect"
    20  	"testing"
    21  )
    22  
    23  func TestBadFramingMultipleContentLengths(t *testing.T) {
    24  	m := NewBadFramingModifier()
    25  	req, err := http.NewRequest("GET", "/", nil)
    26  	if err != nil {
    27  		t.Fatalf("http.NewRequest(): got %v, want no error", err)
    28  	}
    29  	req.Header["Content-Length"] = []string{"42", "42, 42"}
    30  
    31  	if err := m.ModifyRequest(req); err != nil {
    32  		t.Errorf("ModifyRequest(): got %v, want no error", err)
    33  	}
    34  	if got, want := req.Header["Content-Length"], []string{"42"}; !reflect.DeepEqual(got, want) {
    35  		t.Errorf("req.Header[%q]: got %v, want %v", "Content-Length", got, want)
    36  	}
    37  
    38  	req.Header["Content-Length"] = []string{"42", "32, 42"}
    39  	if err := m.ModifyRequest(req); err == nil {
    40  		t.Error("ModifyRequest(): got nil, want error")
    41  	}
    42  }
    43  
    44  func TestBadFramingTransferEncodingAndContentLength(t *testing.T) {
    45  	m := NewBadFramingModifier()
    46  	req, err := http.NewRequest("GET", "/", nil)
    47  	if err != nil {
    48  		t.Fatalf("http.NewRequest(): got %v, want no error", err)
    49  	}
    50  	req.Header["Transfer-Encoding"] = []string{"gzip, chunked"}
    51  	req.Header["Content-Length"] = []string{"42"}
    52  
    53  	if err := m.ModifyRequest(req); err != nil {
    54  		t.Errorf("ModifyRequest(): got %v, want no error", err)
    55  	}
    56  	if _, ok := req.Header["Content-Length"]; ok {
    57  		t.Fatalf("req.Header[%q]: got ok, want !ok", "Content-Length")
    58  	}
    59  
    60  	req.Header.Set("Transfer-Encoding", "gzip, identity")
    61  	req.Header.Del("Content-Length")
    62  	if err := m.ModifyRequest(req); err == nil {
    63  		t.Error("ModifyRequest(): got nil, want error")
    64  	}
    65  }