@@ -6,18 +6,18 @@ import 'package:flutter/services.dart';
6
6
import 'package:flutter_test/flutter_test.dart' ;
7
7
import 'package:instabug_flutter/instabug_custom_http_client.dart' ;
8
8
import 'package:instabug_flutter/utils/http_client_logger.dart' ;
9
+ import 'package:mockito/annotations.dart' ;
9
10
import 'package:mockito/mockito.dart' ;
10
11
11
- class MockHttpClient extends Mock implements HttpClient {}
12
+ import 'network_logger_test.mocks.dart' ;
12
13
13
14
class MockHttpClientRequest extends Mock implements HttpClientRequest {}
14
15
15
16
class MockHttpClientResponse extends Mock implements HttpClientResponse {}
16
17
17
- class MockHttpClientLogger extends Mock implements HttpClientLogger {}
18
-
19
18
class MockHttpClientCredentials extends Mock implements HttpClientCredentials {}
20
19
20
+ @GenerateMocks ([HttpClient , HttpClientLogger ])
21
21
void main () {
22
22
WidgetsFlutterBinding .ensureInitialized ();
23
23
@@ -47,7 +47,8 @@ void main() {
47
47
48
48
test ('expect instabug custom http client GET URL to return request and log' ,
49
49
() async {
50
- when < dynamic > (instabugCustomHttpClient.client.getUrl (any))
50
+ when < dynamic > (
51
+ (instabugCustomHttpClient.client as MockHttpClient ).getUrl (any))
51
52
.thenAnswer ((_) async => MockHttpClientRequest ());
52
53
53
54
final request = await instabugCustomHttpClient.getUrl (Uri .parse (url));
@@ -60,7 +61,8 @@ void main() {
60
61
61
62
test ('expect instabug custom http client GET to return request and log' ,
62
63
() async {
63
- when < dynamic > (instabugCustomHttpClient.client.get (any, any, any))
64
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
65
+ .get (any, any, any))
64
66
.thenAnswer ((_) async => MockHttpClientRequest ());
65
67
66
68
final request = await instabugCustomHttpClient.get (url, port, path);
@@ -74,7 +76,8 @@ void main() {
74
76
test (
75
77
'expect instabug custom http client DELETE URL to return request and log' ,
76
78
() async {
77
- when < dynamic > (instabugCustomHttpClient.client.deleteUrl (any))
79
+ when < dynamic > (
80
+ (instabugCustomHttpClient.client as MockHttpClient ).deleteUrl (any))
78
81
.thenAnswer ((_) async => MockHttpClientRequest ());
79
82
80
83
final request = await instabugCustomHttpClient.deleteUrl (Uri .parse (url));
@@ -87,7 +90,8 @@ void main() {
87
90
88
91
test ('expect instabug custom http client DELETE to return request and log' ,
89
92
() async {
90
- when < dynamic > (instabugCustomHttpClient.client.delete (any, any, any))
93
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
94
+ .delete (any, any, any))
91
95
.thenAnswer ((_) async => MockHttpClientRequest ());
92
96
93
97
final request = await instabugCustomHttpClient.delete (url, port, path);
@@ -100,7 +104,8 @@ void main() {
100
104
101
105
test ('expect instabug custom http client POST URL to return request and log' ,
102
106
() async {
103
- when < dynamic > (instabugCustomHttpClient.client.postUrl (any))
107
+ when < dynamic > (
108
+ (instabugCustomHttpClient.client as MockHttpClient ).postUrl (any))
104
109
.thenAnswer ((_) async => MockHttpClientRequest ());
105
110
106
111
final request = await instabugCustomHttpClient.postUrl (Uri .parse (url));
@@ -113,7 +118,8 @@ void main() {
113
118
114
119
test ('expect instabug custom http client POST to return request and log' ,
115
120
() async {
116
- when < dynamic > (instabugCustomHttpClient.client.post (any, any, any))
121
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
122
+ .post (any, any, any))
117
123
.thenAnswer ((_) async => MockHttpClientRequest ());
118
124
119
125
final request = await instabugCustomHttpClient.post (url, port, path);
@@ -126,7 +132,8 @@ void main() {
126
132
127
133
test ('expect instabug custom http client HEAD URL to return request and log' ,
128
134
() async {
129
- when < dynamic > (instabugCustomHttpClient.client.headUrl (any))
135
+ when < dynamic > (
136
+ (instabugCustomHttpClient.client as MockHttpClient ).headUrl (any))
130
137
.thenAnswer ((_) async => MockHttpClientRequest ());
131
138
132
139
final request = await instabugCustomHttpClient.headUrl (Uri .parse (url));
@@ -139,7 +146,8 @@ void main() {
139
146
140
147
test ('expect instabug custom http client HEAD to return request and log' ,
141
148
() async {
142
- when < dynamic > (instabugCustomHttpClient.client.head (any, any, any))
149
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
150
+ .head (any, any, any))
143
151
.thenAnswer ((_) async => MockHttpClientRequest ());
144
152
145
153
final request = await instabugCustomHttpClient.head (url, port, path);
@@ -152,7 +160,8 @@ void main() {
152
160
153
161
test ('expect instabug custom http client PATCH URL to return request and log' ,
154
162
() async {
155
- when < dynamic > (instabugCustomHttpClient.client.patchUrl (any))
163
+ when < dynamic > (
164
+ (instabugCustomHttpClient.client as MockHttpClient ).patchUrl (any))
156
165
.thenAnswer ((_) async => MockHttpClientRequest ());
157
166
158
167
final request = await instabugCustomHttpClient.patchUrl (Uri .parse (url));
@@ -165,7 +174,8 @@ void main() {
165
174
166
175
test ('expect instabug custom http client PATCH to return request and log' ,
167
176
() async {
168
- when < dynamic > (instabugCustomHttpClient.client.patch (any, any, any))
177
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
178
+ .patch (any, any, any))
169
179
.thenAnswer ((_) async => MockHttpClientRequest ());
170
180
171
181
final request = await instabugCustomHttpClient.patch (url, port, path);
@@ -178,7 +188,8 @@ void main() {
178
188
179
189
test ('expect instabug custom http client OPEN URL to return request and log' ,
180
190
() async {
181
- when < dynamic > (instabugCustomHttpClient.client.openUrl (any, any))
191
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
192
+ .openUrl (any, any))
182
193
.thenAnswer ((_) async => MockHttpClientRequest ());
183
194
184
195
final request =
@@ -192,7 +203,8 @@ void main() {
192
203
193
204
test ('expect instabug custom http client OPEN to return request and log' ,
194
205
() async {
195
- when < dynamic > (instabugCustomHttpClient.client.open (any, any, any, any))
206
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
207
+ .open (any, any, any, any))
196
208
.thenAnswer ((_) async => MockHttpClientRequest ());
197
209
198
210
final request = await instabugCustomHttpClient.open ('GET' , url, port, path);
@@ -205,7 +217,8 @@ void main() {
205
217
206
218
test ('expect instabug custom http client PUT URL to return request and log' ,
207
219
() async {
208
- when < dynamic > (instabugCustomHttpClient.client.putUrl (any))
220
+ when < dynamic > (
221
+ (instabugCustomHttpClient.client as MockHttpClient ).putUrl (any))
209
222
.thenAnswer ((_) async => MockHttpClientRequest ());
210
223
211
224
final request = await instabugCustomHttpClient.putUrl (Uri .parse (url));
@@ -218,7 +231,8 @@ void main() {
218
231
219
232
test ('expect instabug custom http client PUT to return request and log' ,
220
233
() async {
221
- when < dynamic > (instabugCustomHttpClient.client.put (any, any, any))
234
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
235
+ .put (any, any, any))
222
236
.thenAnswer ((_) async => MockHttpClientRequest ());
223
237
224
238
final request = await instabugCustomHttpClient.put (url, port, path);
@@ -231,7 +245,8 @@ void main() {
231
245
232
246
test ('expect instabug custom http client POST URL to return request and log' ,
233
247
() async {
234
- when < dynamic > (instabugCustomHttpClient.client.postUrl (any))
248
+ when < dynamic > (
249
+ (instabugCustomHttpClient.client as MockHttpClient ).postUrl (any))
235
250
.thenAnswer ((_) async => MockHttpClientRequest ());
236
251
237
252
final request = await instabugCustomHttpClient.postUrl (Uri .parse (url));
@@ -244,7 +259,8 @@ void main() {
244
259
245
260
test ('expect instabug custom http client POST to return request and log' ,
246
261
() async {
247
- when < dynamic > (instabugCustomHttpClient.client.post (any, any, any))
262
+ when < dynamic > ((instabugCustomHttpClient.client as MockHttpClient )
263
+ .post (any, any, any))
248
264
.thenAnswer ((_) async => MockHttpClientRequest ());
249
265
250
266
final request = await instabugCustomHttpClient.post (url, port, path);
@@ -257,19 +273,22 @@ void main() {
257
273
258
274
test ('expect instabug custom http client to get client autoUncompress' ,
259
275
() async {
260
- when (instabugCustomHttpClient.client.autoUncompress).thenReturn (true );
276
+ when ((instabugCustomHttpClient.client as MockHttpClient ).autoUncompress)
277
+ .thenReturn (true );
261
278
expect (instabugCustomHttpClient.autoUncompress, true );
262
279
});
263
280
264
281
test ('expect instabug custom http client to set client autoUncompress' ,
265
282
() async {
266
283
instabugCustomHttpClient.autoUncompress = false ;
267
- verify (instabugCustomHttpClient.client.autoUncompress = false ).called (1 );
284
+ verify ((instabugCustomHttpClient.client as MockHttpClient ).autoUncompress =
285
+ false )
286
+ .called (1 );
268
287
});
269
288
270
289
test ('expect instabug custom http client to get client connectionTimout' ,
271
290
() async {
272
- when (instabugCustomHttpClient.client.connectionTimeout)
291
+ when (( instabugCustomHttpClient.client as MockHttpClient ) .connectionTimeout)
273
292
.thenReturn (const Duration (seconds: 2 ));
274
293
expect (
275
294
instabugCustomHttpClient.connectionTimeout, const Duration (seconds: 2 ));
@@ -278,46 +297,53 @@ void main() {
278
297
test ('expect instabug custom http client to set client connectionTimout' ,
279
298
() async {
280
299
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 ))
283
302
.called (1 );
284
303
});
285
304
286
305
test ('expect instabug custom http client to get client idleTimeout' ,
287
306
() async {
288
- when (instabugCustomHttpClient.client.idleTimeout)
307
+ when (( instabugCustomHttpClient.client as MockHttpClient ) .idleTimeout)
289
308
.thenReturn (const Duration (seconds: 2 ));
290
309
expect (instabugCustomHttpClient.idleTimeout, const Duration (seconds: 2 ));
291
310
});
292
311
293
312
test ('expect instabug custom http client to set client idleTimeout' ,
294
313
() async {
295
314
instabugCustomHttpClient.idleTimeout = const Duration (seconds: 5 );
296
- verify (instabugCustomHttpClient.client.idleTimeout =
315
+ verify (( instabugCustomHttpClient.client as MockHttpClient ) .idleTimeout =
297
316
const Duration (seconds: 5 ))
298
317
.called (1 );
299
318
});
300
319
301
320
test ('expect instabug custom http client to get client maxConnectionsPerHost' ,
302
321
() async {
303
- when (instabugCustomHttpClient.client.maxConnectionsPerHost).thenReturn (2 );
322
+ when ((instabugCustomHttpClient.client as MockHttpClient )
323
+ .maxConnectionsPerHost)
324
+ .thenReturn (2 );
304
325
expect (instabugCustomHttpClient.maxConnectionsPerHost, 2 );
305
326
});
306
327
307
328
test ('expect instabug custom http client to set client maxConnectionsPerHost' ,
308
329
() async {
309
330
instabugCustomHttpClient.maxConnectionsPerHost = 5 ;
310
- verify (instabugCustomHttpClient.client.maxConnectionsPerHost = 5 ).called (1 );
331
+ verify ((instabugCustomHttpClient.client as MockHttpClient )
332
+ .maxConnectionsPerHost = 5 )
333
+ .called (1 );
311
334
});
312
335
313
336
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' );
315
339
expect (instabugCustomHttpClient.userAgent, '2' );
316
340
});
317
341
318
342
test ('expect instabug custom http client to set client userAgent' , () async {
319
343
instabugCustomHttpClient.userAgent = 'something' ;
320
- verify (instabugCustomHttpClient.client.userAgent = 'something' ).called (1 );
344
+ verify ((instabugCustomHttpClient.client as MockHttpClient ).userAgent =
345
+ 'something' )
346
+ .called (1 );
321
347
});
322
348
323
349
test ('expect instabug custom http client to call client addClientCredentials' ,
@@ -349,7 +375,8 @@ void main() {
349
375
final Future <bool > Function (Uri url, String scheme, String realm) f =
350
376
(Uri url, String scheme, String realm) async => true ;
351
377
instabugCustomHttpClient.authenticate = f;
352
- verify (instabugCustomHttpClient.client.authenticate = f).called (1 );
378
+ verify ((instabugCustomHttpClient.client as MockHttpClient ).authenticate = f)
379
+ .called (1 );
353
380
});
354
381
355
382
test ('expect instabug custom http client to set client authenticateProxy' ,
@@ -358,7 +385,9 @@ void main() {
358
385
String host, int port, String scheme, String realm) f =
359
386
(String host, int port, String scheme, String realm) async => true ;
360
387
instabugCustomHttpClient.authenticateProxy = f;
361
- verify (instabugCustomHttpClient.client.authenticateProxy = f).called (1 );
388
+ verify ((instabugCustomHttpClient.client as MockHttpClient )
389
+ .authenticateProxy = f)
390
+ .called (1 );
362
391
});
363
392
364
393
test (
@@ -367,24 +396,32 @@ void main() {
367
396
final bool Function (X509Certificate cert, String host, int port) f =
368
397
(X509Certificate cert, String host, int port) => true ;
369
398
instabugCustomHttpClient.badCertificateCallback = f;
370
- verify (instabugCustomHttpClient.client.badCertificateCallback = f)
399
+ verify ((instabugCustomHttpClient.client as MockHttpClient )
400
+ .badCertificateCallback = f)
371
401
.called (1 );
372
402
});
373
403
374
404
test ('expect instabug custom http client to call client close' , () async {
375
405
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 );
377
409
});
378
410
379
411
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))
381
414
.thenAnswer ((_) async => MockHttpClientRequest ());
382
415
383
416
for (int i = 0 ; i < 10000 ; i++ ) {
384
417
await instabugCustomHttpClient.getUrl (Uri .parse (url));
385
418
}
386
419
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 );
389
426
});
390
427
}
0 commit comments