go.uber.org/yarpc@v1.72.1/transport/http/request_sender_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package http
    22  
    23  import (
    24  	"io"
    25  	"io/ioutil"
    26  	"net/http"
    27  	"net/http/httptest"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  // *http.Client do more than what a RoundTrip is supposed to do:
    35  // - It tries to handle higher-level protocol details such as redirects, authentications, or cookies.
    36  // - It requires a client request(requestURI can't be set) so it is not possible to proxy a server request transparently.
    37  // We want to make sure transportSender can proxy server requests transparently.
    38  func TestSender(t *testing.T) {
    39  	const data = "dummy server response body"
    40  
    41  	var (
    42  		server = httptest.NewServer(http.HandlerFunc(
    43  			func(w http.ResponseWriter, _ *http.Request) {
    44  				io.WriteString(w, data)
    45  			},
    46  		))
    47  		clientReq, _ = http.NewRequest("GET", server.URL, nil)
    48  		serverReq    = httptest.NewRequest("GET", server.URL, nil)
    49  		client       = &http.Client{
    50  			Transport: http.DefaultTransport,
    51  		}
    52  	)
    53  	defer server.Close()
    54  
    55  	tests := []struct {
    56  		msg            string
    57  		sender         sender
    58  		req            *http.Request
    59  		wantStatusCode int
    60  		wantBody       string
    61  		wantError      string
    62  	}{
    63  		{
    64  			msg:            "http.Client sender, http client request",
    65  			req:            clientReq,
    66  			sender:         http.DefaultClient,
    67  			wantStatusCode: http.StatusOK,
    68  			wantBody:       data,
    69  		},
    70  		{
    71  			msg:       "http.Client sender, http server request",
    72  			req:       serverReq,
    73  			sender:    http.DefaultClient,
    74  			wantError: "http: Request.RequestURI can't be set in client requests",
    75  		},
    76  		{
    77  			msg:            "transportSender, http client request",
    78  			req:            clientReq,
    79  			sender:         &transportSender{Client: client},
    80  			wantStatusCode: http.StatusOK,
    81  			wantBody:       data,
    82  		},
    83  		{
    84  			msg:            "transportSender, http server request",
    85  			req:            serverReq,
    86  			sender:         &transportSender{Client: client},
    87  			wantStatusCode: http.StatusOK,
    88  			wantBody:       data,
    89  		},
    90  	}
    91  
    92  	for _, tt := range tests {
    93  		t.Run(tt.msg, func(t *testing.T) {
    94  			resp, err := tt.sender.Do(tt.req)
    95  			if tt.wantError != "" {
    96  				require.Error(t, err, "expect error when we use http.Client to send a server request")
    97  				assert.Contains(t, err.Error(), tt.wantError, "error body mismatch")
    98  				return
    99  			}
   100  			assert.Equal(t, tt.wantStatusCode, resp.StatusCode, "status code does not match")
   101  			body, _ := ioutil.ReadAll(resp.Body)
   102  			defer resp.Body.Close()
   103  			assert.Equal(t, tt.wantBody, string(body), "response body does not match")
   104  		})
   105  	}
   106  }