Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

adding auth token in header for listbalances client method. fixed tests #54

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading