Skip to content

Commit

Permalink
adding auth token in header for listbalances client method. fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyoonie9 committed Jul 25, 2024
1 parent 3c04496 commit d9951c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/src/http_client/tbdex_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,17 @@ class TbdexHttpClient {
}

static Future<List<Balance>> listBalances(
BearerDid did,
String pfiDid,
) async {
final requestToken = await _generateRequestToken(did, pfiDid);
final headers = {'Authorization': 'Bearer $requestToken'};
final pfiServiceEndpoint = await _getPfiServiceEndpoint(pfiDid);
final url = Uri.parse('$pfiServiceEndpoint/balances/');

http.Response response;
try {
response = await _client.get(url);
response = await _client.get(url, headers: headers);

if (response.statusCode != 200) {
throw ResponseError(
Expand Down
19 changes: 14 additions & 5 deletions test/http_client/tbdex_http_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,37 @@ void main() async {

test('can list balances', () async {
when(
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/balances/')),
() => mockHttpClient.get(
Uri.parse('$pfiServiceEndpoint/balances/'),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(TestData.listBalancesResponse(), 200),
);

final response = await TbdexHttpClient.listBalances(pfiDid);
final response = await TbdexHttpClient.listBalances(TestData.aliceDid, pfiDid);
expect(response.length, 1);

verify(
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/balances/')),
() => mockHttpClient.get(
Uri.parse('$pfiServiceEndpoint/balances/'),
headers: any(named: 'headers'),
),
).called(1);
});

test('list balances throws ResponseError', () async {
when(
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/balances/')),
() => mockHttpClient.get(
Uri.parse('$pfiServiceEndpoint/balances/'),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response('Error', 400),
);

expect(
() async => TbdexHttpClient.listBalances(pfiDid),
() async => TbdexHttpClient.listBalances(TestData.aliceDid, pfiDid),
throwsA(isA<ResponseError>()),
);
});
Expand Down

0 comments on commit d9951c5

Please sign in to comment.