diff --git a/src/Mockaco/Templating/TemplateResponseProcessor.cs b/src/Mockaco/Templating/TemplateResponseProcessor.cs index c183c0f..5f5ed49 100644 --- a/src/Mockaco/Templating/TemplateResponseProcessor.cs +++ b/src/Mockaco/Templating/TemplateResponseProcessor.cs @@ -7,10 +7,13 @@ namespace Mockaco { public class TemplateResponseProcessor : ITemplateResponseProcessor { + private const string JsonContentType = "application/json"; + private const HttpStatusCode DefaultHttpStatusCode = HttpStatusCode.OK; + public async Task PrepareResponse(HttpResponse httpResponse, IScriptContext scriptContext, Template template) { httpResponse.StatusCode = template.Response.Status == default - ? (int)HttpStatusCode.OK + ? (int)DefaultHttpStatusCode : (int)template.Response.Status; WriteResponseHeaders(httpResponse, template.Response); @@ -32,25 +35,33 @@ private void WriteResponseHeaders(HttpResponse response, ResponseTemplate respon if (string.IsNullOrEmpty(response.ContentType)) { - response.ContentType = "application/json"; + response.ContentType = JsonContentType; } } private async Task WriteResponseBody(HttpResponse response, ResponseTemplate responseTemplate) { - var formatting = responseTemplate.Indented.GetValueOrDefault() ? Formatting.Indented : default; + if(responseTemplate.Body == null) + { + return; + } - var responseBody = responseTemplate.Body?.ToString(formatting); + string responseBody; - if (response.ContentType != "application/json") + //TODO Move to a factory + if (response.ContentType == JsonContentType) { - responseBody = JsonConvert.DeserializeObject(responseBody); - } + var formatting = responseTemplate.Indented.GetValueOrDefault() ? Formatting.Indented : default; - if (responseBody != null) + responseBody = responseTemplate.Body?.ToString(formatting); + } + else { - await response.WriteAsync(responseBody); + //Deserializes the JSON string to unescape the body into its raw value + responseBody = JsonConvert.DeserializeObject(responseTemplate.Body?.ToString()); } + + await response.WriteAsync(responseBody); } } } \ No newline at end of file