ldservices

package
v2.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2022 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package ldservices provides HTTP handlers that simulate the behavior of LaunchDarkly service endpoints.

This is mainly intended for use in the LaunchDarkly Go SDK's unit tests, but it could also be useful in testing applications that use the Go SDK if it is desirable to use real HTTP rather than test fixtures.

Index

Constants

View Source
const (
	// ServerSideSDKStreamingPath is the expected request path for server-side SDK stream requests.
	ServerSideSDKStreamingPath = "/all"
)

Variables

This section is empty.

Functions

func ServerSideEventsServiceHandler

func ServerSideEventsServiceHandler() http.Handler

ServerSideEventsServiceHandler creates an HTTP handler to mimic the LaunchDarkly server-side events service. It returns a 202 status for POSTs to the /bulk and /diagnostic paths, otherwise a 404.

func ServerSidePollingServiceHandler

func ServerSidePollingServiceHandler(data interface{}) http.Handler

ServerSidePollingServiceHandler creates an HTTP handler to mimic the LaunchDarkly server-side polling service.

This handler returns JSON data for requests to /sdk/latest-all, and a 404 error for all other requests.

Since this package cannot depend on the LaunchDarkly data model types, the caller is responsible for providing an object that can be marshaled to JSON (such as ServerSDKData). If the data parameter is nil, the default response is an empty JSON object {}. The data is marshalled again for each request.

data := NewServerSDKData().Flags(flag1, flag2)
handler := PollingServiceHandler(data)

If you want the mock service to return different responses at different points during a test, you can either provide a *ServerSDKData and modify its properties, or use a DelegatingHandler or SequentialHandler that can be made to delegate to the ServerSidePollingServiceHandler at one time but a different handler at another time.

func ServerSideStreamingServiceHandler

func ServerSideStreamingServiceHandler(
	initialEvent httphelpers.SSEEvent,
) (http.Handler, httphelpers.SSEStreamControl)

ServerSideStreamingServiceHandler creates an HTTP handler to mimic the LaunchDarkly server-side streaming service. It uses httphelpers.SSEHandler(), while also enforcing that the request path is ServerSideSDKStreamingPath and that the method is GET.

There must always be an initial event, since LaunchDarkly streams always start with a "put".

initialData := ldservices.NewServerSDKData().Flags(flag1, flag2) // all clients will get this in a "put" event
handler, stream := ldservices.ServerSideStreamingHandler(initialData.ToPutEvent())
server := httptest.NewServer(handler)
stream.Enqueue(httphelpers.SSEEvent{Event: "patch", Data: myPatchData}) // push an update
stream.Close() // force any current stream connections to be closed

Types

type KeyedData

type KeyedData interface {
	GetKey() string
}

KeyedData is an interface for use with ServerSideData as an abstraction for data model objects that have a key, since this package cannot depend on LaunchDarkly data model types themselves. The actual FeatureFlag and Segment types implement this method; you can also use FlagOrSegment for a stub object.

func FlagOrSegment

func FlagOrSegment(key string, version int) KeyedData

FlagOrSegment provides a stub implementation of KeyedData that has only "key" and "version" properties. This may be enough for some testing purposes that don't require full flag or segment data.

type ServerSDKData

type ServerSDKData struct {
	FlagsMap    map[string]interface{} `json:"flags"`
	SegmentsMap map[string]interface{} `json:"segments"`
}

ServerSDKData is a convenience type for constructing a test server-side SDK data payload for PollingServiceHandler or StreamingServiceHandler. Its String() method returns a JSON object with the expected "flags" and "segments" properties.

data := NewServerSDKData().Flags(flag1, flag2)
handler := PollingServiceHandler(data)

func NewServerSDKData

func NewServerSDKData() *ServerSDKData

NewServerSDKData creates a ServerSDKData instance.

func (*ServerSDKData) Flags

func (s *ServerSDKData) Flags(flags ...KeyedData) *ServerSDKData

Flags adds the specified items to the struct's "flags" map.

Each item may be either a stub object from FlagOrSegment or a real data model object that implements KeyedData.

func (*ServerSDKData) Segments

func (s *ServerSDKData) Segments(segments ...KeyedData) *ServerSDKData

Segments adds the specified items to the struct's "segments" map.

Each item may be either a stub object from FlagOrSegment or a real data model object that implements KeyedData.

func (*ServerSDKData) String

func (s *ServerSDKData) String() string

String returns the JSON encoding of the struct as a string.

func (*ServerSDKData) ToPutEvent

func (s *ServerSDKData) ToPutEvent() httphelpers.SSEEvent

ToPutEvent creates an SSE event in the format that is used by the server-side SDK streaming endpoint.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL