ldservices

package
v6.0.0-alpha.pub.2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2022 License: Apache-2.0 Imports: 6 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 Go SDK's unit tests. It is also used in unit tests for the LaunchDarkly Relay Proxy, and could be useful in testing other applications that use the Go SDK if it is desirable to use real HTTP rather than other kinds of 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 FlagOrSegment

func FlagOrSegment(key string, version int) interface{}

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

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 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 ...interface{}) *ServerSDKData

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

Each item may be either a object produced by FlagOrSegment or a real data model object from the ldmodel package. The minimum requirement is that when converted to JSON, it has a "key" property.

func (*ServerSDKData) Segments

func (s *ServerSDKData) Segments(segments ...interface{}) *ServerSDKData

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

Each item may be either a object produced by FlagOrSegment or a real data model object from the ldmodel package. The minimum requirement is that when converted to JSON, it has a "key" property.

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