@@ -8,25 +8,31 @@ import 'package:zulip/api/core.dart';
88import 'package:zulip/api/exception.dart' ;
99import 'package:zulip/model/localizations.dart' ;
1010
11+ import '../model/binding.dart' ;
1112import '../stdlib_checks.dart' ;
1213import 'exception_checks.dart' ;
1314import 'fake_api.dart' ;
1415import '../example_data.dart' as eg;
1516
1617void main () {
18+ TestZulipBinding .ensureInitialized ();
19+ tearDown (testBinding.reset);
20+
1721 test ('ApiConnection.get' , () async {
1822 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 ('' );
3036 });
3137 }
3238
@@ -47,19 +53,21 @@ void main() {
4753
4854 test ('ApiConnection.post' , () async {
4955 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);
6371 });
6472 }
6573
@@ -78,26 +86,28 @@ void main() {
7886
7987 test ('ApiConnection.postFileFromStream' , () async {
8088 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+ );
101111 });
102112 }
103113
@@ -113,19 +123,21 @@ void main() {
113123
114124 test ('ApiConnection.delete' , () async {
115125 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);
129141 });
130142 }
131143
0 commit comments