const ( // ServerSideSDKStreamingPath is the expected request path for server-side SDK stream requests. ServerSideSDKStreamingPath = "/all" )
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() 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(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( 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
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)
type ServerSDKData struct { FlagsMap map[string]interface{} `json:"flags"` SegmentsMap map[string]interface{} `json:"segments"` }
func NewServerSDKData() *ServerSDKData
NewServerSDKData creates a ServerSDKData instance.
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 (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 (s *ServerSDKData) String() string
String returns the JSON encoding of the struct as a string.
func (s *ServerSDKData) ToPutEvent() httphelpers.SSEEvent
ToPutEvent creates an SSE event in the format that is used by the server-side SDK streaming endpoint.