...

Source file src/github.com/launchdarkly/go-server-sdk/v6/proxytest/http_transport_proxy_test.go

Documentation: github.com/launchdarkly/go-server-sdk/v6/proxytest

     1  //go:build proxytest1
     2  // +build proxytest1
     3  
     4  // Note, the tests in this package must be run one at a time in separate "go test" invocations, because
     5  // (depending on the platform) Go may cache the value of HTTP_PROXY. Therefore, we have a separate build
     6  // tag for each test and the Makefile runs this package once for each tag.
     7  
     8  package proxytest
     9  
    10  import (
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"os"
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/require"
    18  
    19  	"github.com/launchdarkly/go-test-helpers/v3/httphelpers"
    20  
    21  	"github.com/launchdarkly/go-server-sdk/v6/ldhttp"
    22  )
    23  
    24  func TestDefaultTransportUsesProxyEnvVars(t *testing.T) {
    25  	oldHttpProxy := os.Getenv("HTTP_PROXY")
    26  	defer os.Setenv("HTTP_PROXY", oldHttpProxy)
    27  
    28  	targetURL := "http://badhost/url"
    29  
    30  	// Create an extremely minimal fake proxy server that doesn't actually do any proxying, just to
    31  	// verify that we are connecting to it. If the HTTP_PROXY setting is ignored, then it will try
    32  	// to connect directly to the nonexistent host "badhost" instead and get an error.
    33  	handler, requestsCh := httphelpers.RecordingHandler(httphelpers.HandlerWithStatus(200))
    34  	httphelpers.WithServer(handler, func(proxy *httptest.Server) {
    35  		// Note that in normal usage, we will be connecting to secure LaunchDarkly endpoints, so it's
    36  		// really HTTPS_PROXY that is relevant. But support for HTTP_PROXY and HTTPS_PROXY comes from the
    37  		// same mechanism, so it's simpler to just test against an insecure proxy.
    38  		os.Setenv("HTTP_PROXY", proxy.URL)
    39  
    40  		transport, _, err := ldhttp.NewHTTPTransport()
    41  		require.NoError(t, err)
    42  
    43  		client := *http.DefaultClient
    44  		client.Transport = transport
    45  		resp, err := client.Get(targetURL)
    46  		require.NoError(t, err)
    47  
    48  		assert.Equal(t, 200, resp.StatusCode)
    49  		assert.Equal(t, 1, len(requestsCh))
    50  		r := <-requestsCh
    51  		assert.Equal(t, targetURL, r.Request.URL.String())
    52  	})
    53  }
    54  

View as plain text