Skip to content

Commit

Permalink
feat: add http proxy test for client-side events
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Nov 18, 2024
1 parent f4a4ca2 commit cf93be3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
7 changes: 5 additions & 2 deletions sdktests/client_side_events_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package sdktests

import (
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
m "github.com/launchdarkly/go-test-helpers/v2/matchers"
h "github.com/launchdarkly/sdk-test-harness/v2/framework/helpers"
"github.com/launchdarkly/sdk-test-harness/v2/framework/ldtest"
"github.com/launchdarkly/sdk-test-harness/v2/mockld"
"github.com/launchdarkly/sdk-test-harness/v2/servicedef"

m "github.com/launchdarkly/go-test-helpers/v2/matchers"
)

func doClientSideEventTests(t *ldtest.T) {
Expand Down Expand Up @@ -40,6 +39,10 @@ func doClientSideEventRequestTests(t *ldtest.T) {
Header("Content-Type").Should(m.StringContains("application/json")),
))

if t.Capabilities().Has(servicedef.CapabilityHTTPProxy) {
eventTests.HttpProxy(t)
}

requestPathMatcher := h.IfElse(
sdkKind == mockld.JSClientSDK,
m.Equal("/events/bulk/"+envIDOrMobileKey),
Expand Down
40 changes: 40 additions & 0 deletions sdktests/common_tests_events_request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sdktests

import (
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -119,3 +120,42 @@ func (c CommonEventTests) UniquePayloadIDs(t *ldtest.T) {
}
})
}

func (c CommonEventTests) HttpProxy(t *ldtest.T) {

Check failure on line 124 in sdktests/common_tests_events_request.go

View workflow job for this annotation

GitHub Actions / build-and-test

ST1003: method HttpProxy should be HTTPProxy (stylecheck)
t.Run("http proxy", func(t *ldtest.T) {
events := NewSDKEventSink(t)

// The idea here is that we'll configure the SDK's service endpoints with an arbitrary host, but with the
// correct path that the test harness expects (like /endpoints/1). Then, we'll inject the actual test harness's
// endpoint via the HTTP Proxy configuration.
//
// The SDK should therefore:
// 1. Open a socket to the test harness's host and port
// 2. Send an HTTP request that has the arbitrary host and the correct path
//
// If the SDK didn't support proxying, then it would attempt to connect to the arbitrary host and
// the harness should fail the connection assertion.
eventURI := strings.Replace(events.Endpoint().BaseURL(), "localhost", "not.valid.local", 1)

u, err := url.Parse(events.Endpoint().BaseURL())
if err != nil {
t.Errorf("unexpected error parsing URL: %s", err)
t.FailNow()
}
u.Path = ""

dataSource := NewSDKDataSource(t, nil)

client := NewSDKClient(t, c.baseSDKConfigurationPlus(dataSource, events, WithEventsConfig(
servicedef.SDKConfigEventParams{
BaseURI: eventURI,
}), c.withHTTPProxy(u.String()))...)

c.sendArbitraryEvent(t, client)
client.FlushEvents(t)

request := events.Endpoint().RequireConnection(t, time.Second)

m.In(t).For("request method").Assert(request.Method, m.Equal("POST"))
})
}

0 comments on commit cf93be3

Please sign in to comment.