Skip to content

Commit 028c494

Browse files
committed
test: add tests for body encoding
1 parent bff8e2a commit 028c494

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

packages/functions_client/test/custom_http_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CustomHttpClient extends BaseClient {
1414
// Add request to receivedRequests list.
1515
receivedRequests = receivedRequests..add(request);
1616

17-
if (request.url.path.endsWith("function")) {
17+
if (request.url.path.endsWith("error-function")) {
1818
//Return custom status code to check for usage of this client.
1919
return StreamedResponse(
2020
Stream.value(utf8.encode(jsonEncode({"key": "Hello World"}))),

packages/functions_client/test/functions_dart_test.dart

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ void main() {
2020
});
2121
test('function throws', () async {
2222
try {
23-
await functionsCustomHttpClient.invoke('function');
23+
await functionsCustomHttpClient.invoke('error-function');
2424
fail('should throw');
2525
} on FunctionException catch (e) {
2626
expect(e.status, 420);
2727
}
2828
});
2929

3030
test('function call', () async {
31-
final res = await functionsCustomHttpClient.invoke('function1');
31+
final res = await functionsCustomHttpClient.invoke('function');
3232
expect(res.data, {'key': 'Hello World'});
3333
expect(res.status, 200);
3434
});
3535

3636
test('function call with query parameters', () async {
3737
final res = await functionsCustomHttpClient
38-
.invoke('function1', queryParameters: {'key': 'value'});
38+
.invoke('function', queryParameters: {'key': 'value'});
3939

4040
final request = customHttpClient.receivedRequests.last;
4141

@@ -48,7 +48,7 @@ void main() {
4848
final fileName = "file.txt";
4949
final fileContent = "Hello World";
5050
final res = await functionsCustomHttpClient.invoke(
51-
'function1',
51+
'function',
5252
queryParameters: {'key': 'value'},
5353
files: [
5454
MultipartFile.fromString(fileName, fileContent),
@@ -78,7 +78,7 @@ void main() {
7878
);
7979

8080
await client.dispose();
81-
final res = await client.invoke('function1');
81+
final res = await client.invoke('function');
8282
expect(res.data, {'key': 'Hello World'});
8383
});
8484

@@ -90,5 +90,60 @@ void main() {
9090
['a', 'b', 'c'],
9191
));
9292
});
93+
94+
group('body encoding', () {
95+
test('integer properly encoded', () async {
96+
await functionsCustomHttpClient.invoke('function', body: 42);
97+
98+
final req = customHttpClient.receivedRequests.last;
99+
expect(req, isA<Request>());
100+
101+
req as Request;
102+
expect(req.body, '42');
103+
});
104+
105+
test('double is properly encoded', () async {
106+
await functionsCustomHttpClient.invoke('function', body: 42.9);
107+
108+
final req = customHttpClient.receivedRequests.last;
109+
expect(req, isA<Request>());
110+
111+
req as Request;
112+
expect(req.body, '42.9');
113+
});
114+
115+
test('string is properly encoded', () async {
116+
await functionsCustomHttpClient.invoke('function', body: 'ExampleText');
117+
118+
final req = customHttpClient.receivedRequests.last;
119+
expect(req, isA<Request>());
120+
121+
req as Request;
122+
expect(req.body, 'ExampleText');
123+
});
124+
125+
test('list is properly encoded', () async {
126+
await functionsCustomHttpClient.invoke('function', body: [1, 2, 3]);
127+
128+
final req = customHttpClient.receivedRequests.last;
129+
expect(req, isA<Request>());
130+
131+
req as Request;
132+
expect(req.body, '[1,2,3]');
133+
});
134+
135+
test('map is properly encoded', () async {
136+
await functionsCustomHttpClient.invoke(
137+
'function',
138+
body: {'thekey': 'thevalue'},
139+
);
140+
141+
final req = customHttpClient.receivedRequests.last;
142+
expect(req, isA<Request>());
143+
144+
req as Request;
145+
expect(req.body, '{"thekey":"thevalue"}');
146+
});
147+
});
93148
});
94149
}

0 commit comments

Comments
 (0)