@@ -2,16 +2,14 @@ import 'dart:io';
2
2
3
3
import 'package:chopper/chopper.dart' ;
4
4
import 'package:http/http.dart' as http;
5
- import 'package:mockito/annotations.dart' ;
6
- import 'package:mockito/mockito.dart' ;
5
+ import 'package:mocktail/mocktail.dart' ;
7
6
import 'package:oauth2/oauth2.dart' ;
8
7
import 'package:oauth_chopper/src/oauth_authenticator.dart' ;
9
8
import 'package:oauth_chopper/src/oauth_chopper.dart' ;
10
9
import 'package:oauth_chopper/src/oauth_token.dart' ;
11
10
import 'package:test/test.dart' ;
12
11
13
- @GenerateMocks ([OAuthChopper ])
14
- import 'oauth_authenticator_test.mocks.dart' ;
12
+ class MockOAuthChopper extends Mock implements OAuthChopper {}
15
13
16
14
void main () {
17
15
final mockOAuthChopper = MockOAuthChopper ();
@@ -22,16 +20,16 @@ void main() {
22
20
expiration: DateTime (2022 , 9 , 1 ),
23
21
),
24
22
);
25
- final testRequest = Request ('GET' , Uri (host : 'test' ), Uri (host : 'test' ));
23
+ final testRequest = Request ('GET' , Uri . parse ( 'test' ), Uri . parse ( 'test' ));
26
24
final unauthorizedResponse =
27
25
Response (http.Response ('body' , HttpStatus .unauthorized), 'body' );
28
26
final authorizedResponse =
29
27
Response (http.Response ('body' , HttpStatus .accepted), 'body' );
30
28
31
29
test ('only refresh on unauthorized and token' , () async {
32
30
// arrange
33
- when (mockOAuthChopper.refresh ()).thenAnswer ((_) async => testToken);
34
- when (mockOAuthChopper.token).thenAnswer ((_) async => testToken);
31
+ when (() => mockOAuthChopper.refresh ()).thenAnswer ((_) async => testToken);
32
+ when (() => mockOAuthChopper.token).thenAnswer ((_) async => testToken);
35
33
final authenticator = OAuthAuthenticator (mockOAuthChopper, null );
36
34
final expected = {'Authorization' : 'Bearer token' };
37
35
@@ -40,59 +38,59 @@ void main() {
40
38
await authenticator.authenticate (testRequest, unauthorizedResponse);
41
39
42
40
// assert
43
- verify (mockOAuthChopper.refresh ()).called (1 );
41
+ verify (() => mockOAuthChopper.refresh ()).called (1 );
44
42
expect (result? .headers, expected);
45
43
});
46
44
47
45
test ("Don't refresh on authorized" , () async {
48
46
// arrange
49
- when (mockOAuthChopper.refresh ()).thenAnswer ((_) async => testToken);
50
- when (mockOAuthChopper.token).thenAnswer ((_) async => testToken);
47
+ when (() => mockOAuthChopper.refresh ()).thenAnswer ((_) async => testToken);
48
+ when (() => mockOAuthChopper.token).thenAnswer ((_) async => testToken);
51
49
final authenticator = OAuthAuthenticator (mockOAuthChopper, null );
52
50
53
51
// act
54
52
final result =
55
53
await authenticator.authenticate (testRequest, authorizedResponse);
56
54
57
55
// assert
58
- verifyNever (mockOAuthChopper.refresh ());
56
+ verifyNever (() => mockOAuthChopper.refresh ());
59
57
expect (result, null );
60
58
});
61
59
62
60
test ("Don't refresh on token not available" , () async {
63
61
// arrange
64
- when (mockOAuthChopper.refresh ()).thenAnswer ((_) async => testToken);
65
- when (mockOAuthChopper.token).thenAnswer ((_) async => null );
62
+ when (() => mockOAuthChopper.refresh ()).thenAnswer ((_) async => testToken);
63
+ when (() => mockOAuthChopper.token).thenAnswer ((_) async => null );
66
64
final authenticator = OAuthAuthenticator (mockOAuthChopper, null );
67
65
68
66
// act
69
67
final result =
70
68
await authenticator.authenticate (testRequest, unauthorizedResponse);
71
69
72
70
// assert
73
- verifyNever (mockOAuthChopper.refresh ());
71
+ verifyNever (() => mockOAuthChopper.refresh ());
74
72
expect (result, null );
75
73
});
76
74
77
75
test ("Don't add headers on failed refresh" , () async {
78
76
// arrange
79
- when (mockOAuthChopper.refresh ()).thenAnswer ((_) async => null );
80
- when (mockOAuthChopper.token).thenAnswer ((_) async => testToken);
77
+ when (() => mockOAuthChopper.refresh ()).thenAnswer ((_) async => null );
78
+ when (() => mockOAuthChopper.token).thenAnswer ((_) async => testToken);
81
79
final authenticator = OAuthAuthenticator (mockOAuthChopper, null );
82
80
83
81
// act
84
82
final result =
85
83
await authenticator.authenticate (testRequest, unauthorizedResponse);
86
84
87
85
// assert
88
- verify (mockOAuthChopper.refresh ()).called (1 );
86
+ verify (() => mockOAuthChopper.refresh ()).called (1 );
89
87
expect (result, null );
90
88
});
91
89
92
90
test ("Exception thrown if onError is null" , () async {
93
91
// arrange
94
- when (mockOAuthChopper.refresh ()).thenThrow (FormatException ('failed' ));
95
- when (mockOAuthChopper.token).thenAnswer ((_) async => testToken);
92
+ when (() => mockOAuthChopper.refresh ()).thenThrow (FormatException ('failed' ));
93
+ when (() => mockOAuthChopper.token).thenAnswer ((_) async => testToken);
96
94
final authenticator = OAuthAuthenticator (mockOAuthChopper, null );
97
95
98
96
// act
@@ -106,8 +104,8 @@ void main() {
106
104
test ("Exception not thrown if onError is supplied" , () async {
107
105
// arrange
108
106
FormatException ? result;
109
- when (mockOAuthChopper.refresh ()).thenThrow (FormatException ('failed' ));
110
- when (mockOAuthChopper.token).thenAnswer ((_) async => testToken);
107
+ when (() => mockOAuthChopper.refresh ()).thenThrow (FormatException ('failed' ));
108
+ when (() => mockOAuthChopper.token).thenAnswer ((_) async => testToken);
111
109
final authenticator = OAuthAuthenticator (
112
110
mockOAuthChopper, (e, s) => result = e as FormatException );
113
111
0 commit comments