Skip to content

Commit f303320

Browse files
author
louisdachet
committed
feat(functions): invoke function with custom query params
1 parent 0a3b73e commit f303320

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

packages/functions_client/lib/src/functions_client.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ class FunctionsClient {
7171
String functionName, {
7272
Map<String, String>? headers,
7373
Map<String, dynamic>? body,
74+
Map<String, String>? queryParameters,
7475
HttpMethod method = HttpMethod.post,
7576
}) async {
7677
final bodyStr = body == null ? null : await _isolate.encode(body);
7778

78-
final uri = Uri.parse('$_url/$functionName');
79+
final uri = Uri.parse('$_url/$functionName')
80+
.replace(queryParameters: queryParameters);
7981

8082
final finalHeaders = <String, String>{
8183
..._headers,

packages/functions_client/test/custom_http_client.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ import 'dart:convert';
33
import 'package:http/http.dart';
44

55
class CustomHttpClient extends BaseClient {
6+
/// List of received requests by the client.
7+
///
8+
/// Usefull for testing purposes, to check the request was constructed
9+
/// correctly.
10+
List<BaseRequest> receivedRequests = <BaseRequest>[];
11+
612
@override
713
Future<StreamedResponse> send(BaseRequest request) async {
14+
// Add request to receivedRequests list.
15+
receivedRequests = receivedRequests..add(request);
16+
817
if (request.url.path.endsWith("function")) {
918
//Return custom status code to check for usage of this client.
1019
return StreamedResponse(

packages/functions_client/test/functions_dart_test.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import 'custom_http_client.dart';
99

1010
void main() {
1111
late FunctionsClient functionsCustomHttpClient;
12+
late CustomHttpClient customHttpClient;
1213

1314
group("Custom http client", () {
1415
setUp(() {
16+
customHttpClient = CustomHttpClient();
1517
functionsCustomHttpClient =
16-
FunctionsClient("", {}, httpClient: CustomHttpClient());
18+
FunctionsClient("", {}, httpClient: customHttpClient);
1719
});
1820
test('function throws', () async {
1921
try {
@@ -30,6 +32,17 @@ void main() {
3032
expect(res.status, 200);
3133
});
3234

35+
test('function call with query parameters', () async {
36+
final res = await functionsCustomHttpClient
37+
.invoke('function1', queryParameters: {'key': 'value'});
38+
39+
final request = customHttpClient.receivedRequests.last;
40+
41+
expect(request.url.queryParameters, {'key': 'value'});
42+
expect(res.data, {'key': 'Hello World'});
43+
expect(res.status, 200);
44+
});
45+
3346
test('dispose isolate', () async {
3447
await functionsCustomHttpClient.dispose();
3548
expect(functionsCustomHttpClient.invoke('function'), throwsStateError);

0 commit comments

Comments
 (0)