...

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

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

     1  //go:build proxytest2
     2  // +build proxytest2
     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/httptest"
    12  	"os"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/launchdarkly/go-test-helpers/v3/httphelpers"
    17  
    18  	ld "github.com/launchdarkly/go-server-sdk/v6"
    19  	"github.com/launchdarkly/go-server-sdk/v6/interfaces"
    20  	"github.com/launchdarkly/go-server-sdk/v6/internal/sharedtest"
    21  	"github.com/launchdarkly/go-server-sdk/v6/ldcomponents"
    22  	"github.com/launchdarkly/go-server-sdk/v6/testhelpers/ldservices"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestClientUsesProxyEnvVars(t *testing.T) {
    29  	oldHttpProxy := os.Getenv("HTTP_PROXY")
    30  	defer os.Setenv("HTTP_PROXY", oldHttpProxy)
    31  
    32  	fakeBaseURL := "http://badhost"
    33  	fakeEndpointURL := fakeBaseURL + "/sdk/latest-all"
    34  
    35  	// Create an extremely minimal fake proxy server that doesn't actually do any proxying, just to
    36  	// verify that we are connecting to it. If the HTTP_PROXY setting is ignored, then it will try
    37  	// to connect directly to the nonexistent host "badhost" instead and get an error.
    38  	handler, requestsCh := httphelpers.RecordingHandler(ldservices.ServerSidePollingServiceHandler(ldservices.NewServerSDKData()))
    39  	httphelpers.WithServer(handler, func(proxy *httptest.Server) {
    40  		// Note that in normal usage, we will be connecting to secure LaunchDarkly endpoints, so it's
    41  		// really HTTPS_PROXY that is relevant. But support for HTTP_PROXY and HTTPS_PROXY comes from the
    42  		// same mechanism, so it's simpler to just test against an insecure proxy.
    43  		os.Setenv("HTTP_PROXY", proxy.URL)
    44  
    45  		config := ld.Config{}
    46  		config.Logging = ldcomponents.Logging().Loggers(sharedtest.NewTestLoggers())
    47  		config.DataSource = ldcomponents.PollingDataSource()
    48  		config.Events = ldcomponents.NoEvents()
    49  		config.ServiceEndpoints = interfaces.ServiceEndpoints{Polling: fakeBaseURL}
    50  
    51  		client, err := ld.MakeCustomClient("sdkKey", config, 5*time.Second)
    52  		require.NoError(t, err)
    53  		defer client.Close()
    54  
    55  		assert.Equal(t, 1, len(requestsCh))
    56  		r := <-requestsCh
    57  		assert.Equal(t, fakeEndpointURL, r.Request.URL.String())
    58  	})
    59  }
    60  
    61  func TestClientOverridesProxyEnvVarsWithProgrammaticProxyOption(t *testing.T) {
    62  	fakeBaseURL := "http://badhost"
    63  	fakeEndpointURL := fakeBaseURL + "/sdk/latest-all"
    64  
    65  	// Create an extremely minimal fake proxy server that doesn't actually do any proxying, just to
    66  	// verify that we are connecting to it. If the HTTP_PROXY setting is ignored, then it will try
    67  	// to connect directly to the nonexistent host "badhost" instead and get an error.
    68  	handler, requestsCh := httphelpers.RecordingHandler(ldservices.ServerSidePollingServiceHandler(ldservices.NewServerSDKData()))
    69  	httphelpers.WithServer(handler, func(proxy *httptest.Server) {
    70  		config := ld.Config{}
    71  		config.HTTP = ldcomponents.HTTPConfiguration().ProxyURL(proxy.URL)
    72  		config.Logging = ldcomponents.Logging().Loggers(sharedtest.NewTestLoggers())
    73  		config.DataSource = ldcomponents.PollingDataSource()
    74  		config.Events = ldcomponents.NoEvents()
    75  		config.ServiceEndpoints = interfaces.ServiceEndpoints{Polling: fakeBaseURL}
    76  
    77  		client, err := ld.MakeCustomClient("sdkKey", config, 5*time.Second)
    78  		require.NoError(t, err)
    79  		defer client.Close()
    80  
    81  		assert.Equal(t, 1, len(requestsCh))
    82  		r := <-requestsCh
    83  		assert.Equal(t, fakeEndpointURL, r.Request.URL.String())
    84  	})
    85  }
    86  

View as plain text