5
5
"context"
6
6
"encoding/json"
7
7
"errors"
8
- "fmt"
9
8
"io"
10
9
"net/http"
11
10
"strings"
@@ -36,45 +35,49 @@ func TestSuccess200(t *testing.T) {
36
35
t .Fatal (err )
37
36
}
38
37
39
- client := mockOutbound {
38
+ resolver := OutboundResolver { client : mockOutbound {
40
39
rsp : http.Response {
41
40
StatusCode : http .StatusOK ,
42
41
Body : io .NopCloser (bytes .NewReader (successBytes )),
43
42
},
44
- }
43
+ }}
45
44
46
- successDto , resolutionError := NewOutboundResolver ( client ) .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
45
+ successDto , resolutionError := resolver .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
47
46
48
47
if resolutionError != nil {
49
- t .Error ( fmt . Sprintf ( "expected no errors, but got error: %v" , err ) )
48
+ t .Errorf ( "expected no errors, but got error: %v" , err )
50
49
}
51
50
52
51
if successDto == nil {
53
52
t .Fatal ("expected non empty success response" )
54
53
}
55
54
56
55
if successDto .Value != success .Value {
57
- t .Errorf (fmt . Sprintf ( "expected value %v, but got %v" , success .Value , successDto .Value ) )
56
+ t .Errorf ("expected value %v, but got %v" , success .Value , successDto .Value )
58
57
}
59
58
60
59
if successDto .Variant != success .Variant {
61
- t .Errorf (fmt . Sprintf ( "expected variant %v, but got %v" , success .Variant , successDto .Variant ) )
60
+ t .Errorf ("expected variant %v, but got %v" , success .Variant , successDto .Variant )
62
61
}
63
62
64
63
if successDto .Reason != success .Reason {
65
- t .Errorf (fmt .Sprintf ("expected reason %s, but got %s" , success .Reason , successDto .Reason ))
64
+ t .Errorf ("expected reason %s, but got %s" , success .Reason , successDto .Reason )
65
+ }
66
+
67
+ if successDto .Metadata ["key" ] != "value" {
68
+ t .Errorf ("expected key to contain value %s, but got %s" , "value" , successDto .Metadata ["key" ])
66
69
}
67
70
})
68
71
69
72
t .Run ("invalid payload type results in general error" , func (t * testing.T ) {
70
- client := mockOutbound {
73
+ resolver := OutboundResolver { client : mockOutbound {
71
74
rsp : http.Response {
72
75
StatusCode : http .StatusOK ,
73
76
Body : io .NopCloser (bytes .NewReader ([]byte ("some payload" ))),
74
77
},
75
- }
78
+ }}
79
+ success , resolutionError := resolver .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
76
80
77
- success , resolutionError := NewOutboundResolver (client ).resolveSingle (context .Background (), "" , make (map [string ]interface {}))
78
81
validateErrorCode (success , resolutionError , of .GeneralCode , t )
79
82
})
80
83
}
@@ -116,8 +119,9 @@ func TestResolveGeneralErrors(t *testing.T) {
116
119
117
120
for _ , test := range tests {
118
121
t .Run (test .name , func (t * testing.T ) {
119
- // when
120
- success , resolutionError := NewOutboundResolver (test .client ).resolveSingle (context .Background (), "key" , map [string ]interface {}{})
122
+ resolver := OutboundResolver {client : test .client }
123
+ success , resolutionError := resolver .resolveSingle (context .Background (), "key" , map [string ]interface {}{})
124
+
121
125
validateErrorCode (success , resolutionError , of .GeneralCode , t )
122
126
})
123
127
}
@@ -166,31 +170,27 @@ func TestEvaluationError4xx(t *testing.T) {
166
170
t .Fatal (err )
167
171
}
168
172
169
- client := mockOutbound {
173
+ resolver := OutboundResolver { client : mockOutbound {
170
174
rsp : http.Response {
171
175
StatusCode : http .StatusBadRequest ,
172
176
Body : io .NopCloser (bytes .NewReader (errBytes )),
173
177
},
174
- }
175
- // when
176
- success , resolutionError := NewOutboundResolver (client ).resolveSingle (context .Background (), "" , make (map [string ]interface {}))
178
+ }}
179
+ success , resolutionError := resolver .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
177
180
178
- // then
179
181
validateErrorCode (success , resolutionError , test .expectCode , t )
180
182
})
181
183
}
182
184
}
183
185
184
186
func TestFlagNotFound404 (t * testing.T ) {
185
- client := mockOutbound {
187
+ resolver := OutboundResolver { client : mockOutbound {
186
188
rsp : http.Response {
187
189
StatusCode : http .StatusNotFound ,
188
190
},
189
- }
190
- // when
191
- success , resolutionError := NewOutboundResolver (client ).resolveSingle (context .Background (), "" , make (map [string ]interface {}))
191
+ }}
192
+ success , resolutionError := resolver .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
192
193
193
- // then
194
194
validateErrorCode (success , resolutionError , of .FlagNotFoundCode , t )
195
195
}
196
196
@@ -225,29 +225,24 @@ func Test429(t *testing.T) {
225
225
}
226
226
}
227
227
228
- client := mockOutbound {
229
- rsp : response ,
230
- }
228
+ resolver := OutboundResolver {client : mockOutbound {rsp : response }}
229
+ success , resolutionError := resolver .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
231
230
232
- // when
233
- success , resolutionError := NewOutboundResolver (client ).resolveSingle (context .Background (), "" , make (map [string ]interface {}))
234
231
validateErrorCode (success , resolutionError , of .GeneralCode , t )
235
232
})
236
233
}
237
234
}
238
235
239
236
func TestEvaluationError5xx (t * testing.T ) {
240
237
t .Run ("without body" , func (t * testing.T ) {
241
- client := mockOutbound {
238
+ resolver := OutboundResolver { client : mockOutbound {
242
239
rsp : http.Response {
243
240
StatusCode : http .StatusInternalServerError ,
244
241
Body : io .NopCloser (bytes .NewReader ([]byte {})),
245
242
},
246
- }
247
- // when
248
- success , resolutionError := NewOutboundResolver (client ).resolveSingle (context .Background (), "" , make (map [string ]interface {}))
243
+ }}
244
+ success , resolutionError := resolver .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
249
245
250
- // then
251
246
validateErrorCode (success , resolutionError , of .GeneralCode , t )
252
247
})
253
248
@@ -257,30 +252,26 @@ func TestEvaluationError5xx(t *testing.T) {
257
252
t .Fatal (err )
258
253
}
259
254
260
- client := mockOutbound {
255
+ resolver := OutboundResolver { client : mockOutbound {
261
256
rsp : http.Response {
262
257
StatusCode : http .StatusInternalServerError ,
263
258
Body : io .NopCloser (bytes .NewReader (errorBytes )),
264
259
},
265
- }
266
- // when
267
- success , resolutionError := NewOutboundResolver (client ).resolveSingle (context .Background (), "" , make (map [string ]interface {}))
260
+ }}
261
+ success , resolutionError := resolver .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
268
262
269
- // then
270
263
validateErrorCode (success , resolutionError , of .GeneralCode , t )
271
264
})
272
265
273
266
t .Run ("with invalid body" , func (t * testing.T ) {
274
- client := mockOutbound {
267
+ resolver := OutboundResolver { client : mockOutbound {
275
268
rsp : http.Response {
276
269
StatusCode : http .StatusInternalServerError ,
277
270
Body : io .NopCloser (bytes .NewReader ([]byte ("some error" ))),
278
271
},
279
- }
280
- // when
281
- success , resolutionError := NewOutboundResolver (client ).resolveSingle (context .Background (), "" , make (map [string ]interface {}))
272
+ }}
273
+ success , resolutionError := resolver .resolveSingle (context .Background (), "" , make (map [string ]interface {}))
282
274
283
- // then
284
275
validateErrorCode (success , resolutionError , of .GeneralCode , t )
285
276
})
286
277
}
0 commit comments