github.com/blend/go-sdk@v1.20220411.3/reverseproxy/proxy_option_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package reverseproxy
     9  
    10  import (
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"net/url"
    14  	"reflect"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/blend/go-sdk/assert"
    19  	"github.com/blend/go-sdk/webutil"
    20  )
    21  
    22  func TestOptProxyTransformRequest(t *testing.T) {
    23  	t.Skip() // test is flaky
    24  	it := assert.New(t)
    25  
    26  	var requests []*http.Request
    27  	tr := func(req *http.Request) {
    28  		requests = append(requests, req)
    29  	}
    30  
    31  	target, err := url.Parse("http://web.invalid:9876")
    32  	it.Nil(err)
    33  
    34  	p, err := NewProxy(
    35  		OptProxyUpstream(NewUpstream(target,
    36  			OptUpstreamDial(
    37  				OptDialTimeout(time.Second),
    38  			),
    39  		)),
    40  		OptProxySetHeaderValue(webutil.HeaderXForwardedProto, webutil.SchemeHTTP),
    41  		OptProxyTransformRequest(tr),
    42  	)
    43  	it.Nil(err)
    44  	// Need to special case function equality.
    45  	it.Equal(reflect.ValueOf(tr).Pointer(), reflect.ValueOf(p.TransformRequest).Pointer())
    46  
    47  	mockedProxy := httptest.NewServer(p)
    48  	defer mockedProxy.Close()
    49  
    50  	res, err := http.Get(mockedProxy.URL)
    51  	it.Nil(err)
    52  	defer res.Body.Close()
    53  	it.Equal(http.StatusBadGateway, res.StatusCode)
    54  
    55  	it.Len(requests, 1)
    56  	calledReq := requests[0]
    57  	it.NotNil(calledReq)
    58  	it.Equal("GET", calledReq.Method)
    59  	it.Equal("/", calledReq.URL.String())
    60  	it.Equal(mockedProxy.URL, "http://"+calledReq.Host)
    61  }