@@ -3106,3 +3106,105 @@ func TestUnsupportedandSemverMatcherRedis(t *testing.T) {
31063106		prefixedClient .Del (k )
31073107	}
31083108}
3109+ 
3110+ func  TestPrerequisites (t  * testing.T ) {
3111+ 	var  isDestroyCalled  =  false 
3112+ 	var  splitsMock , _  =  ioutil .ReadFile ("../../testdata/splits_mock_5.json" )
3113+ 
3114+ 	postChannel  :=  make (chan  string , 1 )
3115+ 
3116+ 	ts  :=  httptest .NewServer (http .HandlerFunc (func (w  http.ResponseWriter , r  * http.Request ) {
3117+ 		switch  r .URL .Path  {
3118+ 		case  "/api/v2/auth" :
3119+ 			if  r .URL .Query ().Get ("s" ) !=  "1.3"  {
3120+ 				t .Error ("should be parameter s, for flags spec" )
3121+ 			}
3122+ 			fmt .Fprintln (w , "{\" pushEnabled\" : false, \" token\" : \" token\" }" )
3123+ 			return 
3124+ 		case  "/splitChanges" :
3125+ 			fmt .Fprintln (w , string (splitsMock ))
3126+ 			return 
3127+ 		case  "/testImpressions/bulk" :
3128+ 			if  r .Header .Get ("SplitSDKImpressionsMode" ) !=  commonsCfg .ImpressionsModeOptimized  {
3129+ 				t .Error ("Wrong header" )
3130+ 			}
3131+ 
3132+ 			if  isDestroyCalled  {
3133+ 				rBody , _  :=  ioutil .ReadAll (r .Body )
3134+ 				var  dataInPost  []map [string ]interface {}
3135+ 				err  :=  json .Unmarshal (rBody , & dataInPost )
3136+ 				if  err  !=  nil  {
3137+ 					t .Error (err )
3138+ 					return 
3139+ 				}
3140+ 				if  len (dataInPost ) !=  1  {
3141+ 					t .Error ("It should send one impressions in optimized mode" )
3142+ 				}
3143+ 				for  _ , ki  :=  range  dataInPost  {
3144+ 					if  asISlice , ok  :=  ki ["i" ].([]interface {}); ! ok  ||  len (asISlice ) !=  3  {
3145+ 						t .Error ("It should send three impressions per featureName" , dataInPost )
3146+ 					}
3147+ 					if  ki ["f" ] ==  "unsupported"  {
3148+ 						message  :=  ki ["i" ].([]interface {})[0 ].(map [string ]interface {})["r" ]
3149+ 						if  message  !=  "targeting rule type unsupported by sdk"  {
3150+ 							t .Error ("message sould be: targeting rule type unsupported by sdk" )
3151+ 						}
3152+ 					}
3153+ 				}
3154+ 			}
3155+ 
3156+ 			fmt .Fprintln (w , "ok" )
3157+ 			postChannel  <-  "finished" 
3158+ 		case  "/testImpressions/count" :
3159+ 			fallthrough 
3160+ 		case  "/keys/ss" :
3161+ 			fallthrough 
3162+ 		case  "/events/bulk" :
3163+ 			fallthrough 
3164+ 		case  "/segmentChanges" :
3165+ 		default :
3166+ 			fmt .Fprintln (w , "ok" )
3167+ 			return 
3168+ 		}
3169+ 	}))
3170+ 	defer  ts .Close ()
3171+ 
3172+ 	cfg  :=  conf .Default ()
3173+ 	cfg .Advanced .AuthServiceURL  =  ts .URL 
3174+ 	cfg .Advanced .EventsURL  =  ts .URL 
3175+ 	cfg .Advanced .SdkURL  =  ts .URL 
3176+ 	cfg .Advanced .TelemetryServiceURL  =  ts .URL 
3177+ 
3178+ 	factory , _  :=  NewSplitFactory ("test" , cfg )
3179+ 	client  :=  factory .Client ()
3180+ 	client .BlockUntilReady (2 )
3181+ 
3182+ 	// Calls treatments to generate one valid impression 
3183+ 	time .Sleep (300  *  time .Millisecond ) // Let's wait until first call of recorders have finished 
3184+ 
3185+ 	evaluation  :=  client .Treatment ("mauro@split.io" , "always_on_if_prerequisite" , nil )
3186+ 	if  evaluation  !=  "off"  {
3187+ 		t .Error ("evaluation for mauro@split.io should be off" )
3188+ 	}
3189+ 
3190+ 	evaluation  =  client .Treatment ("bilal@split.io" , "always_on_if_prerequisite" , nil )
3191+ 	if  evaluation  !=  "on"  {
3192+ 		t .Error ("evaluation for bilal@split.io should be on" )
3193+ 	}
3194+ 
3195+ 	evaluation  =  client .Treatment ("other_key" , "always_on_if_prerequisite" , nil )
3196+ 	if  evaluation  !=  "off"  {
3197+ 		t .Error ("evaluation for other_key should be off" )
3198+ 	}
3199+ 
3200+ 	isDestroyCalled  =  true 
3201+ 	client .Destroy ()
3202+ 
3203+ 	select  {
3204+ 	case  <- postChannel :
3205+ 		return 
3206+ 	case  <- time .After (4  *  time .Second ):
3207+ 		t .Error ("The test couldn't send impressions to check headers" )
3208+ 		return 
3209+ 	}
3210+ }
0 commit comments