From d8470830abdeed20294eaf208aac54ce3b0a0cfe Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Sun, 13 Dec 2020 12:21:25 -0300 Subject: [PATCH] Fix #53, avoid unexpected response when the request object is malformed --- .../Middlewares/RequestMatchingMiddleware.cs | 6 +++--- src/Mockaco/Startup.cs | 6 +++--- .../Templating/Request/RequestConditionMatcher.cs | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Mockaco/Middlewares/RequestMatchingMiddleware.cs b/src/Mockaco/Middlewares/RequestMatchingMiddleware.cs index 3b4a480..09249c3 100644 --- a/src/Mockaco/Middlewares/RequestMatchingMiddleware.cs +++ b/src/Mockaco/Middlewares/RequestMatchingMiddleware.cs @@ -29,7 +29,7 @@ IEnumerable requestMatchers { await LogHttpContext(httpContext); - AttachRequestToScriptContext(httpContext, mockacoContext, scriptContext); + await AttachRequestToScriptContext(httpContext, mockacoContext, scriptContext); if (mockacoContext.Errors.Any()) { @@ -65,11 +65,11 @@ IEnumerable requestMatchers } //TODO Remove redundant code - private void AttachRequestToScriptContext(HttpContext httpContext, IMockacoContext mockacoContext, IScriptContext scriptContext) + private async Task AttachRequestToScriptContext(HttpContext httpContext, IMockacoContext mockacoContext, IScriptContext scriptContext) { try { - scriptContext.AttachRequest(httpContext.Request); + await scriptContext.AttachRequest(httpContext.Request); } catch (Exception ex) { diff --git a/src/Mockaco/Startup.cs b/src/Mockaco/Startup.cs index 0e8e775..b9b4553 100644 --- a/src/Mockaco/Startup.cs +++ b/src/Mockaco/Startup.cs @@ -12,7 +12,7 @@ public partial class Startup public Startup(IConfiguration configuration) { - _configuration = configuration; + _configuration = configuration; } public void ConfigureServices(IServiceCollection services) @@ -29,7 +29,7 @@ public void ConfigureServices(IServiceCollection services) .AddTransient() .AddSingleton() - + .AddSingleton() .AddSingleton() .AddSingleton() @@ -40,9 +40,9 @@ public void ConfigureServices(IServiceCollection services) .AddTransient() - .AddTransient() .AddTransient() .AddTransient() + .AddTransient() .AddTransient() diff --git a/src/Mockaco/Templating/Request/RequestConditionMatcher.cs b/src/Mockaco/Templating/Request/RequestConditionMatcher.cs index 4accc52..f440920 100644 --- a/src/Mockaco/Templating/Request/RequestConditionMatcher.cs +++ b/src/Mockaco/Templating/Request/RequestConditionMatcher.cs @@ -7,7 +7,7 @@ namespace Mockaco { public class RequestConditionMatcher : IRequestMatcher - { + { private readonly ITemplateTransformer _templateTransformer; private readonly IFakerFactory _fakerFactory; private readonly IRequestBodyFactory _requestBodyFactory; @@ -15,14 +15,14 @@ public class RequestConditionMatcher : IRequestMatcher private readonly IGlobalVariableStorage _globalVarialeStorage; private readonly ILogger _logger; - public RequestConditionMatcher( + public RequestConditionMatcher( ITemplateTransformer templateTransformer, IFakerFactory fakerFactory, IRequestBodyFactory requestBodyFactory, IMockacoContext mockacoContext, IGlobalVariableStorage globalVariableStoreage, ILogger logger) - { + { _templateTransformer = templateTransformer; _fakerFactory = fakerFactory; _requestBodyFactory = requestBodyFactory; @@ -34,8 +34,8 @@ public RequestConditionMatcher( public async Task IsMatch(HttpRequest httpRequest, Mock mock) { var conditionMatcherScriptContext = new ScriptContext(_fakerFactory, _requestBodyFactory, _globalVarialeStorage); - - AttachRequestToScriptContext(httpRequest.HttpContext, _mockacoContext, conditionMatcherScriptContext); + + await AttachRequestToScriptContext(httpRequest.HttpContext, _mockacoContext, conditionMatcherScriptContext); if (_mockacoContext.Errors.Any()) { @@ -52,11 +52,11 @@ public async Task IsMatch(HttpRequest httpRequest, Mock mock) } //TODO Remove redundant code - private void AttachRequestToScriptContext(HttpContext httpContext, IMockacoContext mockacoContext, IScriptContext scriptContext) + private async Task AttachRequestToScriptContext(HttpContext httpContext, IMockacoContext mockacoContext, IScriptContext scriptContext) { try { - scriptContext.AttachRequest(httpContext.Request); + await scriptContext.AttachRequest(httpContext.Request); } catch (Exception ex) {