github.com/google/martian/v3@v3.3.3/martianhttp/martianhttp_integration_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 martianhttp
    16  
    17  import (
    18  	"net"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"net/url"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/google/martian/v3"
    26  	"github.com/google/martian/v3/martiantest"
    27  
    28  	_ "github.com/google/martian/v3/header"
    29  )
    30  
    31  func TestIntegration(t *testing.T) {
    32  	ptr := martiantest.NewTransport()
    33  
    34  	proxy := martian.NewProxy()
    35  	defer proxy.Close()
    36  
    37  	proxy.SetRoundTripper(ptr)
    38  
    39  	l, err := net.Listen("tcp", "[::]:0")
    40  	if err != nil {
    41  		t.Fatalf("net.Listen(): got %v, want no error", err)
    42  	}
    43  
    44  	go proxy.Serve(l)
    45  
    46  	m := NewModifier()
    47  	proxy.SetRequestModifier(m)
    48  	proxy.SetResponseModifier(m)
    49  
    50  	mux := http.NewServeMux()
    51  	mux.Handle("/", m)
    52  
    53  	s := httptest.NewServer(mux)
    54  	defer s.Close()
    55  
    56  	body := strings.NewReader(`{
    57  		"header.Modifier": {
    58        "scope": ["request", "response"],
    59  			"name": "Martian-Test",
    60  			"value": "true"
    61  		}
    62  	}`)
    63  
    64  	res, err := http.Post(s.URL, "application/json", body)
    65  	if err != nil {
    66  		t.Fatalf("http.Post(%s): got %v, want no error", s.URL, err)
    67  	}
    68  	res.Body.Close()
    69  
    70  	if got, want := res.StatusCode, 200; got != want {
    71  		t.Fatalf("res.StatusCode: got %d, want %d", got, want)
    72  	}
    73  
    74  	tr := &http.Transport{
    75  		Proxy: http.ProxyURL(&url.URL{
    76  			Scheme: "http",
    77  			Host:   l.Addr().String(),
    78  		}),
    79  	}
    80  	defer tr.CloseIdleConnections()
    81  
    82  	req, err := http.NewRequest("GET", "http://example.com", nil)
    83  	if err != nil {
    84  		t.Fatalf("http.NewRequest(): got %v, want no error", err)
    85  	}
    86  	req.Header.Set("Connection", "close")
    87  
    88  	res, err = tr.RoundTrip(req)
    89  	if err != nil {
    90  		t.Fatalf("transport.RoundTrip(%q): got %v, want no error", req.URL, err)
    91  	}
    92  	res.Body.Close()
    93  
    94  	if got, want := res.Header.Get("Martian-Test"), "true"; got != want {
    95  		t.Errorf("res.Header.Get(%q): got %q, want %q", "Martian-Test", got, want)
    96  	}
    97  }