github.com/blend/go-sdk@v1.20220411.3/reverseproxy/upstream_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  	"fmt"
    12  	"net/http"
    13  	"net/http/httptest"
    14  	"testing"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  	"github.com/blend/go-sdk/logger"
    18  )
    19  
    20  func TestUpstreamWithoutHopHeaders(t *testing.T) {
    21  	assert := assert.New(t)
    22  
    23  	u := NewUpstream(MustParseURL("http://localhost:5000"))
    24  	assert.NotNil(u.ReverseProxy)
    25  }
    26  
    27  func TestUpstreamServeHTTP(t *testing.T) {
    28  	assert := assert.New(t)
    29  
    30  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    31  		w.WriteHeader(http.StatusOK)
    32  		fmt.Fprintf(w, "OK!\n")
    33  	}))
    34  	defer srv.Close()
    35  
    36  	u := NewUpstream(MustParseURL(srv.URL))
    37  	assert.NotNil(u.ReverseProxy)
    38  	u.Log = logger.None()
    39  
    40  	proxy, err := NewProxy()
    41  	assert.Nil(err)
    42  	proxy.Upstreams = append(proxy.Upstreams, u)
    43  
    44  	req, err := http.NewRequest("GET", srv.URL, nil)
    45  	assert.Nil(err)
    46  
    47  	res, err := http.DefaultClient.Do(req)
    48  	assert.Nil(err)
    49  
    50  	assert.Empty(res.Header.Get("X-Forwarded-For"))
    51  	assert.Empty(res.Header.Get("X-Forwarded-Port"))
    52  }