-
Notifications
You must be signed in to change notification settings - Fork 0
/
dio_test.dart
337 lines (318 loc) · 10.7 KB
/
dio_test.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import 'dart:async';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:test/test.dart';
class MyTransformer extends DefaultTransformer {
@override
Future<String> transformRequest(RequestOptions options) async {
if (options.data is List<String>) {
throw new DioError(message: "Can't send List to sever directly");
} else {
return super.transformRequest(options);
}
}
/// The [Options] doesn't contain the cookie info. we add the cookie
/// info to [Options.extra], and you can retrieve it in [ResponseInterceptor]
/// and [Response] with `response.request.extra["cookies"]`.
@override
Future transformResponse(RequestOptions options, ResponseBody response) async {
options.extra["xx"] = "extra";
return super.transformResponse(options, response);
}
}
void main() {
const BASE_URL = "http://www.dtworkroom.com/doris/1/2.0.0/";
group('lan', () {
test("lan", () {
var list = [""];
assert(list is List<String>);
assert(!(list is List<int>));
});
});
group('restful', () {
Dio dio;
setUp(() {
dio = new Dio();
dio.options.baseUrl = BASE_URL;
dio.options.headers = {'User-Agent': 'dartisan', 'XX': '8'};
});
test('test', () async {
Response response;
response = await dio.get("/test", queryParameters: {"id": '12', "name": "wendu"});
expect(response.data["errCode"], 0);
response = await dio.post("/test");
expect(response.data["errCode"], 0);
response = await dio.put("/test");
expect(response.data["errCode"], 0);
response = await dio.delete("/test");
expect(response.data["errCode"], 0);
response = await dio.patch("/test", data: {"xx": "你好"});
expect(response.data["errCode"], 0);
expect(response.headers != null, true);
try {
// Response response = await dio.head("/test");
Response response = await dio.head("http://www.dtworkroom.com:80");
expect(response.data["errCode"], 0);
} on DioError catch (e) {
assert(e.response != null &&
e.type == DioErrorType.RESPONSE &&
e.response.statusCode == 403);
}
});
});
group('download', () {
test("test", () async {
var dio = new Dio();
var url = "https://flutter.io/assets/flutter-lockup-4cb0ee072ab312e59784d9fbf4fb7ad42688a7fdaea1270ccf6bbf4f34b7e03f.svg";
await dio.download(url, "./example/flutter.svg",
options: Options(headers: {HttpHeaders.acceptEncodingHeader: "*"}), // disable gzip
// Listen the download progress.
onReceiveProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + "%");
}
});
var f = new File("./example/flutter.svg");
var t = await f.open();
t.close();
});
});
group('formdata', () {
test("test", () async {
var dio = new Dio();
dio.options.baseUrl = "http://www.dtworkroom.com/doris/1/2.0.0/";
// dio.onHttpClientCreate = (HttpClient client) {
// client.findProxy = (uri) {
// //proxy all request to localhost:8888
// return "PROXY localhost:8888";
// };
// };
FormData formData = new FormData.from({
"name": "wendux",
"age": 25,
});
formData.remove("name");
formData["xx"] = 9;
formData.add("file",
new UploadFileInfo(new File("./example/upload.txt"), "upload.txt"));
Response response = await dio.post("/test", data: formData);
formData.clear();
expect(formData.length, 0);
});
});
group('Cancellation', () {
test("test", () async {
var dio = new Dio();
CancelToken token = new CancelToken();
new Timer(new Duration(milliseconds: 10), () {
token.cancel("cancelled");
});
var url = "https://accounts.google.com";
await dio.get(url, cancelToken: token).catchError((e) {
expect(CancelToken.isCancel(e), true);
if (CancelToken.isCancel(e)) {
expect(e.type, DioErrorType.CANCEL);
print('$url: $e');
}
});
});
test("test download", () async {
var dio = new Dio();
CancelToken token = new CancelToken();
new Timer(new Duration(milliseconds: 1000), () {
token.cancel("cancelled");
});
final url = 'http://download.dcloud.net.cn/HBuilder.9.0.2.macosx_64.dmg';
final savePath = './example/HBuilder.9.0.2.macosx_64.dmg';
await dio.download(url, savePath, cancelToken: token).catchError((e) {
expect(CancelToken.isCancel(e), true);
if (CancelToken.isCancel(e)) {
expect(e.type, DioErrorType.CANCEL);
}
});
});
});
group('transfomer', () {
test("test", () async {
var dio = new Dio();
dio.options.baseUrl="http://www.dtworkroom.com/doris/1/2.0.0";
dio.transformer = new MyTransformer();
// Response response = await dio.get("https://www.baidu.com");
// assert(response.request.extra["cookies"]!=null);
try {
var r = await dio.post("/test",
data: ["1", "2"]);
} catch (e) {
expect(e.message, "Can't send List to sever directly");
}
dio.get("/test").then((r){
expect(r.request.extra["xx"], "extra");
});
var data = {
"a": "你好",
"b": [5, "6"],
"c": {
"d": 8,
"e": {
"a": 5,
"b": [66, 8]
}
}
};
var dest =
"a=%E4%BD%A0%E5%A5%BD&b%5B%5D=5&b%5B%5D=6&c%5Bd%5D=8&c%5Be%5D%5Ba%5D=5&c%5Be%5D%5Bb%5D%5B%5D=66&c%5Be%5D%5Bb%5D%5B%5D=8";
expect(Transformer.urlEncodeMap(data), dest);
});
});
group('Request Interceptor', () {
Dio dio;
setUp(() {
dio = new Dio();
dio.options.baseUrl = BASE_URL;
dio.interceptors.add(InterceptorsWrapper(
onRequest: (RequestOptions options) async {
switch (options.path) {
case "/fakepath1":
return dio.resolve("fake data");
case "/fakepath2":
return dio.get("/test");
case "/fakepath3":
return dio.reject(
"test error"); //you can also return a HttpError directly.
case "fakepath4":
return new DioError(
message:
"test error"); // Here is equivalent to call dio.reject("test error")
case "/test?tag=1":
{
Response response = await dio.get("/token");
print(response);
options.headers["token"] = response.data["data"]["token"];
return options;
}
default:
return options; //continue
}
}
));
});
test('TestRI', () async {
Response
// response = await dio.get("/fakepath1");
// expect(response.data, "fake data");
// response = await dio.get("/fakepath2");
// expect(response.data["errCode"], 0);
//
// try {
// response = await dio.get("/fakepath3");
// } on DioError catch (e) {
// expect(e.message, "test error");
// expect(e.response, null);
// }
// try {
// response = await dio.get("/fakepath4");
// } on DioError catch (e) {
// expect(e.message, "test error");
// expect(e.response, null);
// }
// response = await dio.get("/test");
// expect(response.data["errCode"], 0);
//
response = await dio.get("/test?tag=1");
expect(response.data["errCode"], 0);
// try {
// await dio.get("https://wendux.github.io/xsddddd");
// } on DioError catch (e) {
// expect(e.response.statusCode, 404);
// }
});
});
group('Response Interceptor', () {
Dio dio;
const String URL_NOT_FIND = "https://wendux.github.io/xxxxx/";
const String URL_NOT_FIND_1 = URL_NOT_FIND + "1";
const String URL_NOT_FIND_2 = URL_NOT_FIND + "2";
const String URL_NOT_FIND_3 = URL_NOT_FIND + "3";
setUp(() {
dio = new Dio();
dio.options.baseUrl = BASE_URL;
dio.interceptors.add(InterceptorsWrapper(
onResponse: (Response response) {
return response.data["data"];
},
onError: (DioError e) {
if (e.response != null) {
switch (e.response.request.path) {
case URL_NOT_FIND:
return e;
case URL_NOT_FIND_1:
return dio.resolve("fake data"); // you can also return a HttpError directly.
case URL_NOT_FIND_2:
return new Response(data: "fake data");
case URL_NOT_FIND_3:
return 'custom error info [${e.response.statusCode}]';
}
}
return e;
}
));
});
test('Test', () async {
//await dio.get("/test").then(print);
Response response = await dio.get("/test");
expect(response.data["path"], "/test");
try {
await dio.get(URL_NOT_FIND);
} catch (e) {
expect(e.response.statusCode, 404);
}
response = await dio.get(URL_NOT_FIND + "1");
expect(response.data, "fake data");
response = await dio.get(URL_NOT_FIND + "2");
expect(response.data, "fake data");
try {
await dio.get(URL_NOT_FIND + "3");
} catch (e) {
expect(e.message, 'custom error info [404]');
}
});
});
group('Interceptor lock', () {
test("test", () async {
Dio dio = new Dio();
// new dio instance to request token
Dio tokenDio = new Dio();
String csrfToken;
dio.options.baseUrl = "http://www.dtworkroom.com/doris/1/2.0.0/";
tokenDio.options = dio.options;
dio.interceptors.add(InterceptorsWrapper(
onRequest: (RequestOptions options) {
print('send request:path:${options.path},baseURL:${options.baseUrl}');
if (csrfToken == null) {
print("no token,request token firstly...");
//lock the dio.
dio.lock();
return tokenDio.get("/token").then((d) {
options.headers["csrfToken"] = csrfToken = d.data['data']['token'];
print("request token succeed, value: " + d.data['data']['token']);
print(
'continue to perform request:path:${options.path},baseURL:${options.path}');
return options;
}).whenComplete(() => dio.unlock()); // unlock the dio
} else {
options.headers["csrfToken"] = csrfToken;
return options;
}
}
));
_onResult(d) {
print("request ok!");
}
await Future.wait([
dio.get("/test?tag=1").then(_onResult),
dio.get("/test?tag=2").then(_onResult),
dio.get("/test?tag=3").then(_onResult)
]);
});
});
}