From ebdf68a07e7be8a17097168a6fe47354cbb36019 Mon Sep 17 00:00:00 2001 From: Prasanna Anbazhagan Date: Sun, 28 Apr 2024 23:04:08 +0530 Subject: [PATCH] Perform url decode to allow dot values in json key --- .../services/script_executor_service.dart | 2 +- .../script_executor_service_test.dart | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/domain/services/script_executor_service.dart b/lib/domain/services/script_executor_service.dart index 66a632b..c9c9d1b 100644 --- a/lib/domain/services/script_executor_service.dart +++ b/lib/domain/services/script_executor_service.dart @@ -41,7 +41,7 @@ class ScriptExecutorService { double? _getValueFromJsonPath(String json, String jsonPath) { Map map = jsonDecode(json); - List keys = jsonPath.split('.'); + List keys = jsonPath.split('.').map((e) => Uri.decodeFull(e)).toList(); dynamic current = map; for (String key in keys) { diff --git a/test/domain/services/script_executor_service_test.dart b/test/domain/services/script_executor_service_test.dart index 684fa58..44ce5c9 100644 --- a/test/domain/services/script_executor_service_test.dart +++ b/test/domain/services/script_executor_service_test.dart @@ -37,4 +37,25 @@ void main() { expect(value, equals(3.0)); }); + + test('should fetch data from script for dot values', () async { + const String script = 'test_script'; + when(mockDSLParser.parse(script)).thenReturn( + ParsedScript( + url: 'https://api.mfapi.in/mf/128074/latest?user=11', + headers: {'Authorization': 'Bearer'}, + responsePath: 'Global Quote.05%2e price', + ), + ); + final scriptExecutorService = ScriptExecutorService.withMock( + client: mockClient, dslParser: mockDSLParser); + when(mockClient.get( + Uri.parse('https://api.mfapi.in/mf/128074/latest?user=11'), + headers: {'Authorization': 'Bearer'})) + .thenAnswer((_) async => http.Response('{"Global Quote":{"05. price": "3"}}', 200)); + final value = + await scriptExecutorService.executeScript(script: script); + + expect(value, equals(3.0)); + }); }