@@ -3,6 +3,7 @@ package http_fetcher
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "fmt"
6
7
"net/http"
7
8
"net/http/httptest"
8
9
"strings"
@@ -134,16 +135,25 @@ func TestFetchAccount(t *testing.T) {
134
135
fetcher , close := newTestAccountFetcher (t , []string {"acc-1" })
135
136
defer close ()
136
137
137
- account , errs := fetcher .FetchAccount (context .Background (), "acc-1" )
138
+ account , errs := fetcher .FetchAccount (context .Background (), json . RawMessage ( `{"disabled":true}` ), "acc-1" )
138
139
assert .Empty (t , errs , "Unexpected error fetching existing account" )
139
- assert .JSONEq (t , `"acc-1"` , string (account ), "Unexpected account data fetching existing account" )
140
+ assert .JSONEq (t , `{"disabled": true, "id":"acc-1"}` , string (account ), "Unexpected account data fetching existing account" )
141
+ }
142
+
143
+ func TestAccountMergeError (t * testing.T ) {
144
+ fetcher , close := newTestAccountFetcher (t , []string {"acc-1" })
145
+ defer close ()
146
+
147
+ _ , errs := fetcher .FetchAccount (context .Background (), json .RawMessage (`{"disabled"}` ), "acc-1" )
148
+ assert .Error (t , errs [0 ])
149
+ assert .Equal (t , fmt .Errorf ("Invalid JSON Document" ), errs [0 ])
140
150
}
141
151
142
152
func TestFetchAccountNoData (t * testing.T ) {
143
153
fetcher , close := newFetcherBrokenBackend ()
144
154
defer close ()
145
155
146
- unknownAccount , errs := fetcher .FetchAccount (context .Background (), "unknown-acc" )
156
+ unknownAccount , errs := fetcher .FetchAccount (context .Background (), json . RawMessage ( `{disabled":true}` ), "unknown-acc" )
147
157
assert .NotEmpty (t , errs , "Retrieving unknown account should return error" )
148
158
assert .Nil (t , unknownAccount , "Retrieving unknown account should return nil json.RawMessage" )
149
159
}
@@ -152,7 +162,7 @@ func TestFetchAccountNoIDProvided(t *testing.T) {
152
162
fetcher , close := newTestAccountFetcher (t , nil )
153
163
defer close ()
154
164
155
- account , errs := fetcher .FetchAccount (context .Background (), "" )
165
+ account , errs := fetcher .FetchAccount (context .Background (), json . RawMessage ( `{disabled":true}` ), "" )
156
166
assert .Len (t , errs , 1 , "Fetching account with empty id should error" )
157
167
assert .Nil (t , account , "Fetching account with empty id should return nil" )
158
168
}
@@ -233,12 +243,12 @@ func newHandler(t *testing.T, expectReqIDs []string, expectImpIDs []string, json
233
243
}
234
244
235
245
func newTestAccountFetcher (t * testing.T , expectAccIDs []string ) (fetcher * HttpFetcher , closer func ()) {
236
- handler := newAccountHandler (t , expectAccIDs , jsonifyID )
246
+ handler := newAccountHandler (t , expectAccIDs )
237
247
server := httptest .NewServer (http .HandlerFunc (handler ))
238
248
return NewFetcher (server .Client (), server .URL ), server .Close
239
249
}
240
250
241
- func newAccountHandler (t * testing.T , expectAccIDs []string , jsonifier func ( string ) json. RawMessage ) func (w http.ResponseWriter , r * http.Request ) {
251
+ func newAccountHandler (t * testing.T , expectAccIDs []string ) func (w http.ResponseWriter , r * http.Request ) {
242
252
return func (w http.ResponseWriter , r * http.Request ) {
243
253
query := r .URL .Query ()
244
254
gotAccIDs := richSplit (query .Get ("account-ids" ))
@@ -249,7 +259,7 @@ func newAccountHandler(t *testing.T, expectAccIDs []string, jsonifier func(strin
249
259
250
260
for _ , accID := range gotAccIDs {
251
261
if accID != "" {
252
- accIDResponse [accID ] = jsonifier ( accID )
262
+ accIDResponse [accID ] = json . RawMessage ( `{"id":"` + accID + `"}` )
253
263
}
254
264
}
255
265
0 commit comments