@@ -16,6 +16,27 @@ import (
1616 "github.com/stretchr/testify/assert"
1717)
1818
19+ func getTestHttpServer (t * testing.T , expectedPath string , expectedEnvKey string , expectedRequestBody * string , responseFixture string ) * httptest.Server {
20+ return httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
21+ assert .Equal (t , req .URL .Path , expectedPath )
22+ assert .Equal (t , expectedEnvKey , req .Header .Get ("X-Environment-Key" ))
23+
24+ if expectedRequestBody != nil {
25+ // Test that we sent the correct body
26+ rawBody , err := io .ReadAll (req .Body )
27+ assert .NoError (t , err )
28+
29+ assert .Equal (t , * expectedRequestBody , string (rawBody ))
30+ }
31+
32+ rw .Header ().Set ("Content-Type" , "application/json" )
33+
34+ _ , err := io .WriteString (rw , responseFixture )
35+
36+ assert .NoError (t , err )
37+ }))
38+ }
39+
1940func TestClientErrorsIfLocalEvaluationWithNonServerSideKey (t * testing.T ) {
2041 // When, Then
2142 assert .Panics (t , func () {
@@ -158,6 +179,135 @@ func TestClientUpdatesEnvironmentOnEachRefresh(t *testing.T) {
158179 assert .Equal (t , expectedEnvironmentRefreshCount , actualEnvironmentRefreshCounter .count )
159180}
160181
182+ func TestGetFlags (t * testing.T ) {
183+ // Given
184+ ctx := context .Background ()
185+ server := getTestHttpServer (t , "/api/v1/flags/" , fixtures .EnvironmentAPIKey , nil , fixtures .FlagsJson )
186+ defer server .Close ()
187+
188+ // When
189+ client := flagsmith .NewClient (fixtures .EnvironmentAPIKey , flagsmith .WithBaseURL (server .URL + "/api/v1/" ))
190+
191+ flags , err := client .GetFlags (ctx , nil )
192+
193+ // Then
194+ assert .NoError (t , err )
195+
196+ allFlags := flags .AllFlags ()
197+
198+ assert .Equal (t , 1 , len (allFlags ))
199+
200+ assert .Equal (t , fixtures .Feature1Name , allFlags [0 ].FeatureName )
201+ assert .Equal (t , fixtures .Feature1ID , allFlags [0 ].FeatureID )
202+ assert .Equal (t , fixtures .Feature1Value , allFlags [0 ].Value )
203+ }
204+
205+ func TestGetFlagsTransientIdentity (t * testing.T ) {
206+ // Given
207+ ctx := context .Background ()
208+ expectedRequestBody := `{"identifier":"transient","transient":true}`
209+ server := getTestHttpServer (t , "/api/v1/identities/" , fixtures .EnvironmentAPIKey , & expectedRequestBody , fixtures .IdentityResponseJson )
210+ defer server .Close ()
211+
212+ // When
213+ client := flagsmith .NewClient (fixtures .EnvironmentAPIKey , flagsmith .WithBaseURL (server .URL + "/api/v1/" ))
214+
215+ flags , err := client .GetFlags (ctx , & flagsmith.EvaluationContext {Identity : & flagsmith.IdentityEvaluationContext {Identifier : "transient" , Transient : true }})
216+
217+ // Then
218+ assert .NoError (t , err )
219+
220+ allFlags := flags .AllFlags ()
221+
222+ assert .Equal (t , 1 , len (allFlags ))
223+
224+ assert .Equal (t , fixtures .Feature1Name , allFlags [0 ].FeatureName )
225+ assert .Equal (t , fixtures .Feature1ID , allFlags [0 ].FeatureID )
226+ assert .Equal (t , fixtures .Feature1Value , allFlags [0 ].Value )
227+ }
228+
229+ func TestGetFlagsTransientTraits (t * testing.T ) {
230+ // Given
231+ ctx := context .Background ()
232+ expectedRequestBody := `{"identifier":"test_identity","traits":` +
233+ `[{"trait_key":"NullTrait","trait_value":null},` +
234+ `{"trait_key":"StringTrait","trait_value":"value"},` +
235+ `{"trait_key":"TransientTrait","trait_value":"value","transient":true}],"transient":false}`
236+ server := getTestHttpServer (t , "/api/v1/identities/" , fixtures .EnvironmentAPIKey , & expectedRequestBody , fixtures .IdentityResponseJson )
237+ defer server .Close ()
238+
239+ // When
240+ client := flagsmith .NewClient (fixtures .EnvironmentAPIKey , flagsmith .WithBaseURL (server .URL + "/api/v1/" ))
241+
242+ flags , err := client .GetFlags (
243+ ctx ,
244+ & flagsmith.EvaluationContext {
245+ Identity : & flagsmith.IdentityEvaluationContext {
246+ Identifier : "test_identity" ,
247+ Traits : map [string ]* flagsmith.TraitEvaluationContext {
248+ "NullTrait" : nil ,
249+ "StringTrait" : {Value : "value" },
250+ "TransientTrait" : {
251+ Value : "value" ,
252+ Transient : true ,
253+ },
254+ },
255+ },
256+ })
257+
258+ // Then
259+ assert .NoError (t , err )
260+
261+ allFlags := flags .AllFlags ()
262+
263+ assert .Equal (t , 1 , len (allFlags ))
264+
265+ assert .Equal (t , fixtures .Feature1Name , allFlags [0 ].FeatureName )
266+ assert .Equal (t , fixtures .Feature1ID , allFlags [0 ].FeatureID )
267+ assert .Equal (t , fixtures .Feature1Value , allFlags [0 ].Value )
268+ }
269+
270+ func TestGetFlagsEnvironmentEvaluationContextFlags (t * testing.T ) {
271+ // Given
272+ ctx := context .Background ()
273+ expectedEnvKey := "different"
274+ server := getTestHttpServer (t , "/api/v1/flags/" , expectedEnvKey , nil , fixtures .FlagsJson )
275+ defer server .Close ()
276+
277+ // When
278+ client := flagsmith .NewClient (fixtures .EnvironmentAPIKey , flagsmith .WithBaseURL (server .URL + "/api/v1/" ))
279+
280+ _ , err := client .GetFlags (
281+ ctx ,
282+ & flagsmith.EvaluationContext {
283+ Environment : & flagsmith.EnvironmentEvaluationContext {APIKey : expectedEnvKey },
284+ })
285+
286+ // Then
287+ assert .NoError (t , err )
288+ }
289+
290+ func TestGetFlagsEnvironmentEvaluationContextIdentity (t * testing.T ) {
291+ // Given
292+ ctx := context .Background ()
293+ expectedEnvKey := "different"
294+ server := getTestHttpServer (t , "/api/v1/identities/" , expectedEnvKey , nil , fixtures .IdentityResponseJson )
295+ defer server .Close ()
296+
297+ // When
298+ client := flagsmith .NewClient (fixtures .EnvironmentAPIKey , flagsmith .WithBaseURL (server .URL + "/api/v1/" ))
299+
300+ _ , err := client .GetFlags (
301+ ctx ,
302+ & flagsmith.EvaluationContext {
303+ Environment : & flagsmith.EnvironmentEvaluationContext {APIKey : expectedEnvKey },
304+ Identity : & flagsmith.IdentityEvaluationContext {Identifier : "test_identity" },
305+ })
306+
307+ // Then
308+ assert .NoError (t , err )
309+ }
310+
161311func TestGetEnvironmentFlagsUseslocalEnvironmentWhenAvailable (t * testing.T ) {
162312 // Given
163313 ctx := context .Background ()
0 commit comments