Skip to content

Commit

Permalink
Closes #23, add support to response/callback indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
natenho committed Aug 1, 2019
1 parent 820fbc6 commit 5c6f6b1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
5 changes: 4 additions & 1 deletion src/Mockaco/Middlewares/CallbackMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Mockaco/Middlewares/RequestMatchingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mockaco.Middlewares
public class RequestMatchingMiddleware
{
private readonly RequestDelegate _next;

public RequestMatchingMiddleware(RequestDelegate next)
{
_next = next;
Expand Down
6 changes: 3 additions & 3 deletions src/Mockaco/Models/CallbackTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace Mockaco
{
public class CallbackTemplate
{
public bool? Condition { get; set; }

public string Method { get; set; }

public string Url { get; set; }
Expand All @@ -17,8 +15,10 @@ public class CallbackTemplate

public int? Timeout { get; set; }

public bool? Indented { get; set; }

public IDictionary<string, string> Headers { get; set; }

public JContainer Body { get; set; }
public JToken Body { get; set; }
}
}
9 changes: 6 additions & 3 deletions src/Mockaco/Models/ResponseTemplate.cs
Original file line number Diff line number Diff line change
@@ -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; }

Expand Down
33 changes: 11 additions & 22 deletions src/Mockaco/Templating/TemplateResponseProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Globalization;
using System.Net;
using System.Threading.Tasks;

namespace Mockaco
{
public class TemplateResponseProcessor : ITemplateResponseProcessor
{
private readonly ILogger<TemplateResponseProcessor> _logger;

public TemplateResponseProcessor(
IMockacoContext mockacoContext,
ILogger<TemplateResponseProcessor> 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);
}
Expand All @@ -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")
{
Expand All @@ -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);
}
}

}
}
7 changes: 3 additions & 4 deletions src/Mockaco/Templating/TemplateTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ private async Task<string> Transform(string input, IScriptContext scriptContext)

return output.ToString();
}

// TODO Remove repeated code

private async Task<object> Run(string code, IScriptContext scriptContext)
{
object result = null;
Expand All @@ -91,11 +90,11 @@ private async Task<object> Run(string code, IScriptContext scriptContext)
{
result = await _scriptRunnerFactory.Invoke<IScriptContext, object>(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;
Expand Down

0 comments on commit 5c6f6b1

Please sign in to comment.