From 7ded898dee07004cbb20e4d7c209f94a507fad3b Mon Sep 17 00:00:00 2001 From: Louis DACHET <87450402+Cteq3132@users.noreply.github.com> Date: Mon, 13 May 2024 16:01:35 +0200 Subject: [PATCH] feat(functions): Invoke function with custom query params (#926) * feat(functions): invoke function with custom query params * feat: update query parameters type to match Uri type --------- Co-authored-by: louisdachet --- .../lib/src/functions_client.dart | 4 +++- .../functions_client/test/custom_http_client.dart | 9 +++++++++ .../test/functions_dart_test.dart | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/functions_client/lib/src/functions_client.dart b/packages/functions_client/lib/src/functions_client.dart index 97926f50..99d7c7a5 100644 --- a/packages/functions_client/lib/src/functions_client.dart +++ b/packages/functions_client/lib/src/functions_client.dart @@ -71,11 +71,13 @@ class FunctionsClient { String functionName, { Map? headers, Map? body, + Map? queryParameters, HttpMethod method = HttpMethod.post, }) async { final bodyStr = body == null ? null : await _isolate.encode(body); - final uri = Uri.parse('$_url/$functionName'); + final uri = Uri.parse('$_url/$functionName') + .replace(queryParameters: queryParameters); final finalHeaders = { ..._headers, diff --git a/packages/functions_client/test/custom_http_client.dart b/packages/functions_client/test/custom_http_client.dart index 74db4b28..247d6095 100644 --- a/packages/functions_client/test/custom_http_client.dart +++ b/packages/functions_client/test/custom_http_client.dart @@ -3,8 +3,17 @@ import 'dart:convert'; import 'package:http/http.dart'; class CustomHttpClient extends BaseClient { + /// List of received requests by the client. + /// + /// Usefull for testing purposes, to check the request was constructed + /// correctly. + List receivedRequests = []; + @override Future send(BaseRequest request) async { + // Add request to receivedRequests list. + receivedRequests = receivedRequests..add(request); + if (request.url.path.endsWith("function")) { //Return custom status code to check for usage of this client. return StreamedResponse( diff --git a/packages/functions_client/test/functions_dart_test.dart b/packages/functions_client/test/functions_dart_test.dart index 89f0e867..7637b8f0 100644 --- a/packages/functions_client/test/functions_dart_test.dart +++ b/packages/functions_client/test/functions_dart_test.dart @@ -9,11 +9,13 @@ import 'custom_http_client.dart'; void main() { late FunctionsClient functionsCustomHttpClient; + late CustomHttpClient customHttpClient; group("Custom http client", () { setUp(() { + customHttpClient = CustomHttpClient(); functionsCustomHttpClient = - FunctionsClient("", {}, httpClient: CustomHttpClient()); + FunctionsClient("", {}, httpClient: customHttpClient); }); test('function throws', () async { try { @@ -30,6 +32,17 @@ void main() { expect(res.status, 200); }); + test('function call with query parameters', () async { + final res = await functionsCustomHttpClient + .invoke('function1', queryParameters: {'key': 'value'}); + + final request = customHttpClient.receivedRequests.last; + + expect(request.url.queryParameters, {'key': 'value'}); + expect(res.data, {'key': 'Hello World'}); + expect(res.status, 200); + }); + test('dispose isolate', () async { await functionsCustomHttpClient.dispose(); expect(functionsCustomHttpClient.invoke('function'), throwsStateError);