Skip to content

Commit

Permalink
feat(functions): invoke function with custom query params
Browse files Browse the repository at this point in the history
  • Loading branch information
louisdachet committed May 13, 2024
1 parent 0a3b73e commit f303320
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/functions_client/lib/src/functions_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ class FunctionsClient {
String functionName, {
Map<String, String>? headers,
Map<String, dynamic>? body,
Map<String, String>? 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 = <String, String>{
..._headers,
Expand Down
9 changes: 9 additions & 0 deletions packages/functions_client/test/custom_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseRequest> receivedRequests = <BaseRequest>[];

@override
Future<StreamedResponse> 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(
Expand Down
15 changes: 14 additions & 1 deletion packages/functions_client/test/functions_dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit f303320

Please sign in to comment.