diff --git a/src/Mockaco/Middlewares/CallbackMiddleware.cs b/src/Mockaco/Middlewares/CallbackMiddleware.cs index 55a7bda..47362af 100644 --- a/src/Mockaco/Middlewares/CallbackMiddleware.cs +++ b/src/Mockaco/Middlewares/CallbackMiddleware.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Mockaco.Processors; +using Newtonsoft.Json; using System; using System.Diagnostics; using System.Linq; @@ -79,11 +80,13 @@ private static HttpRequestMessage PrepareHttpRequest(CallbackTemplate callbackTe { var request = new HttpRequestMessage(new HttpMethod(callbackTemplate.Method), callbackTemplate.Url); + var formatting = callbackTemplate.Indented.GetValueOrDefault() ? Formatting.Indented : default; + if (callbackTemplate.Body != null) { request.Content = callbackTemplate.Headers.ContainsKey("Content-Type") ? new StringContent(callbackTemplate.Body.ToString(), Encoding.UTF8, callbackTemplate.Headers["Content-Type"]) - : new StringContent(callbackTemplate.Body.ToString()); + : new StringContent(callbackTemplate.Body.ToString(formatting)); } PrepareHeaders(callbackTemplate, request); diff --git a/src/Mockaco/Middlewares/RequestMatchingMiddleware.cs b/src/Mockaco/Middlewares/RequestMatchingMiddleware.cs index 79d9b4a..269f64a 100644 --- a/src/Mockaco/Middlewares/RequestMatchingMiddleware.cs +++ b/src/Mockaco/Middlewares/RequestMatchingMiddleware.cs @@ -10,7 +10,7 @@ namespace Mockaco.Middlewares public class RequestMatchingMiddleware { private readonly RequestDelegate _next; - + public RequestMatchingMiddleware(RequestDelegate next) { _next = next; diff --git a/src/Mockaco/Models/CallbackTemplate.cs b/src/Mockaco/Models/CallbackTemplate.cs index 0449d9e..0c183b2 100644 --- a/src/Mockaco/Models/CallbackTemplate.cs +++ b/src/Mockaco/Models/CallbackTemplate.cs @@ -7,8 +7,6 @@ namespace Mockaco { public class CallbackTemplate { - public bool? Condition { get; set; } - public string Method { get; set; } public string Url { get; set; } @@ -17,8 +15,10 @@ public class CallbackTemplate public int? Timeout { get; set; } + public bool? Indented { get; set; } + public IDictionary Headers { get; set; } - public JContainer Body { get; set; } + public JToken Body { get; set; } } } \ No newline at end of file diff --git a/src/Mockaco/Models/ResponseTemplate.cs b/src/Mockaco/Models/ResponseTemplate.cs index 303c3d2..301d87f 100644 --- a/src/Mockaco/Models/ResponseTemplate.cs +++ b/src/Mockaco/Models/ResponseTemplate.cs @@ -1,12 +1,15 @@ -using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.Net; namespace Mockaco { public class ResponseTemplate - { - public int? Delay { get; set; } + { + public int? Delay { get; set; } + + public bool? Indented { get; set; } public HttpStatusCode Status { get; set; } diff --git a/src/Mockaco/Templating/TemplateResponseProcessor.cs b/src/Mockaco/Templating/TemplateResponseProcessor.cs index c34a5b4..c183c0f 100644 --- a/src/Mockaco/Templating/TemplateResponseProcessor.cs +++ b/src/Mockaco/Templating/TemplateResponseProcessor.cs @@ -1,7 +1,5 @@ using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; using Newtonsoft.Json; -using System.Globalization; using System.Net; using System.Threading.Tasks; @@ -9,33 +7,24 @@ namespace Mockaco { public class TemplateResponseProcessor : ITemplateResponseProcessor { - private readonly ILogger _logger; - - public TemplateResponseProcessor( - IMockacoContext mockacoContext, - ILogger logger) - { - _logger = logger; - } - public async Task PrepareResponse(HttpResponse httpResponse, IScriptContext scriptContext, Template template) { - httpResponse.StatusCode = template.Response.Status == 0 + httpResponse.StatusCode = template.Response.Status == default ? (int)HttpStatusCode.OK : (int)template.Response.Status; - WriteResponseHeaders(httpResponse, template); + WriteResponseHeaders(httpResponse, template.Response); - await WriteResponseBody(httpResponse, template); + await WriteResponseBody(httpResponse, template.Response); scriptContext.AttachResponse(httpResponse.Headers, template.Response.Body); } - private void WriteResponseHeaders(HttpResponse response, Template template) + private void WriteResponseHeaders(HttpResponse response, ResponseTemplate responseTemplate) { - if (template.Response.Headers != null) + if (responseTemplate.Headers != null) { - foreach (var header in template.Response.Headers) + foreach (var header in responseTemplate.Headers) { response.Headers.Add(header.Key, header.Value); } @@ -47,9 +36,11 @@ private void WriteResponseHeaders(HttpResponse response, Template template) } } - private async Task WriteResponseBody(HttpResponse response, Template template) + private async Task WriteResponseBody(HttpResponse response, ResponseTemplate responseTemplate) { - var responseBody = template.Response.Body?.ToString(); + var formatting = responseTemplate.Indented.GetValueOrDefault() ? Formatting.Indented : default; + + var responseBody = responseTemplate.Body?.ToString(formatting); if (response.ContentType != "application/json") { @@ -58,10 +49,8 @@ private async Task WriteResponseBody(HttpResponse response, Template template) if (responseBody != null) { - await response.WriteAsync(responseBody) - .ConfigureAwait(false); + await response.WriteAsync(responseBody); } } - } } \ No newline at end of file diff --git a/src/Mockaco/Templating/TemplateTransformer.cs b/src/Mockaco/Templating/TemplateTransformer.cs index 4a60b2a..ad6e84e 100644 --- a/src/Mockaco/Templating/TemplateTransformer.cs +++ b/src/Mockaco/Templating/TemplateTransformer.cs @@ -81,8 +81,7 @@ private async Task Transform(string input, IScriptContext scriptContext) return output.ToString(); } - - // TODO Remove repeated code + private async Task Run(string code, IScriptContext scriptContext) { object result = null; @@ -91,11 +90,11 @@ private async Task Run(string code, IScriptContext scriptContext) { result = await _scriptRunnerFactory.Invoke(scriptContext, code); - _logger.LogDebug($"Processed script {code} with result {result}"); + _logger.LogDebug("Processed script {code} with result {result}", code, result); } catch (Exception ex) { - _logger.LogError($"Processed script {code} with result {ex}"); + _logger.LogError(ex, "Processed script {code} with error", code); } return result;