...
1
2
3
4
5
6
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
36
37
38 handler, requestsCh := httphelpers.RecordingHandler(ldservices.ServerSidePollingServiceHandler(ldservices.NewServerSDKData()))
39 httphelpers.WithServer(handler, func(proxy *httptest.Server) {
40
41
42
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
66
67
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