@@ -2,6 +2,7 @@ import 'dart:convert';
22
33import 'package:functions_client/src/functions_client.dart' ;
44import 'package:functions_client/src/types.dart' ;
5+ import 'package:http/http.dart' ;
56import 'package:test/test.dart' ;
67import 'package:yet_another_json_isolate/yet_another_json_isolate.dart' ;
78
@@ -19,22 +20,24 @@ void main() {
1920 });
2021 test ('function throws' , () async {
2122 try {
22- await functionsCustomHttpClient.invoke ('function' );
23+ await functionsCustomHttpClient.invoke ('error- function' );
2324 fail ('should throw' );
2425 } on FunctionException catch (e) {
2526 expect (e.status, 420 );
2627 }
2728 });
2829
2930 test ('function call' , () async {
30- final res = await functionsCustomHttpClient.invoke ('function1' );
31+ final res = await functionsCustomHttpClient.invoke ('function' );
32+ expect (
33+ customHttpClient.receivedRequests.last.headers["Content-Type" ], null );
3134 expect (res.data, {'key' : 'Hello World' });
3235 expect (res.status, 200 );
3336 });
3437
3538 test ('function call with query parameters' , () async {
3639 final res = await functionsCustomHttpClient
37- .invoke ('function1 ' , queryParameters: {'key' : 'value' });
40+ .invoke ('function ' , queryParameters: {'key' : 'value' });
3841
3942 final request = customHttpClient.receivedRequests.last;
4043
@@ -43,6 +46,27 @@ void main() {
4346 expect (res.status, 200 );
4447 });
4548
49+ test ('function call with files' , () async {
50+ final fileName = "file.txt" ;
51+ final fileContent = "Hello World" ;
52+ final res = await functionsCustomHttpClient.invoke (
53+ 'function' ,
54+ queryParameters: {'key' : 'value' },
55+ files: [
56+ MultipartFile .fromString (fileName, fileContent),
57+ ],
58+ );
59+
60+ final request = customHttpClient.receivedRequests.last;
61+
62+ expect (request.url.queryParameters, {'key' : 'value' });
63+ expect (request.headers['Content-Type' ], contains ('multipart/form-data' ));
64+ expect (res.data, [
65+ {'name' : fileName, 'content' : fileContent}
66+ ]);
67+ expect (res.status, 200 );
68+ });
69+
4670 test ('dispose isolate' , () async {
4771 await functionsCustomHttpClient.dispose ();
4872 expect (functionsCustomHttpClient.invoke ('function' ), throwsStateError);
@@ -57,7 +81,7 @@ void main() {
5781 );
5882
5983 await client.dispose ();
60- final res = await client.invoke ('function1 ' );
84+ final res = await client.invoke ('function ' );
6185 expect (res.data, {'key' : 'Hello World' });
6286 });
6387
@@ -69,5 +93,65 @@ void main() {
6993 ['a' , 'b' , 'c' ],
7094 ));
7195 });
96+
97+ group ('body encoding' , () {
98+ test ('integer properly encoded' , () async {
99+ await functionsCustomHttpClient.invoke ('function' , body: 42 );
100+
101+ final req = customHttpClient.receivedRequests.last;
102+ expect (req, isA <Request >());
103+
104+ req as Request ;
105+ expect (req.body, '42' );
106+ expect (req.headers["Content-Type" ], contains ("application/json" ));
107+ });
108+
109+ test ('double is properly encoded' , () async {
110+ await functionsCustomHttpClient.invoke ('function' , body: 42.9 );
111+
112+ final req = customHttpClient.receivedRequests.last;
113+ expect (req, isA <Request >());
114+
115+ req as Request ;
116+ expect (req.body, '42.9' );
117+ expect (req.headers["Content-Type" ], contains ("application/json" ));
118+ });
119+
120+ test ('string is properly encoded' , () async {
121+ await functionsCustomHttpClient.invoke ('function' , body: 'ExampleText' );
122+
123+ final req = customHttpClient.receivedRequests.last;
124+ expect (req, isA <Request >());
125+
126+ req as Request ;
127+ expect (req.body, 'ExampleText' );
128+ expect (req.headers["Content-Type" ], contains ("text/plain" ));
129+ });
130+
131+ test ('list is properly encoded' , () async {
132+ await functionsCustomHttpClient.invoke ('function' , body: [1 , 2 , 3 ]);
133+
134+ final req = customHttpClient.receivedRequests.last;
135+ expect (req, isA <Request >());
136+
137+ req as Request ;
138+ expect (req.body, '[1,2,3]' );
139+ expect (req.headers["Content-Type" ], contains ("application/json" ));
140+ });
141+
142+ test ('map is properly encoded' , () async {
143+ await functionsCustomHttpClient.invoke (
144+ 'function' ,
145+ body: {'thekey' : 'thevalue' },
146+ );
147+
148+ final req = customHttpClient.receivedRequests.last;
149+ expect (req, isA <Request >());
150+
151+ req as Request ;
152+ expect (req.body, '{"thekey":"thevalue"}' );
153+ expect (req.headers["Content-Type" ], contains ("application/json" ));
154+ });
155+ });
72156 });
73157}
0 commit comments