From 0253666012238edd740feb0e5e9c59d087cf4877 Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Sun, 10 Mar 2024 18:33:57 -0300 Subject: [PATCH] Fix mocks being returned even when the condition script throws an error --- Mockaco.sln | 2 +- .../Request/RequestConditionMatcher.cs | 17 +++--- .../Templating/TemplateTransformer.cs | 32 +++-------- .../Request/RequestConditionMatcherTest.cs | 54 +++++++++++++++++++ .../Templating/TemplateTransformerTest.cs | 18 +++---- 5 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 test/Mockaco.AspNetCore.Tests/Templating/Request/RequestConditionMatcherTest.cs diff --git a/Mockaco.sln b/Mockaco.sln index 932cbd4..548d28c 100644 --- a/Mockaco.sln +++ b/Mockaco.sln @@ -10,7 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mockaco.Tests", "test\Mockaco.AspNetCore.Tests\Mockaco.AspNetCore.Tests.csproj", "{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mockaco.AspNetCore.Tests", "test\Mockaco.AspNetCore.Tests\Mockaco.AspNetCore.Tests.csproj", "{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mockaco.AspNetCore", "src\Mockaco.AspNetCore\Mockaco.AspNetCore.csproj", "{7766C592-9887-4162-8B9C-51003ED30335}" EndProject diff --git a/src/Mockaco.AspNetCore/Templating/Request/RequestConditionMatcher.cs b/src/Mockaco.AspNetCore/Templating/Request/RequestConditionMatcher.cs index 858a674..ec6aefd 100644 --- a/src/Mockaco.AspNetCore/Templating/Request/RequestConditionMatcher.cs +++ b/src/Mockaco.AspNetCore/Templating/Request/RequestConditionMatcher.cs @@ -1,8 +1,5 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; -using System; -using System.Linq; -using System.Threading.Tasks; namespace Mockaco { @@ -43,12 +40,18 @@ public async Task IsMatch(HttpRequest httpRequest, Mock mock) } await conditionMatcherScriptContext.AttachRouteParameters(httpRequest, mock); + try + { + var template = await _templateTransformer.Transform(mock.RawTemplate, conditionMatcherScriptContext); - var template = await _templateTransformer.Transform(mock.RawTemplate, conditionMatcherScriptContext); - - var isMatch = template.Request?.Condition ?? true; + var isMatch = template.Request?.Condition ?? true; - return isMatch; + return isMatch; + } + catch (Exception) + { + return false; + } } //TODO Remove redundant code diff --git a/src/Mockaco.AspNetCore/Templating/TemplateTransformer.cs b/src/Mockaco.AspNetCore/Templating/TemplateTransformer.cs index 941f8e7..529da81 100644 --- a/src/Mockaco.AspNetCore/Templating/TemplateTransformer.cs +++ b/src/Mockaco.AspNetCore/Templating/TemplateTransformer.cs @@ -1,9 +1,7 @@ using Microsoft.Extensions.Logging; using Mono.TextTemplating; using Newtonsoft.Json; -using System; using System.Text; -using System.Threading.Tasks; namespace Mockaco { @@ -28,12 +26,12 @@ public async Task