2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
@TestOn ('vm' )
5
-
6
5
import 'dart:async' ;
7
6
import 'dart:io' ;
8
7
import 'package:dio/dio.dart' ;
9
8
import 'package:test/test.dart' ;
10
9
11
10
void main () {
11
+ test ('#test options' , () {
12
+ var map = {'a' : '5' };
13
+ var mapOverride = {'b' : '6' };
14
+ var baseOptions = BaseOptions (
15
+ connectTimeout: 2000 ,
16
+ receiveTimeout: 2000 ,
17
+ sendTimeout: 2000 ,
18
+ baseUrl: 'http://localhost' ,
19
+ queryParameters: map,
20
+ extra: map,
21
+ headers: map,
22
+ contentType: "application/json" ,
23
+ followRedirects: false ,
24
+ );
25
+ var opt1 = baseOptions.merge (
26
+ method: 'post' ,
27
+ receiveTimeout: 3000 ,
28
+ sendTimeout: 3000 ,
29
+ baseUrl: 'https://flutterchina.club' ,
30
+ extra: mapOverride,
31
+ headers: mapOverride,
32
+ contentType: 'text/html' ,
33
+ );
34
+ assert (opt1.method == "post" );
35
+ assert (opt1.receiveTimeout == 3000 );
36
+ assert (opt1.connectTimeout == 2000 );
37
+ assert (opt1.followRedirects == false );
38
+ assert (opt1.baseUrl == 'https://flutterchina.club' );
39
+ assert (opt1.headers['b' ] == '6' );
40
+ assert (opt1.extra['b' ] == '6' );
41
+ assert (opt1.queryParameters['b' ] == null );
42
+ assert (opt1.contentType == 'text/html' );
43
+
44
+ var opt2 = Options (
45
+ method: 'get' ,
46
+ receiveTimeout: 2000 ,
47
+ sendTimeout: 2000 ,
48
+ extra: map,
49
+ headers: map,
50
+ contentType: "application/json" ,
51
+ followRedirects: false ,
52
+ );
53
+ var opt3 = opt2.merge (
54
+ method: 'post' ,
55
+ receiveTimeout: 3000 ,
56
+ sendTimeout: 3000 ,
57
+ extra: mapOverride,
58
+ headers: mapOverride,
59
+ contentType: 'text/html' ,
60
+ );
61
+ assert (opt3.method == "post" );
62
+ assert (opt3.receiveTimeout == 3000 );
63
+ assert (opt3.followRedirects == false );
64
+ assert (opt3.headers['b' ] == '6' );
65
+ assert (opt3.extra['b' ] == '6' );
66
+ assert (opt3.contentType == 'text/html' );
67
+
68
+ var opt4 = RequestOptions (
69
+ sendTimeout: 2000 ,
70
+ followRedirects: false ,
71
+ );
72
+ var opt5 = opt4.merge (
73
+ method: 'post' ,
74
+ receiveTimeout: 3000 ,
75
+ sendTimeout: 3000 ,
76
+ extra: mapOverride,
77
+ headers: mapOverride,
78
+ data: "xx=5" ,
79
+ path: '/' ,
80
+ contentType: 'text/html' ,
81
+ );
82
+ assert (opt5.method == "post" );
83
+ assert (opt5.receiveTimeout == 3000 );
84
+ assert (opt5.followRedirects == false );
85
+ assert (opt5.contentType == 'text/html' );
86
+ assert (opt5.headers['b' ] == '6' );
87
+ assert (opt5.extra['b' ] == '6' );
88
+ assert (opt5.data == 'xx=5' );
89
+ assert (opt5.path == '/' );
90
+ });
91
+
92
+ test ('#test headers' , () {
93
+ var headers = Headers .fromMap ({
94
+ "set-cookie" : ['k=v' , 'k1=v1' ],
95
+ 'content-length' : ['200' ],
96
+ 'test' : ['1' , '2' ],
97
+ });
98
+ headers.add ('SET-COOKIE' , 'k2=v2' );
99
+ assert (headers.value ('content-length' ) == '200' );
100
+ expect (Future (() => headers.value ('test' )), throwsException);
101
+ assert (headers['set-cookie' ].length == 3 );
102
+ headers.remove ("set-cookie" , 'k=v' );
103
+ assert (headers['set-cookie' ].length == 2 );
104
+ headers.removeAll ('set-cookie' );
105
+ assert (headers['set-cookie' ] == null );
106
+ var ls = [];
107
+ headers.forEach ((k, list) {
108
+ ls.addAll (list);
109
+ });
110
+ assert (ls.length == 3 );
111
+ assert (headers.toString () == "content-length: 200\n test: 1\n test: 2\n " );
112
+ headers.set ('content-length' , '300' );
113
+ assert (headers.value ('content-length' ) == '300' );
114
+ headers.set ('content-length' , ['400' ]);
115
+ assert (headers.value ('content-length' ) == '400' );
116
+
117
+ var headers1 = Headers ();
118
+ headers1.set ('xx' , 'v' );
119
+ assert (headers1.value ('xx' ) == 'v' );
120
+ headers1.clear ();
121
+ assert (headers1.map.isEmpty == true );
122
+ });
123
+
12
124
test ('#send with an invalid URL' , () {
13
125
expect (Dio ().get ('http://http.invalid' ).catchError ((e) => throw e.error),
14
126
throwsA (const TypeMatcher <SocketException >()));
15
127
});
128
+
16
129
test ('#cancellation' , () async {
17
130
var dio = Dio ();
18
131
CancelToken token = CancelToken ();
19
132
Timer (Duration (milliseconds: 10 ), () {
20
133
token.cancel ('cancelled' );
134
+ dio.httpClientAdapter.close (force: true );
21
135
});
22
136
23
137
var url = 'https://accounts.google.com' ;
@@ -28,7 +142,6 @@ void main() {
28
142
throwsA (isTrue));
29
143
});
30
144
31
-
32
145
test ('#url encode ' , () {
33
146
var data = {
34
147
'a' : '你好' ,
0 commit comments