-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmain.dart
126 lines (105 loc) · 3.42 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import 'package:test/test.dart';
void main() async {
late Dio dio;
late DioAdapter dioAdapter;
Response<dynamic> response;
group('Accounts', () {
const baseUrl = 'https://example.com';
const userCredentials = <String, dynamic>{
'email': '[email protected]',
'password': 'password',
};
setUp(() {
//// Exact body check
// dio = Dio(BaseOptions(contentType: Headers.jsonContentType));
// dioAdapter = DioAdapter(
// dio: dio,
// matcher: const FullHttpRequestMatcher(needsExactBody: true),
// );
dio = Dio(BaseOptions(baseUrl: baseUrl));
dioAdapter = DioAdapter(
dio: dio,
// [FullHttpRequestMatcher] is a default matcher class
// (which actually means you haven't to pass it manually) that matches entire URL.
//
// Use [UrlRequestMatcher] for matching request based on the path of the URL.
//
// Or create your own http-request matcher via extending your class from [HttpRequestMatcher].
// See -> issue:[124] & pr:[125]
matcher: const FullHttpRequestMatcher(),
);
});
test('signs up user', () async {
const route = '/signup';
dioAdapter.onPost(
route,
(server) => server.reply(
201,
null,
// Adds one-sec delay to reply method.
// Basically, I'd wait for one second before returning reply data.
// See -> issue:[106] & pr:[126]
delay: const Duration(seconds: 1),
),
data: userCredentials,
);
// Returns a response with 201 Created success status response code.
response = await dio.post(route, data: userCredentials);
expect(response.statusCode, 201);
});
test('signs in user and fetches account information', () async {
const signInRoute = '/signin';
const accountRoute = '/account';
const accessToken = <String, dynamic>{
'token': 'ACCESS_TOKEN',
};
final headers = <String, dynamic>{
'Authentication': 'Bearer $accessToken',
};
const userInformation = <String, dynamic>{
'id': 1,
'email': '[email protected]',
'password': 'password',
'email_verified': false,
};
dioAdapter
..onPost(
signInRoute,
(server) => server.throws(
401,
DioException(
requestOptions: RequestOptions(
path: signInRoute,
),
),
),
)
..onPost(
signInRoute,
(server) => server.reply(200, accessToken),
data: userCredentials,
)
..onGet(
accountRoute,
(server) => server.reply(200, userInformation),
headers: headers,
);
// Throws without user credentials.
expect(
() async => await dio.post(signInRoute),
throwsA(isA<DioException>()),
);
// Returns an access token if user credentials are provided.
response = await dio.post(signInRoute, data: userCredentials);
expect(response.data, accessToken);
// Returns user information if an access token is provided in headers.
response = await dio.get(
accountRoute,
options: Options(headers: headers),
);
expect(response.data, userInformation);
});
});
}