1 package ldservices 2 3 import ( 4 "net/http" 5 6 "github.com/launchdarkly/go-test-helpers/v3/httphelpers" 7 ) 8 9 const serverSideSDKPollingPath = "/sdk/latest-all" 10 11 // ServerSidePollingServiceHandler creates an HTTP handler to mimic the LaunchDarkly server-side polling service. 12 // 13 // This handler returns JSON data for requests to /sdk/latest-all, and a 404 error for all other requests. 14 // 15 // Since this package cannot depend on the LaunchDarkly data model types, the caller is responsible for providing an 16 // object that can be marshaled to JSON (such as ServerSDKData). If the data parameter is nil, the default response 17 // is an empty JSON object {}. The data is marshalled again for each request. 18 // 19 // data := NewServerSDKData().Flags(flag1, flag2) 20 // handler := PollingServiceHandler(data) 21 // 22 // If you want the mock service to return different responses at different points during a test, you can either 23 // provide a *ServerSDKData and modify its properties, or use a DelegatingHandler or SequentialHandler that can 24 // be made to delegate to the ServerSidePollingServiceHandler at one time but a different handler at another time. 25 func ServerSidePollingServiceHandler(data interface{}) http.Handler { 26 if data == nil { 27 data = map[string]interface{}{} // default is an empty JSON object rather than null 28 } 29 return httphelpers.HandlerForPath(serverSideSDKPollingPath, 30 httphelpers.HandlerForMethod("GET", httphelpers.HandlerWithJSONResponse(data, nil), nil), nil) 31 } 32