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

Commit 47402b9

Browse files
authored
feat: add response error tests (#40)
1 parent 4f12240 commit 47402b9

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

lib/src/http_client/tbdex_http_client.dart

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class TbdexHttpClient {
5353
);
5454
}
5555
} on Exception catch (e) {
56+
if (e is ResponseError) rethrow;
57+
5658
throw RequestError(
5759
message: 'failed to send get exchange request',
5860
url: url.toString(),
@@ -95,6 +97,8 @@ class TbdexHttpClient {
9597
);
9698
}
9799
} on Exception catch (e) {
100+
if (e is ResponseError) rethrow;
101+
98102
throw RequestError(
99103
message: 'failed to send list exchange request',
100104
url: url.toString(),
@@ -136,6 +140,8 @@ class TbdexHttpClient {
136140
);
137141
}
138142
} on Exception catch (e) {
143+
if (e is ResponseError) rethrow;
144+
139145
throw RequestError(
140146
message: 'failed to send list offerings request',
141147
url: url.toString(),
@@ -227,6 +233,8 @@ class TbdexHttpClient {
227233
);
228234
}
229235
} on Exception catch (e) {
236+
if (e is ResponseError) rethrow;
237+
230238
throw RequestError(
231239
message: exchangeId != null
232240
? 'failed to send create exchange request'

test/http_client/tbdex_http_client_test.dart

+112-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:http/http.dart' as http;
2-
32
import 'package:mocktail/mocktail.dart';
3+
import 'package:tbdex/src/http_client/exceptions/http_exceptions.dart';
44
import 'package:tbdex/src/http_client/tbdex_http_client.dart';
55
import 'package:test/test.dart';
66

@@ -51,6 +51,26 @@ void main() async {
5151
).called(1);
5252
});
5353

54+
test('get exchange throws ResponseError', () async {
55+
when(
56+
() => mockHttpClient.get(
57+
Uri.parse('$pfiServiceEndpoint/exchanges/1234'),
58+
headers: any(named: 'headers'),
59+
),
60+
).thenAnswer(
61+
(_) async => http.Response('Error', 400),
62+
);
63+
64+
expect(
65+
() async => TbdexHttpClient.getExchange(
66+
TestData.aliceDid,
67+
pfiDid,
68+
'1234',
69+
),
70+
throwsA(isA<ResponseError>()),
71+
);
72+
});
73+
5474
test('can list exchanges', () async {
5575
when(
5676
() => mockHttpClient.get(
@@ -73,6 +93,22 @@ void main() async {
7393
).called(1);
7494
});
7595

96+
test('list exchanges throws ResponseError', () async {
97+
when(
98+
() => mockHttpClient.get(
99+
Uri.parse('$pfiServiceEndpoint/exchanges/'),
100+
headers: any(named: 'headers'),
101+
),
102+
).thenAnswer(
103+
(_) async => http.Response('Error', 400),
104+
);
105+
106+
expect(
107+
() async => TbdexHttpClient.listExchanges(TestData.aliceDid, pfiDid),
108+
throwsA(isA<ResponseError>()),
109+
);
110+
});
111+
76112
test('can list offerings', () async {
77113
when(
78114
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/offerings/')),
@@ -88,6 +124,19 @@ void main() async {
88124
).called(1);
89125
});
90126

127+
test('list offerings throws ResponseError', () async {
128+
when(
129+
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/offerings/')),
130+
).thenAnswer(
131+
(_) async => http.Response('Error', 400),
132+
);
133+
134+
expect(
135+
() async => await TbdexHttpClient.listOfferings(pfiDid),
136+
throwsA(isA<ResponseError>()),
137+
);
138+
});
139+
91140
test('can create exchange', () async {
92141
final rfq = TestData.getRfq(to: pfiDid);
93142
await rfq.sign(TestData.aliceDid);
@@ -113,6 +162,26 @@ void main() async {
113162
).called(1);
114163
});
115164

165+
test('create exchange throws ResponseError', () async {
166+
final rfq = TestData.getRfq(to: pfiDid);
167+
await rfq.sign(TestData.aliceDid);
168+
169+
when(
170+
() => mockHttpClient.post(
171+
Uri.parse('$pfiServiceEndpoint/exchanges'),
172+
headers: any(named: 'headers'),
173+
body: TestData.getCreateExchangeRequest(rfq, replyTo: 'reply_to'),
174+
),
175+
).thenAnswer(
176+
(_) async => http.Response('Error', 400),
177+
);
178+
179+
expect(
180+
() async => TbdexHttpClient.createExchange(rfq, replyTo: 'reply_to'),
181+
throwsA(isA<ResponseError>()),
182+
);
183+
});
184+
116185
test('can submit order', () async {
117186
final order = TestData.getOrder(to: pfiDid);
118187
final exchangeId = order.metadata.exchangeId;
@@ -139,6 +208,27 @@ void main() async {
139208
).called(1);
140209
});
141210

211+
test('submit order throws ResponseError', () async {
212+
final order = TestData.getOrder(to: pfiDid);
213+
final exchangeId = order.metadata.exchangeId;
214+
await order.sign(TestData.aliceDid);
215+
216+
when(
217+
() => mockHttpClient.put(
218+
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
219+
headers: any(named: 'headers'),
220+
body: TestData.getSubmitOrderRequest(order),
221+
),
222+
).thenAnswer(
223+
(_) async => http.Response('Error', 400),
224+
);
225+
226+
expect(
227+
() async => TbdexHttpClient.submitOrder(order),
228+
throwsA(isA<ResponseError>()),
229+
);
230+
});
231+
142232
test('can submit close', () async {
143233
final close = TestData.getClose(to: pfiDid);
144234
final exchangeId = close.metadata.exchangeId;
@@ -164,5 +254,26 @@ void main() async {
164254
),
165255
).called(1);
166256
});
257+
258+
test('submit close throws ResponseError', () async {
259+
final close = TestData.getClose(to: pfiDid);
260+
final exchangeId = close.metadata.exchangeId;
261+
await close.sign(TestData.aliceDid);
262+
263+
when(
264+
() => mockHttpClient.put(
265+
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
266+
headers: any(named: 'headers'),
267+
body: TestData.getSubmitCloseRequest(close),
268+
),
269+
).thenAnswer(
270+
(_) async => http.Response('Error', 400),
271+
);
272+
273+
expect(
274+
() async => TbdexHttpClient.submitClose(close),
275+
throwsA(isA<ResponseError>()),
276+
);
277+
});
167278
});
168279
}

0 commit comments

Comments
 (0)