Skip to content

Commit 0c2de28

Browse files
committed
Fix tests
1 parent 43883bd commit 0c2de28

File tree

3 files changed

+290
-40
lines changed

3 files changed

+290
-40
lines changed

pubspec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ dependencies:
1616
stack_trace: ^1.10.0
1717

1818
dev_dependencies:
19+
build_runner: ^2.0.3
1920
flutter_test:
2021
sdk: flutter
21-
mockito:
22+
mockito: ^5.0.7
2223

2324
flutter:
2425
plugin:
@@ -31,6 +32,4 @@ flutter:
3132

3233
environment:
3334
sdk: ">=2.12.0 <3.0.0"
34-
# Flutter versions prior to 1.12 did not support the
35-
# flutter.plugin.platforms map.
3635
flutter: ">=2.0.0 <3.0.0"

test/network_logger_test.dart

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import 'package:flutter/services.dart';
66
import 'package:flutter_test/flutter_test.dart';
77
import 'package:instabug_flutter/instabug_custom_http_client.dart';
88
import 'package:instabug_flutter/utils/http_client_logger.dart';
9+
import 'package:mockito/annotations.dart';
910
import 'package:mockito/mockito.dart';
1011

11-
class MockHttpClient extends Mock implements HttpClient {}
12+
import 'network_logger_test.mocks.dart';
1213

1314
class MockHttpClientRequest extends Mock implements HttpClientRequest {}
1415

1516
class MockHttpClientResponse extends Mock implements HttpClientResponse {}
1617

17-
class MockHttpClientLogger extends Mock implements HttpClientLogger {}
18-
1918
class MockHttpClientCredentials extends Mock implements HttpClientCredentials {}
2019

