1
+ package client_test
2
+
3
+ import (
4
+ "encoding/json"
5
+ "github.com/drone/ff-golang-server-sdk/client"
6
+ "github.com/drone/ff-golang-server-sdk/dto"
7
+ "github.com/drone/ff-golang-server-sdk/evaluation"
8
+ "github.com/drone/ff-golang-server-sdk/rest"
9
+ "github.com/jarcoal/httpmock"
10
+ "github.com/stretchr/testify/assert"
11
+ "net/http"
12
+ "testing"
13
+ "os"
14
+ )
15
+
16
+ const (
17
+ sdkKey = "27bed8d2-2610-462b-90eb-d80fd594b623"
18
+ URL = "http://localhost/api/1.0"
19
+ AuthToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm9qZWN0IjoiMTA0MjM5NzYtODQ1MS00NmZjLTg2NzctYmNiZDM3MTA3M2JhIiwiZW52aXJvbm1lbnQiOiI3ZWQxMDI1ZC1hOWIxLTQxMjktYTg4Zi1lMjdlZjM2MDk4MmQiLCJwcm9qZWN0SWRlbnRpZmllciI6IiIsImVudmlyb25tZW50SWRlbnRpZmllciI6IlByZVByb2R1Y3Rpb24iLCJhY2NvdW50SUQiOiIiLCJvcmdhbml6YXRpb24iOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAifQ.z6EYSDVWwwAY6OTc2PnjSub43R6lOSJywlEObi6PDqQ"
20
+
21
+ )
22
+
23
+ // TestMain runs before the other tests
24
+ func TestMain (m * testing.M ) {
25
+ // httpMock overwrites the http.DefaultClient
26
+ httpmock .Activate ()
27
+ defer httpmock .DeactivateAndReset ()
28
+
29
+ // Register Default Responders
30
+ httpmock .RegisterResponder ("POST" , "http://localhost/api/1.0/client/auth" , ValidAuthResponse )
31
+ httpmock .RegisterResponder ("GET" , "http://localhost/api/1.0/client/env/7ed1025d-a9b1-4129-a88f-e27ef360982d/target-segments" , TargetSegmentsResponse )
32
+ httpmock .RegisterResponder ("GET" , "http://localhost/api/1.0/client/env/7ed1025d-a9b1-4129-a88f-e27ef360982d/feature-configs" , FeatureConfigsResponse )
33
+
34
+ os .Exit (m .Run ())
35
+ }
36
+
37
+ func TestCfClient_BoolVariation (t * testing.T ) {
38
+
39
+ client , target , err := MakeNewClientAndTarget ()
40
+ if err != nil {
41
+ t .Error (err )
42
+ }
43
+
44
+ type args struct {
45
+ key string
46
+ target * evaluation.Target
47
+ defaultValue bool
48
+ }
49
+ tests := []struct {
50
+ name string
51
+ args args
52
+ want bool
53
+ wantErr bool
54
+ }{
55
+ {"Test Invalid Flag Name returns default value" , args {"MadeUpIDontExist" , target , false }, false , false },
56
+ {"Test Default True Flag when On returns true" , args {"TestTrueOn" , target , false }, true , false },
57
+ {"Test Default True Flag when Off returns false" , args {"TestTrueOff" , target , true }, false , false },
58
+ {"Test Default False Flag when On returns false" , args {"TestTrueOn" , target , false }, true , false },
59
+ {"Test Default False Flag when Off returns true" , args {"TestTrueOff" , target , true }, false , false },
60
+ {"Test Default True Flag when Pre-Req is False returns false" , args {"TestTrueOnWithPreReqFalse" , target , true }, false , false },
61
+ {"Test Default True Flag when Pre-Req is True returns true" , args {"TestTrueOnWithPreReqTrue" , target , true }, true , false },
62
+ }
63
+ for _ , tt := range tests {
64
+ t .Run (tt .name , func (t * testing.T ) {
65
+ flag , err := client .BoolVariation (tt .args .key , tt .args .target , tt .args .defaultValue )
66
+ if (err != nil ) != tt .wantErr {
67
+ t .Errorf ("BoolVariation() error = %v, wantErr %v" , err , tt .wantErr )
68
+ return
69
+ }
70
+ assert .Equal (t , tt .want , flag , "%s didn't get expected value" , tt .name )
71
+ })
72
+ }
73
+ }
74
+
75
+ func TestCfClient_StringVariation (t * testing.T ) {
76
+
77
+ client , target , err := MakeNewClientAndTarget ()
78
+ if err != nil {
79
+ t .Error (err )
80
+ }
81
+
82
+ type args struct {
83
+ key string
84
+ target * evaluation.Target
85
+ defaultValue string
86
+ }
87
+ tests := []struct {
88
+ name string
89
+ args args
90
+ want string
91
+ wantErr bool
92
+
93
+ }{
94
+ {"Test Invalid Flag Name returns default value" , args {"MadeUpIDontExist" , target , "foo" }, "foo" , false },
95
+ {"Test Default String Flag with when On returns A" , args {"TestStringAOn" , target , "foo" }, "A" , false },
96
+ {"Test Default String Flag when Off returns B" , args {"TestStringAOff" , target , "foo" }, "B" , false },
97
+ {"Test Default String Flag when Pre-Req is False returns B" , args {"TestStringAOnWithPreReqFalse" , target , "foo" }, "B" , false },
98
+ {"Test Default String Flag when Pre-Req is True returns A" , args {"TestStringAOnWithPreReqTrue" , target , "foo" }, "A" , false },
99
+ }
100
+ for _ , tt := range tests {
101
+ t .Run (tt .name , func (t * testing.T ) {
102
+ flag , err := client .StringVariation (tt .args .key , tt .args .target , tt .args .defaultValue )
103
+ if (err != nil ) != tt .wantErr {
104
+ t .Errorf ("BoolVariation() error = %v, wantErr %v" , err , tt .wantErr )
105
+ return
106
+ }
107
+ assert .Equal (t , tt .want , flag , "%s didn't get expected value" , tt .name )
108
+ })
109
+ }
110
+ }
111
+
112
+ // MakeNewClientAndTarget creates a new client and target. If it returns
113
+ // error then something went wrong.
114
+ func MakeNewClientAndTarget () (* client.CfClient , * evaluation.Target , error ) {
115
+ target := target ()
116
+ client , err := newClient (http .DefaultClient )
117
+ if err != nil {
118
+ return nil , nil , err
119
+ }
120
+
121
+ // Wait to be authenticated - we can timeout if the channel doesn't return
122
+ if ok , err := client .IsInitialized (); ! ok {
123
+ return nil , nil , err
124
+ }
125
+
126
+ return client , target , nil
127
+ }
128
+
129
+
130
+ // newClient creates a new client with some default options
131
+ func newClient (httpClient * http.Client ) (* client.CfClient , error ) {
132
+ return client .NewCfClient (sdkKey ,
133
+ client .WithURL (URL ),
134
+ client .WithStreamEnabled (false ),
135
+ client .WithHTTPClient (httpClient ),
136
+ client .WithStoreEnabled (false ),
137
+ )
138
+ }
139
+
140
+ // target creates a new Target with some default values
141
+ func target () * evaluation.Target {
142
+ target := dto .NewTargetBuilder ("john" ).
143
+ Firstname ("John" ).
144
+ Lastname ("Doe" ).
145
+
146
+ Build ()
147
+ return target
148
+ }
149
+
150
+ var ValidAuthResponse = func (req * http.Request ) (* http.Response , error ) {
151
+ return httpmock .NewJsonResponse (200 , rest.AuthenticationResponse {
152
+ AuthToken : AuthToken })
153
+ }
154
+
155
+ var TargetSegmentsResponse = func (req * http.Request ) (* http.Response , error ) {
156
+ var AllSegmentsResponse []rest.Segment
157
+
158
+ err := json .Unmarshal ([]byte (`[
159
+ {
160
+ "environment": "PreProduction",
161
+ "excluded": [],
162
+ "identifier": "Beta_Users",
163
+ "included": [
164
+ {
165
+ "identifier": "john",
166
+ "name": "John",
167
+ },
168
+ {
169
+ "identifier": "paul",
170
+ "name": "Paul",
171
+ }
172
+ ],
173
+ "name": "Beta Users"
174
+ }
175
+ ]` ), & AllSegmentsResponse )
176
+ if err != nil {
177
+ return jsonError (err )
178
+ }
179
+ return httpmock .NewJsonResponse (200 , AllSegmentsResponse )
180
+ }
181
+
182
+ var FeatureConfigsResponse = func (req * http.Request ) (* http.Response , error ) {
183
+ var FeatureConfigResponse []rest.FeatureConfig
184
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeBoolFeatureConfigs ("TestTrueOn" , "true" , "false" , "on" )... )
185
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeBoolFeatureConfigs ("TestTrueOff" , "true" , "false" , "off" )... )
186
+
187
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeBoolFeatureConfigs ("TestFalseOn" , "false" , "true" , "on" )... )
188
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeBoolFeatureConfigs ("TestFalseOff" , "false" , "true" , "off" )... )
189
+
190
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeBoolFeatureConfigs ("TestTrueOnWithPreReqFalse" , "true" , "false" , "on" , MakeBoolPreRequisite ("PreReq1" , "false" ))... )
191
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeBoolFeatureConfigs ("TestTrueOnWithPreReqTrue" , "true" , "false" , "on" , MakeBoolPreRequisite ("PreReq1" , "true" ))... )
192
+
193
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeStringFeatureConfigs ("TestStringAOn" , "Alpha" , "Bravo" , "on" )... )
194
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeStringFeatureConfigs ("TestStringAOff" , "Alpha" , "Bravo" , "off" )... )
195
+
196
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeStringFeatureConfigs ("TestStringAOnWithPreReqFalse" , "Alpha" , "Bravo" , "on" , MakeBoolPreRequisite ("PreReq1" , "false" ))... )
197
+ FeatureConfigResponse = append (FeatureConfigResponse , MakeStringFeatureConfigs ("TestStringAOnWithPreReqTrue" , "Alpha" , "Bravo" , "on" , MakeBoolPreRequisite ("PreReq1" , "true" ))... )
198
+
199
+ return httpmock .NewJsonResponse (200 , FeatureConfigResponse )
200
+ }
0 commit comments