@@ -8,25 +8,31 @@ import 'package:zulip/api/core.dart';
8
8
import 'package:zulip/api/exception.dart' ;
9
9
import 'package:zulip/model/localizations.dart' ;
10
10
11
+ import '../model/binding.dart' ;
11
12
import '../stdlib_checks.dart' ;
12
13
import 'exception_checks.dart' ;
13
14
import 'fake_api.dart' ;
14
15
import '../example_data.dart' as eg;
15
16
16
17
void main () {
18
+ TestZulipBinding .ensureInitialized ();
19
+ tearDown (testBinding.reset);
20
+
17
21
test ('ApiConnection.get' , () async {
18
22
Future <void > checkRequest (Map <String , dynamic >? params, String expectedRelativeUrl) {
19
- return FakeApiConnection .with_ (account: eg.selfAccount, (connection) async {
20
- connection.prepare (json: {});
21
- await connection.get (kExampleRouteName, (json) => json, 'example/route' , params);
22
- check (connection.lastRequest! ).isA< http.Request > ()
23
- ..method.equals ('GET' )
24
- ..url.asString.equals ('${eg .realmUrl .origin }$expectedRelativeUrl ' )
25
- ..headers.deepEquals ({
26
- ...authHeader (email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
27
- ...userAgentHeader (),
28
- })
29
- ..body.equals ('' );
23
+ return FakeApiConnection .with_ (account: eg.selfAccount,
24
+ userAgentHeader: testBinding.userAgentHeader,
25
+ (connection) async {
26
+ connection.prepare (json: {});
27
+ await connection.get (kExampleRouteName, (json) => json, 'example/route' , params);
28
+ check (connection.lastRequest! ).isA< http.Request > ()
29
+ ..method.equals ('GET' )
30
+ ..url.asString.equals ('${eg .realmUrl .origin }$expectedRelativeUrl ' )
31
+ ..headers.deepEquals ({
32
+ ...authHeader (email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
33
+ ...testBinding.userAgentHeader (),
34
+ })
35
+ ..body.equals ('' );
30
36
});
31
37
}
32
38
@@ -47,19 +53,21 @@ void main() {
47
53
48
54
test ('ApiConnection.post' , () async {
49
55
Future <void > checkRequest (Map <String , dynamic >? params, String expectedBody, {bool expectContentType = true }) {
50
- return FakeApiConnection .with_ (account: eg.selfAccount, (connection) async {
51
- connection.prepare (json: {});
52
- await connection.post (kExampleRouteName, (json) => json, 'example/route' , params);
53
- check (connection.lastRequest! ).isA< http.Request > ()
54
- ..method.equals ('POST' )
55
- ..url.asString.equals ('${eg .realmUrl .origin }/api/v1/example/route' )
56
- ..headers.deepEquals ({
57
- ...authHeader (email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
58
- ...userAgentHeader (),
59
- if (expectContentType)
60
- 'content-type' : 'application/x-www-form-urlencoded; charset=utf-8' ,
61
- })
62
- ..body.equals (expectedBody);
56
+ return FakeApiConnection .with_ (account: eg.selfAccount,
57
+ userAgentHeader: testBinding.userAgentHeader,
58
+ (connection) async {
59
+ connection.prepare (json: {});
60
+ await connection.post (kExampleRouteName, (json) => json, 'example/route' , params);
61
+ check (connection.lastRequest! ).isA< http.Request > ()
62
+ ..method.equals ('POST' )
63
+ ..url.asString.equals ('${eg .realmUrl .origin }/api/v1/example/route' )
64
+ ..headers.deepEquals ({
65
+ ...authHeader (email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
66
+ ...testBinding.userAgentHeader (),
67
+ if (expectContentType)
68
+ 'content-type' : 'application/x-www-form-urlencoded; charset=utf-8' ,
69
+ })
70
+ ..body.equals (expectedBody);
63
71
});
64
72
}
65
73
@@ -78,26 +86,28 @@ void main() {
78
86
79
87
test ('ApiConnection.postFileFromStream' , () async {
80
88
Future <void > checkRequest (List <List <int >> content, int length, String ? filename) {
81
- return FakeApiConnection .with_ (account: eg.selfAccount, (connection) async {
82
- connection.prepare (json: {});
83
- await connection.postFileFromStream (
84
- kExampleRouteName, (json) => json, 'example/route' ,
85
- Stream .fromIterable (content), length, filename: filename);
86
- check (connection.lastRequest! ).isA< http.MultipartRequest > ()
87
- ..method.equals ('POST' )
88
- ..url.asString.equals ('${eg .realmUrl .origin }/api/v1/example/route' )
89
- ..headers.deepEquals ({
90
- ...authHeader (email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
91
- ...userAgentHeader (),
92
- })
93
- ..fields.deepEquals ({})
94
- ..files.single.which ((it) => it
95
- ..field.equals ('file' )
96
- ..length.equals (length)
97
- ..filename.equals (filename)
98
- ..has <Future <List <int >>>((f) => f.finalize ().toBytes (), 'contents' )
99
- .completes ((it) => it.deepEquals (content.expand ((l) => l)))
100
- );
89
+ return FakeApiConnection .with_ (account: eg.selfAccount,
90
+ userAgentHeader: testBinding.userAgentHeader,
91
+ (connection) async {
92
+ connection.prepare (json: {});
93
+ await connection.postFileFromStream (
94
+ kExampleRouteName, (json) => json, 'example/route' ,
95
+ Stream .fromIterable (content), length, filename: filename);
96
+ check (connection.lastRequest! ).isA< http.MultipartRequest > ()
97
+ ..method.equals ('POST' )
98
+ ..url.asString.equals ('${eg .realmUrl .origin }/api/v1/example/route' )
99
+ ..headers.deepEquals ({
100
+ ...authHeader (email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
101
+ ...testBinding.userAgentHeader (),
102
+ })
103
+ ..fields.deepEquals ({})
104
+ ..files.single.which ((it) => it
105
+ ..field.equals ('file' )
106
+ ..length.equals (length)
107
+ ..filename.equals (filename)
108
+ ..has <Future <List <int >>>((f) => f.finalize ().toBytes (), 'contents' )
109
+ .completes ((it) => it.deepEquals (content.expand ((l) => l)))
110
+ );
101
111
});
102
112
}
103
113
@@ -113,19 +123,21 @@ void main() {
113
123
114
124
test ('ApiConnection.delete' , () async {
115
125
Future <void > checkRequest (Map <String , dynamic >? params, String expectedBody, {bool expectContentType = true }) {
116
- return FakeApiConnection .with_ (account: eg.selfAccount, (connection) async {
117
- connection.prepare (json: {});
118
- await connection.delete (kExampleRouteName, (json) => json, 'example/route' , params);
119
- check (connection.lastRequest! ).isA< http.Request > ()
120
- ..method.equals ('DELETE' )
121
- ..url.asString.equals ('${eg .realmUrl .origin }/api/v1/example/route' )
122
- ..headers.deepEquals ({
123
- ...authHeader (email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
124
- ...userAgentHeader (),
125
- if (expectContentType)
126
- 'content-type' : 'application/x-www-form-urlencoded; charset=utf-8' ,
127
- })
128
- ..body.equals (expectedBody);
126
+ return FakeApiConnection .with_ (account: eg.selfAccount,
127
+ userAgentHeader: testBinding.userAgentHeader,
128
+ (connection) async {
129
+ connection.prepare (json: {});
130
+ await connection.delete (kExampleRouteName, (json) => json, 'example/route' , params);
131
+ check (connection.lastRequest! ).isA< http.Request > ()
132
+ ..method.equals ('DELETE' )
133
+ ..url.asString.equals ('${eg .realmUrl .origin }/api/v1/example/route' )
134
+ ..headers.deepEquals ({
135
+ ...authHeader (email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
136
+ ...testBinding.userAgentHeader (),
137
+ if (expectContentType)
138
+ 'content-type' : 'application/x-www-form-urlencoded; charset=utf-8' ,
139
+ })
140
+ ..body.equals (expectedBody);
129
141
});
130
142
}
131
143
0 commit comments