@@ -39,12 +39,20 @@ type mockClient struct {
39
39
collectorCallCount int
40
40
flagChangeCallCount int
41
41
collectorRequests []string
42
+ requestBodies []string
42
43
}
43
44
44
45
func (m * mockClient ) roundTripFunc (req * http.Request ) * http.Response {
46
+ //read req body and store it
47
+ var bodyBytes []byte
48
+ if req .Body != nil {
49
+ bodyBytes , _ = io .ReadAll (req .Body )
50
+ req .Body = io .NopCloser (bytes .NewReader (bodyBytes ))
51
+ m .requestBodies = append (m .requestBodies , string (bodyBytes ))
52
+ }
53
+
45
54
if req .URL .Path == "/v1/data/collector" {
46
55
m .collectorCallCount ++
47
- bodyBytes , _ := io .ReadAll (req .Body )
48
56
m .collectorRequests = append (m .collectorRequests , string (bodyBytes ))
49
57
return & http.Response {
50
58
StatusCode : http .StatusOK ,
@@ -1082,5 +1090,56 @@ func TestProvider_FlagChangePolling(t *testing.T) {
1082
1090
require .NoError (t , err )
1083
1091
assert .Equal (t , of .TargetingMatchReason , details .Reason )
1084
1092
})
1093
+ }
1094
+
1095
+ func TestProvider_EvaluationEnrichmentHook (t * testing.T ) {
1096
+ tests := []struct {
1097
+ name string
1098
+ want string
1099
+ evalCtx of.EvaluationContext
1100
+ exporterMetadata map [string ]interface {}
1101
+ }{
1102
+ {
1103
+ name : "should add the metadata to the evaluation context" ,
1104
+ exporterMetadata : map [string ]interface {}{"toto" : 123 , "tata" : "titi" },
1105
+ evalCtx : defaultEvaluationCtx (),
1106
+ want :
`{"context":{"admin":true,"age":30,"anonymous":false,"company_info":{"name":"my_company","size":120},"email":"[email protected] ","firstname":"john","gofeatureflag":{"exporterMetadata":{"openfeature":true,"provider":"go","tata":"titi","toto":123}},"labels":["pro","beta"],"lastname":"doe","professional":true,"rate":3.14,"targetingKey":"d45e303a-38c2-11ed-a261-0242ac120002"}}` ,
1107
+ },
1108
+ {
1109
+ name : "should have the default metadata if not provided" ,
1110
+ exporterMetadata : nil ,
1111
+ evalCtx : defaultEvaluationCtx (),
1112
+ want :
`{"context":{"admin":true,"age":30,"anonymous":false,"company_info":{"name":"my_company","size":120},"email":"[email protected] ","firstname":"john","gofeatureflag":{"exporterMetadata":{"openfeature":true,"provider":"go"}},"labels":["pro","beta"],"lastname":"doe","professional":true,"rate":3.14,"targetingKey":"d45e303a-38c2-11ed-a261-0242ac120002"}}` ,
1113
+ },
1114
+ {
1115
+ name : "should not remove other gofeatureflag specific metadata" ,
1116
+ exporterMetadata : map [string ]interface {}{"toto" : 123 , "tata" : "titi" },
1117
+ evalCtx : of .NewEvaluationContext ("d45e303a-38c2-11ed-a261-0242ac120002" , map [string ]interface {}{"age" : 30 , "gofeatureflag" : map [string ]interface {}{"flags" : []string {"flag1" , "flag2" }}}),
1118
+ want : `{"context":{"age":30,"gofeatureflag":{"flags":["flag1","flag2"], "exporterMetadata":{"openfeature":true,"provider":"go","tata":"titi","toto":123}}, "targetingKey":"d45e303a-38c2-11ed-a261-0242ac120002"}}` ,
1119
+ },
1120
+ }
1121
+
1122
+ for _ , tt := range tests {
1123
+ t .Run (tt .name , func (t * testing.T ) {
1124
+ cli := mockClient {}
1125
+ options := gofeatureflag.ProviderOptions {
1126
+ Endpoint : "https://gofeatureflag.org/" ,
1127
+ HTTPClient : NewMockClient (cli .roundTripFunc ),
1128
+ ExporterMetadata : tt .exporterMetadata ,
1129
+ }
1130
+ provider , err := gofeatureflag .NewProvider (options )
1131
+ defer provider .Shutdown ()
1132
+ assert .NoError (t , err )
1133
+ err = of .SetProviderAndWait (provider )
1134
+ assert .NoError (t , err )
1135
+ client := of .NewClient ("test-app" )
1085
1136
1137
+ _ , err = client .BooleanValueDetails (context .TODO (), "bool_targeting_match" , false , tt .evalCtx )
1138
+ assert .NoError (t , err )
1139
+
1140
+ want := tt .want
1141
+ got := cli .requestBodies [0 ]
1142
+ assert .JSONEq (t , want , got )
1143
+ })
1144
+ }
1086
1145
}
0 commit comments