ldservices

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2020 License: Apache-2.0 Imports: 8 Imported by: 3

Documentation

Overview

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

Index

Constants

View Source
const (
	// ServerSideSDKStreamingPath is the expected request path for server-side SDK stream requests.
	ServerSideSDKStreamingPath = "/all"
	// ClientSideSDKStreamingBasePath is the expected request path for client-side SDK stream requests that
	// use the REPORT method, or the base path (not including ClientSideOrMobileUserSubpath) for requests that
	// use the GET method.
	ClientSideSDKStreamingBasePath = "/eval"
	// MobileSDKStreamingBasePath is the expected request path for mobile SDK stream requests that
	// use the REPORT method, or the base path (not including ClientSideOrMobileUserSubpath) for requests that
	// use the GET method.
	MobileSDKStreamingBasePath = "/meval"
)

Variables

This section is empty.

Functions

func ClientSideStreamingServiceHandler added in v1.1.0

func ClientSideStreamingServiceHandler(
	initialEvent eventsource.Event,
	eventsCh <-chan eventsource.Event,
) (http.Handler, io.Closer)

ClientSideStreamingServiceHandler creates an HTTP handler to mimic the LaunchDarkly client-side streaming service.

This is the same as StreamingServiceHandler, but enforces that the request path and method correspond to one of the client-side/mobile endpoints.

initialData := ldservices.NewClientSDKData().Flags(flag1, flag2) // all clients will get this in a "put" event
eventsCh := make(chan eventsource.Event)
handler, closer := ldservices.ClientSideStreamingHandler(initialData, eventsCh)
server := httptest.NewServer(handler)
eventsCh <- myUpdatedFlag // push a "patch" event
closer.Close() // force any current stream connections to be closed

func NewSSEEvent

func NewSSEEvent(id, event, data string) eventsource.Event

NewSSEEvent constructs an implementation of eventsource.Event, to be used in testing eventsource or with StreamingServiceHandler.

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 eventsource.Event,
	eventsCh <-chan eventsource.Event,
) (http.Handler, io.Closer)

ServerSideStreamingServiceHandler creates an HTTP handler to mimic the LaunchDarkly server-side streaming service.

This is the same as StreamingServiceHandler, but enforces that the request path is ServerSideSDKStreamingPath and that the method is GET.

initialData := ldservices.NewServerSDKData().Flags(flag1, flag2) // all clients will get this in a "put" event
eventsCh := make(chan eventsource.Event)
handler, closer := ldservices.ServerSideStreamingHandler(initialData, eventsCh)
server := httptest.NewServer(handler)
eventsCh <- ldservices.NewSSEEvent("", "patch", myPatchData) // push a "patch" event
closer.Close() // force any current stream connections to be closed

func StreamingServiceHandler added in v1.1.0

func StreamingServiceHandler(
	initialEvent eventsource.Event,
	eventsCh <-chan eventsource.Event,
) (http.Handler, io.Closer)

StreamingServiceHandler creates an HTTP handler that provides an SSE stream.

This is a very simplistic implementation, not suitable for use in a real server that must handle multiple clients simultaneously (it has a single channel for events, so if there are multiple requests the events will be divided up random between them). Real applications should instead use the Server type in eventsource, which provides a multiplexed publish-subscribe model.

If initialEvent is non-nil, it will be sent at the beginning of each connection; you can pass a *ServerSDKData value to generate a "put" event.

Any events that are pushed to eventsCh (if it is non-nil) will be published to the stream.

Calling Close() on the returned io.Closer causes the handler to close any active stream connections and refuse all subsequent requests. You don't need to do this unless you need to force a stream to disconnect before the test server has been shut down; shutting down the server will close connections anyway.

Types

type ClientSDKData added in v1.1.0

type ClientSDKData map[string]json.RawMessage

ClientSDKData is a set of flag value data as provided by the client-side SDK endpoints.

It also implements the eventsource.Event interface, simulating a "put" event for the streaming service.

func NewClientSDKData added in v1.1.0

func NewClientSDKData() ClientSDKData

NewClientSDKData creates a ClientSDKData instance.

This constructor is provided in case we ever change the implementation to be something other than just a map.

func (ClientSDKData) Data added in v1.1.0

func (c ClientSDKData) Data() string

Data is for the eventsource.Event interface. It provides the marshalled data in the format used by the streaming service.

func (ClientSDKData) Event added in v1.1.0

func (c ClientSDKData) Event() string

Event is for the eventsource.Event interface. It returns "put".

func (ClientSDKData) Flags added in v1.1.0

func (c ClientSDKData) Flags(flags ...FlagValueData) ClientSDKData

Flags adds the specified items to the flags map.

func (ClientSDKData) Id added in v1.1.0

func (c ClientSDKData) Id() string

Id is for the eventsource.Event interface.

func (ClientSDKData) String added in v1.1.0

func (c ClientSDKData) String() string

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

type FlagValueData added in v1.1.0

type FlagValueData struct {
	// Key is the flag key.
	Key string
	// Version is the flag version, as defined in the server-side SDK endpoints.
	Version int
	// FlagVersion is another version property that is provided only by client-side endpoints.
	FlagVersion int
	// Value is the variation value for the current user.
	Value ldvalue.Value
	// VariationIndex is the variation index for the current user. Use -1 to omit this.
	VariationIndex int
	// Reason is the evaluation reason, if available. This is specified as an `ldvalue.Value` to avoid having to
	// bring in `EvaluationReason` and related types; the caller is responsible for providing the JSON
	// representation of the reason.
	Reason ldvalue.Value
	// TrackEvents is true if full event tracking is enabled for this flag.
	TrackEvents bool
	// DebugEventsUntilDate is the time, if any, until which debugging is enabled for this flag.
	DebugEventsUntilDate uint64
}

FlagValueData corresponds to the representation used by the client-side SDK endpoints.

It also implements the eventsource.Event interface, simulating a "patch" event for the streaming service.

func (FlagValueData) Data added in v1.1.0

func (f FlagValueData) Data() string

Data is for the eventsource.Event interface. It provides the marshalled data in the format used by the streaming service.

func (FlagValueData) Event added in v1.1.0

func (f FlagValueData) Event() string

Event is for the eventsource.Event interface. It returns "patch".

func (FlagValueData) Id added in v1.1.0

func (f FlagValueData) Id() string

Id is for the eventsource.Event interface.

func (FlagValueData) ToJSON added in v1.1.1

func (f FlagValueData) ToJSON(withKey bool) []byte

ToJSON returns the JSON representation of the flag data.

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)

It also implements the eventsource.Event interface, simulating a "put" event for the streaming service.

func NewServerSDKData

func NewServerSDKData() *ServerSDKData

NewServerSDKData creates a ServerSDKData instance.

func (*ServerSDKData) Data

func (s *ServerSDKData) Data() string

Data is for the eventsource.Event interface. It provides the marshalled data in the format used by the streaming service.

func (*ServerSDKData) Event

func (s *ServerSDKData) Event() string

Event is for the eventsource.Event interface. It returns "put".

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) Id

func (s *ServerSDKData) Id() string

Id is for the eventsource.Event interface.

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.

Jump to

Keyboard shortcuts

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