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

Commit

Permalink
feat: add response error tests (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanwlee authored Jun 7, 2024
1 parent 4f12240 commit 47402b9
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/src/http_client/tbdex_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class TbdexHttpClient {
);
}
} on Exception catch (e) {
if (e is ResponseError) rethrow;

throw RequestError(
message: 'failed to send get exchange request',
url: url.toString(),
Expand Down Expand Up @@ -95,6 +97,8 @@ class TbdexHttpClient {
);
}
} on Exception catch (e) {
if (e is ResponseError) rethrow;

throw RequestError(
message: 'failed to send list exchange request',
url: url.toString(),
Expand Down Expand Up @@ -136,6 +140,8 @@ class TbdexHttpClient {
);
}
} on Exception catch (e) {
if (e is ResponseError) rethrow;

throw RequestError(
message: 'failed to send list offerings request',
url: url.toString(),
Expand Down Expand Up @@ -227,6 +233,8 @@ class TbdexHttpClient {
);
}
} on Exception catch (e) {
if (e is ResponseError) rethrow;

throw RequestError(
message: exchangeId != null
? 'failed to send create exchange request'
Expand Down
113 changes: 112 additions & 1 deletion test/http_client/tbdex_http_client_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:http/http.dart' as http;

import 'package:mocktail/mocktail.dart';
import 'package:tbdex/src/http_client/exceptions/http_exceptions.dart';
import 'package:tbdex/src/http_client/tbdex_http_client.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -51,6 +51,26 @@ void main() async {
).called(1);
});

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

expect(
() async => TbdexHttpClient.getExchange(
TestData.aliceDid,
pfiDid,
'1234',
),
throwsA(isA<ResponseError>()),
);
});

test('can list exchanges', () async {
when(
() => mockHttpClient.get(
Expand All @@ -73,6 +93,22 @@ void main() async {
).called(1);
});

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

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

test('can list offerings', () async {
when(
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/offerings/')),
Expand All @@ -88,6 +124,19 @@ void main() async {
).called(1);
});

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

expect(
() async => await TbdexHttpClient.listOfferings(pfiDid),
throwsA(isA<ResponseError>()),
);
});

test('can create exchange', () async {
final rfq = TestData.getRfq(to: pfiDid);
await rfq.sign(TestData.aliceDid);
Expand All @@ -113,6 +162,26 @@ void main() async {
).called(1);
});

test('create exchange throws ResponseError', () async {
final rfq = TestData.getRfq(to: pfiDid);
await rfq.sign(TestData.aliceDid);

when(
() => mockHttpClient.post(
Uri.parse('$pfiServiceEndpoint/exchanges'),
headers: any(named: 'headers'),
body: TestData.getCreateExchangeRequest(rfq, replyTo: 'reply_to'),
),
).thenAnswer(
(_) async => http.Response('Error', 400),
);

expect(
() async => TbdexHttpClient.createExchange(rfq, replyTo: 'reply_to'),
throwsA(isA<ResponseError>()),
);
});

test('can submit order', () async {
final order = TestData.getOrder(to: pfiDid);
final exchangeId = order.metadata.exchangeId;
Expand All @@ -139,6 +208,27 @@ void main() async {
).called(1);
});

test('submit order throws ResponseError', () async {
final order = TestData.getOrder(to: pfiDid);
final exchangeId = order.metadata.exchangeId;
await order.sign(TestData.aliceDid);

when(
() => mockHttpClient.put(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitOrderRequest(order),
),
).thenAnswer(
(_) async => http.Response('Error', 400),
);

expect(
() async => TbdexHttpClient.submitOrder(order),
throwsA(isA<ResponseError>()),
);
});

test('can submit close', () async {
final close = TestData.getClose(to: pfiDid);
final exchangeId = close.metadata.exchangeId;
Expand All @@ -164,5 +254,26 @@ void main() async {
),
).called(1);
});

test('submit close throws ResponseError', () async {
final close = TestData.getClose(to: pfiDid);
final exchangeId = close.metadata.exchangeId;
await close.sign(TestData.aliceDid);

when(
() => mockHttpClient.put(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitCloseRequest(close),
),
).thenAnswer(
(_) async => http.Response('Error', 400),
);

expect(
() async => TbdexHttpClient.submitClose(close),
throwsA(isA<ResponseError>()),
);
});
});
}

0 comments on commit 47402b9

Please sign in to comment.