20+
@GenerateMocks([HttpClient, HttpClientLogger])
2121
void main() {
2222
WidgetsFlutterBinding.ensureInitialized();
2323

@@ -47,7 +47,8 @@ void main() {
4747

4848
test('expect instabug custom http client GET URL to return request and log',
4949
() async {
50-
when<dynamic>(instabugCustomHttpClient.client.getUrl(any))
50+
when<dynamic>(
51+
(instabugCustomHttpClient.client as MockHttpClient).getUrl(any))
5152
.thenAnswer((_) async => MockHttpClientRequest());
5253

5354
final request = await instabugCustomHttpClient.getUrl(Uri.parse(url));
@@ -60,7 +61,8 @@ void main() {
6061

6162
test('expect instabug custom http client GET to return request and log',
6263
() async {
63-
when<dynamic>(instabugCustomHttpClient.client.get(any, any, any))
64+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
65+
.get(any, any, any))
6466
.thenAnswer((_) async => MockHttpClientRequest());
6567

6668
final request = await instabugCustomHttpClient.get(url, port, path);
@@ -74,7 +76,8 @@ void main() {
7476
test(
7577
'expect instabug custom http client DELETE URL to return request and log',
7678
() async {
77-
when<dynamic>(instabugCustomHttpClient.client.deleteUrl(any))
79+
when<dynamic>(
80+
(instabugCustomHttpClient.client as MockHttpClient).deleteUrl(any))
7881
.thenAnswer((_) async => MockHttpClientRequest());
7982

8083
final request = await instabugCustomHttpClient.deleteUrl(Uri.parse(url));
@@ -87,7 +90,8 @@ void main() {
8790

8891
test('expect instabug custom http client DELETE to return request and log',
8992
() async {
90-
when<dynamic>(instabugCustomHttpClient.client.delete(any, any, any))
93+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
94+
.delete(any, any, any))
9195
.thenAnswer((_) async => MockHttpClientRequest());
9296

9397
final request = await instabugCustomHttpClient.delete(url, port, path);
@@ -100,7 +104,8 @@ void main() {
100104

101105
test('expect instabug custom http client POST URL to return request and log',
102106
() async {
103-
when<dynamic>(instabugCustomHttpClient.client.postUrl(any))
107+
when<dynamic>(
108+
(instabugCustomHttpClient.client as MockHttpClient).postUrl(any))
104109
.thenAnswer((_) async => MockHttpClientRequest());
105110

106111
final request = await instabugCustomHttpClient.postUrl(Uri.parse(url));
@@ -113,7 +118,8 @@ void main() {
113118

114119
test('expect instabug custom http client POST to return request and log',
115120
() async {
116-
when<dynamic>(instabugCustomHttpClient.client.post(any, any, any))
121+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
122+
.post(any, any, any))
117123
.thenAnswer((_) async => MockHttpClientRequest());
118124

119125
final request = await instabugCustomHttpClient.post(url, port, path);
@@ -126,7 +132,8 @@ void main() {
126132

127133
test('expect instabug custom http client HEAD URL to return request and log',
128134
() async {
129-
when<dynamic>(instabugCustomHttpClient.client.headUrl(any))
135+
when<dynamic>(
136+
(instabugCustomHttpClient.client as MockHttpClient).headUrl(any))
130137
.thenAnswer((_) async => MockHttpClientRequest());
131138

132139
final request = await instabugCustomHttpClient.headUrl(Uri.parse(url));
@@ -139,7 +146,8 @@ void main() {
139146

140147
test('expect instabug custom http client HEAD to return request and log',
141148
() async {
142-
when<dynamic>(instabugCustomHttpClient.client.head(any, any, any))
149+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
150+
.head(any, any, any))
143151
.thenAnswer((_) async => MockHttpClientRequest());
144152

145153
final request = await instabugCustomHttpClient.head(url, port, path);
@@ -152,7 +160,8 @@ void main() {
152160

153161
test('expect instabug custom http client PATCH URL to return request and log',
154162
() async {
155-
when<dynamic>(instabugCustomHttpClient.client.patchUrl(any))
163+
when<dynamic>(
164+
(instabugCustomHttpClient.client as MockHttpClient).patchUrl(any))
156165
.thenAnswer((_) async => MockHttpClientRequest());
157166

158167
final request = await instabugCustomHttpClient.patchUrl(Uri.parse(url));
@@ -165,7 +174,8 @@ void main() {
165174

166175
test('expect instabug custom http client PATCH to return request and log',
167176
() async {
168-
when<dynamic>(instabugCustomHttpClient.client.patch(any, any, any))
177+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
178+
.patch(any, any, any))
169179
.thenAnswer((_) async => MockHttpClientRequest());
170180

171181
final request = await instabugCustomHttpClient.patch(url, port, path);
@@ -178,7 +188,8 @@ void main() {
178188

179189
test('expect instabug custom http client OPEN URL to return request and log',
180190
() async {
181-
when<dynamic>(instabugCustomHttpClient.client.openUrl(any, any))
191+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
192+
.openUrl(any, any))
182193
.thenAnswer((_) async => MockHttpClientRequest());
183194

184195
final request =
@@ -192,7 +203,8 @@ void main() {
192203

193204
test('expect instabug custom http client OPEN to return request and log',
194205
() async {
195-
when<dynamic>(instabugCustomHttpClient.client.open(any, any, any, any))
206+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
207+
.open(any, any, any, any))
196208
.thenAnswer((_) async => MockHttpClientRequest());
197209

198210
final request = await instabugCustomHttpClient.open('GET', url, port, path);
@@ -205,7 +217,8 @@ void main() {
205217

206218
test('expect instabug custom http client PUT URL to return request and log',
207219
() async {
208-
when<dynamic>(instabugCustomHttpClient.client.putUrl(any))
220+
when<dynamic>(
221+
(instabugCustomHttpClient.client as MockHttpClient).putUrl(any))
209222
.thenAnswer((_) async => MockHttpClientRequest());
210223

211224
final request = await instabugCustomHttpClient.putUrl(Uri.parse(url));
@@ -218,7 +231,8 @@ void main() {
218231

219232
test('expect instabug custom http client PUT to return request and log',
220233
() async {
221-
when<dynamic>(instabugCustomHttpClient.client.put(any, any, any))
234+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
235+
.put(any, any, any))
222236
.thenAnswer((_) async => MockHttpClientRequest());
223237

224238
final request = await instabugCustomHttpClient.put(url, port, path);
@@ -231,7 +245,8 @@ void main() {
231245

232246
test('expect instabug custom http client POST URL to return request and log',
233247
() async {
234-
when<dynamic>(instabugCustomHttpClient.client.postUrl(any))
248+
when<dynamic>(
249+
(instabugCustomHttpClient.client as MockHttpClient).postUrl(any))
235250
.thenAnswer((_) async => MockHttpClientRequest());
236251

237252
final request = await instabugCustomHttpClient.postUrl(Uri.parse(url));
@@ -244,7 +259,8 @@ void main() {
244259

245260
test('expect instabug custom http client POST to return request and log',
246261
() async {
247-
when<dynamic>(instabugCustomHttpClient.client.post(any, any, any))
262+
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
263+
.post(any, any, any))
248264
.thenAnswer((_) async => MockHttpClientRequest());
249265

250266
final request = await instabugCustomHttpClient.post(url, port, path);
@@ -257,19 +273,22 @@ void main() {
257273

258274
test('expect instabug custom http client to get client autoUncompress',
259275
() async {
260-
when(instabugCustomHttpClient.client.autoUncompress).thenReturn(true);
276+
when((instabugCustomHttpClient.client as MockHttpClient).autoUncompress)
277+
.thenReturn(true);
261278
expect(instabugCustomHttpClient.autoUncompress, true);
262279
});
263280

264281
test('expect instabug custom http client to set client autoUncompress',
265282
() async {
266283
instabugCustomHttpClient.autoUncompress = false;
267-
verify(instabugCustomHttpClient.client.autoUncompress = false).called(1);
284+
verify((instabugCustomHttpClient.client as MockHttpClient).autoUncompress =
285+
false)
286+
.called(1);
268287
});
269288

270289
test('expect instabug custom http client to get client connectionTimout',
271290
() async {
272-
when(instabugCustomHttpClient.client.connectionTimeout)
291+
when((instabugCustomHttpClient.client as MockHttpClient).connectionTimeout)
273292
.thenReturn(const Duration(seconds: 2));
274293
expect(
275294
instabugCustomHttpClient.connectionTimeout, const Duration(seconds: 2));
@@ -278,46 +297,53 @@ void main() {
278297
test('expect instabug custom http client to set client connectionTimout',
279298
() async {
280299
instabugCustomHttpClient.connectionTimeout = const Duration(seconds: 5);
281-
verify(instabugCustomHttpClient.client.connectionTimeout =
282-
const Duration(seconds: 5))
300+
verify((instabugCustomHttpClient.client as MockHttpClient)
301+
.connectionTimeout = const Duration(seconds: 5))
283302
.called(1);
284303
});
285304

286305
test('expect instabug custom http client to get client idleTimeout',
287306
() async {
288-
when(instabugCustomHttpClient.client.idleTimeout)
307+
when((instabugCustomHttpClient.client as MockHttpClient).idleTimeout)
289308
.thenReturn(const Duration(seconds: 2));
290309
expect(instabugCustomHttpClient.idleTimeout, const Duration(seconds: 2));
291310
});
292311

293312
test('expect instabug custom http client to set client idleTimeout',
294313
() async {
295314
instabugCustomHttpClient.idleTimeout = const Duration(seconds: 5);
296-
verify(instabugCustomHttpClient.client.idleTimeout =
315+
verify((instabugCustomHttpClient.client as MockHttpClient).idleTimeout =
297316
const Duration(seconds: 5))
298317
.called(1);
299318
});
300319

301320
test('expect instabug custom http client to get client maxConnectionsPerHost',
302321
() async {
303-
when(instabugCustomHttpClient.client.maxConnectionsPerHost).thenReturn(2);
322+
when((instabugCustomHttpClient.client as MockHttpClient)
323+
.maxConnectionsPerHost)
324+
.thenReturn(2);
304325
expect(instabugCustomHttpClient.maxConnectionsPerHost, 2);
305326
});
306327

307328
test('expect instabug custom http client to set client maxConnectionsPerHost',
308329
() async {
309330
instabugCustomHttpClient.maxConnectionsPerHost = 5;
310-
verify(instabugCustomHttpClient.client.maxConnectionsPerHost = 5).called(1);
331+
verify((instabugCustomHttpClient.client as MockHttpClient)
332+
.maxConnectionsPerHost = 5)
333+
.called(1);
311334
});
312335

313336
test('expect instabug custom http client to get client userAgent', () async {
314-
when(instabugCustomHttpClient.client.userAgent).thenReturn('2');
337+
when((instabugCustomHttpClient.client as MockHttpClient).userAgent)
338+
.thenReturn('2');
315339
expect(instabugCustomHttpClient.userAgent, '2');
316340
});
317341

318342
test('expect instabug custom http client to set client userAgent', () async {
319343
instabugCustomHttpClient.userAgent = 'something';
320-
verify(instabugCustomHttpClient.client.userAgent = 'something').called(1);
344+
verify((instabugCustomHttpClient.client as MockHttpClient).userAgent =
345+
'something')
346+
.called(1);
321347
});
322348

323349
test('expect instabug custom http client to call client addClientCredentials',
@@ -349,7 +375,8 @@ void main() {
349375
final Future<bool> Function(Uri url, String scheme, String realm) f =
350376
(Uri url, String scheme, String realm) async => true;
351377
instabugCustomHttpClient.authenticate = f;
352-
verify(instabugCustomHttpClient.client.authenticate = f).called(1);
378+
verify((instabugCustomHttpClient.client as MockHttpClient).authenticate = f)
379+
.called(1);
353380
});
354381

355382
test('expect instabug custom http client to set client authenticateProxy',
@@ -358,7 +385,9 @@ void main() {
358385
String host, int port, String scheme, String realm) f =
359386
(String host, int port, String scheme, String realm) async => true;
360387
instabugCustomHttpClient.authenticateProxy = f;
361-
verify(instabugCustomHttpClient.client.authenticateProxy = f).called(1);
388+
verify((instabugCustomHttpClient.client as MockHttpClient)
389+
.authenticateProxy = f)
390+
.called(1);
362391
});
363392

364393
test(
@@ -367,24 +396,32 @@ void main() {
367396
final bool Function(X509Certificate cert, String host, int port) f =
368397
(X509Certificate cert, String host, int port) => true;
369398
instabugCustomHttpClient.badCertificateCallback = f;
370-
verify(instabugCustomHttpClient.client.badCertificateCallback = f)
399+
verify((instabugCustomHttpClient.client as MockHttpClient)
400+
.badCertificateCallback = f)
371401
.called(1);
372402
});
373403

374404
test('expect instabug custom http client to call client close', () async {
375405
instabugCustomHttpClient.close(force: true);
376-
verify(instabugCustomHttpClient.client.close(force: true)).called(1);
406+
verify((instabugCustomHttpClient.client as MockHttpClient)
407+
.close(force: true))
408+
.called(1);
377409
});
378410

379411
test('Stress test on GET URL method', () async {
380-
when<dynamic>(instabugCustomHttpClient.client.getUrl(any))
412+
when<dynamic>(
413+
(instabugCustomHttpClient.client as MockHttpClient).getUrl(any))
381414
.thenAnswer((_) async => MockHttpClientRequest());
382415

383416
for (int i = 0; i < 10000; i++) {
384417
await instabugCustomHttpClient.getUrl(Uri.parse(url));
385418
}
386419

387-
verify(instabugCustomHttpClient.logger.onRequest(any)).called(10000);
388-
verify(instabugCustomHttpClient.logger.onResponse(any, any)).called(10000);
420+
verify((instabugCustomHttpClient.logger as MockHttpClientLogger)
421+
.onRequest(any))
422+
.called(10000);
423+
verify((instabugCustomHttpClient.logger as MockHttpClientLogger)
424+
.onResponse(any, any))
425+
.called(10000);
389426
});
390427
}

0 commit comments

Comments
 (0)