Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import '../../cloud_functions_platform_interface.dart';
import 'method_channel_firebase_functions.dart';
import 'utils/exception.dart';

dynamic _convertNested(Object? value) {
if (value is List) {
return value.map(_convertNested).toList();
}
if (value is Map) {
return value.map<String, dynamic>(
(key, value) => MapEntry(key as String, _convertNested(value)),
);
}
return value;
}

/// Method Channel delegate for [HttpsCallablePlatform].
class MethodChannelHttpsCallable extends HttpsCallablePlatform {
/// Creates a new [MethodChannelHttpsCallable] instance.
Expand Down Expand Up @@ -38,11 +50,7 @@ class MethodChannelHttpsCallable extends HttpsCallablePlatform {
'limitedUseAppCheckToken': options.limitedUseAppCheckToken,
});

if (result is Map) {
return Map<String, dynamic>.from(result);
} else {
return result;
}
return _convertNested(result);
} catch (e, s) {
convertPlatformException(e, s);
}
Expand Down Expand Up @@ -70,12 +78,7 @@ class MethodChannelHttpsCallable extends HttpsCallablePlatform {
'limitedUseAppCheckToken': options.limitedUseAppCheckToken,
'timeout': options.timeout.inMilliseconds,
};
yield* channel.receiveBroadcastStream(eventData).map((message) {
if (message is Map) {
return Map<String, dynamic>.from(message);
}
return message;
});
yield* channel.receiveBroadcastStream(eventData).map(_convertNested);
} catch (e, s) {
convertPlatformException(e, s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import '../mock.dart';
import '../pigeon/test_api.dart';

void main() {
setupFirebaseFunctionsMocks();

MethodChannelFirebaseFunctions? functions;
MethodChannelHttpsCallable? httpsCallable;
final List<MethodCall> logger = <MethodCall>[];

// mock props
bool mockPlatformExceptionThrown = false;
Expand All @@ -32,22 +32,15 @@ void main() {
setUpAll(() async {
FirebaseApp app = await Firebase.initializeApp();

handleMethodCall((call) async {
logger.add(call);
TestCloudFunctionsHostApi.setUp(_TestCloudFunctionsHostApi(() async {
if (mockExceptionThrown) {
throw Exception();
} else if (mockPlatformExceptionThrown) {
throw PlatformException(
code: 'UNKNOWN', message: kPlatformExceptionMessage);
}

switch (call.method) {
case 'FirebaseFunctions#call':
return kParameters;
default:
return null;
}
});
return kParameters;
}));

functions =
MethodChannelFirebaseFunctions(app: app, region: 'us-central1');
Expand All @@ -64,8 +57,6 @@ void main() {
mockPlatformExceptionThrown = false;
mockExceptionThrown = false;
httpsCallable!.options = kOptions;

logger.clear();
});

group('constructor', () {
Expand Down Expand Up @@ -101,6 +92,22 @@ void main() {
});

group('call', () {
test('converts maps nested in lists', () async {
final originalParameters = kParameters;
addTearDown(() => kParameters = originalParameters);
kParameters = <Object?>[
<Object?, Object?>{'name': 'value'},
];

final result = await httpsCallable!.call();

expect(result, isA<List<dynamic>>());
expect((result as List<dynamic>).single, isA<Map<String, dynamic>>());
expect(result, <dynamic>[
<String, dynamic>{'name': 'value'},
]);
});

test('catch an [Exception] error', () async {
mockExceptionThrown = true;
await testExceptionHandling('EXCEPTION', httpsCallable!.call);
Expand All @@ -115,3 +122,15 @@ void main() {
});
});
}

class _TestCloudFunctionsHostApi implements TestCloudFunctionsHostApi {
_TestCloudFunctionsHostApi(this.callHandler);

final Future<Object?> Function() callHandler;

@override
Future<Object?> call(Map<String, Object?> arguments) => callHandler();

@override
Future<void> registerEventChannel(Map<String, Object> arguments) async {}
}
Loading