BodyAsJson (#80)

This commit is contained in:
Stef Heyenrath
2018-01-31 12:41:10 +01:00
parent cdcaaa970a
commit 40ff8514ac
5 changed files with 97 additions and 22 deletions

View File

@@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json;
using WireMock.Validation; using WireMock.Validation;
namespace WireMock.Http namespace WireMock.Http
@@ -29,7 +31,6 @@ namespace WireMock.Http
var x509Certificate2 = CertificateUtil.GetCertificate(clientX509Certificate2ThumbprintOrSubjectName); var x509Certificate2 = CertificateUtil.GetCertificate(clientX509Certificate2ThumbprintOrSubjectName);
handler.ClientCertificates.Add(x509Certificate2); handler.ClientCertificates.Add(x509Certificate2);
#else #else
var webRequestHandler = new WebRequestHandler var webRequestHandler = new WebRequestHandler
{ {
ClientCertificateOptions = ClientCertificateOption.Manual, ClientCertificateOptions = ClientCertificateOption.Manual,
@@ -86,23 +87,46 @@ namespace WireMock.Http
// Call the URL // Call the URL
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead); var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);
// Transform response // Create transform response
var responseMessage = new ResponseMessage var responseMessage = new ResponseMessage { StatusCode = (int)httpResponseMessage.StatusCode };
{
StatusCode = (int)httpResponseMessage.StatusCode,
BodyAsBytes = await httpResponseMessage.Content.ReadAsByteArrayAsync(),
Body = await httpResponseMessage.Content.ReadAsStringAsync()
};
// Set both content and response headers, replacing URLs in values // Set both content and response headers, replacing URLs in values
var headers = httpResponseMessage.Content?.Headers.Union(httpResponseMessage.Headers); var headers = (httpResponseMessage.Content?.Headers.Union(httpResponseMessage.Headers) ?? Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>()).ToArray();
// In case the Content-Type header is application/json, try to set BodyAsJson, else set Body and BodyAsBytes.
bool bodyAsJson = false;
var contentTypeHeader = headers.FirstOrDefault(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase));
if (!contentTypeHeader.Equals(default(KeyValuePair<string, IEnumerable<string>>)) &&
contentTypeHeader.Value.Any(value => value != null && value.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)))
{
if (httpResponseMessage.Content != null)
{
string content = await httpResponseMessage.Content.ReadAsStringAsync();
try
{
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(content, new JsonSerializerSettings { Formatting = Formatting.Indented });
bodyAsJson = true;
}
catch
{
}
}
}
if (!bodyAsJson)
{
if (httpResponseMessage.Content != null)
{
responseMessage.BodyAsBytes = await httpResponseMessage.Content.ReadAsByteArrayAsync();
responseMessage.Body = await httpResponseMessage.Content.ReadAsStringAsync();
}
}
foreach (var header in headers) foreach (var header in headers)
{ {
// if Location header contains absolute redirect URL, and base URL is one that we proxy to, // If Location header contains absolute redirect URL, and base URL is one that we proxy to,
// we need to replace it to original one. // we need to replace it to original one.
if (string.Equals(header.Key, "Location", StringComparison.OrdinalIgnoreCase) if (string.Equals(header.Key, HttpKnownHeaderNames.Location, StringComparison.OrdinalIgnoreCase)
&& Uri.TryCreate(header.Value.First(), UriKind.Absolute, out Uri absoluteLocationUri) && Uri.TryCreate(header.Value.First(), UriKind.Absolute, out Uri absoluteLocationUri)
&& string.Equals(absoluteLocationUri.Host, requiredUri.Host, StringComparison.OrdinalIgnoreCase)) && string.Equals(absoluteLocationUri.Host, requiredUri.Host, StringComparison.OrdinalIgnoreCase))
{ {

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json;
using WireMock.Util; using WireMock.Util;
#if !NETSTANDARD #if !NETSTANDARD
using Microsoft.Owin; using Microsoft.Owin;
@@ -65,7 +66,7 @@ namespace WireMock.Owin
} }
} }
if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == null) if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == null && responseMessage.BodyAsJson == null)
{ {
return; return;
} }
@@ -84,11 +85,20 @@ namespace WireMock.Owin
return; return;
} }
Encoding encoding = responseMessage.BodyEncoding ?? _utf8NoBom; if (responseMessage.BodyAsJson != null)
using (var writer = new StreamWriter(response.Body, encoding)) {
string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyAsJson, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore });
using (var writer = new StreamWriter(response.Body, responseMessage.BodyEncoding ?? _utf8NoBom))
{
await writer.WriteAsync(jsonBody);
}
return;
}
using (var writer = new StreamWriter(response.Body, responseMessage.BodyEncoding ?? _utf8NoBom))
{ {
await writer.WriteAsync(responseMessage.Body); await writer.WriteAsync(responseMessage.Body);
// TODO : response.ContentLength = responseMessage.Body.Length;
} }
} }
} }

View File

@@ -37,6 +37,11 @@ namespace WireMock
/// </summary> /// </summary>
public string Body { get; set; } public string Body { get; set; }
/// <summary>
/// Gets or sets the body as a json object.
/// </summary>
public object BodyAsJson { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body as bytes. /// Gets or sets the body as bytes.
/// </summary> /// </summary>

View File

@@ -97,6 +97,7 @@ namespace WireMock.Serialization
mappingModel.Response.StatusCode = null; mappingModel.Response.StatusCode = null;
mappingModel.Response.Headers = null; mappingModel.Response.Headers = null;
mappingModel.Response.BodyDestination = null; mappingModel.Response.BodyDestination = null;
mappingModel.Response.BodyAsJson = null;
mappingModel.Response.Body = null; mappingModel.Response.Body = null;
mappingModel.Response.BodyAsBytes = null; mappingModel.Response.BodyAsBytes = null;
mappingModel.Response.BodyAsFile = null; mappingModel.Response.BodyAsFile = null;
@@ -110,6 +111,7 @@ namespace WireMock.Serialization
mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination; mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode; mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
mappingModel.Response.Headers = Map(response.ResponseMessage.Headers); mappingModel.Response.Headers = Map(response.ResponseMessage.Headers);
mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyAsJson;
mappingModel.Response.Body = response.ResponseMessage.Body; mappingModel.Response.Body = response.ResponseMessage.Body;
mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes; mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes;
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile; mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile;
@@ -150,7 +152,9 @@ namespace WireMock.Serialization
private static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers) private static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
{ {
if (matchers == null || !matchers.Any()) if (matchers == null || !matchers.Any())
{
return null; return null;
}
return matchers.Select(Map).Where(x => x != null).ToArray(); return matchers.Select(Map).Where(x => x != null).ToArray();
} }
@@ -158,7 +162,9 @@ namespace WireMock.Serialization
private static MatcherModel Map([CanBeNull] IMatcher matcher) private static MatcherModel Map([CanBeNull] IMatcher matcher)
{ {
if (matcher == null) if (matcher == null)
{
return null; return null;
}
var patterns = matcher.GetPatterns(); var patterns = matcher.GetPatterns();

View File

@@ -17,7 +17,7 @@ namespace WireMock.Net.Tests
private FluentMockServer _serverForProxyForwarding; private FluentMockServer _serverForProxyForwarding;
[Fact] [Fact]
public async Task FluentMockServer_Should_proxy_responses() public async Task FluentMockServer_Proxy_Should_proxy_responses()
{ {
// given // given
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
@@ -33,7 +33,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task FluentMockServer_Should_preserve_content_header_in_proxied_request() public async Task FluentMockServer_Proxy_Should_preserve_content_header_in_proxied_request()
{ {
// given // given
_serverForProxyForwarding = FluentMockServer.Start(); _serverForProxyForwarding = FluentMockServer.Start();
@@ -64,7 +64,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task FluentMockServer_Should_preserve_content_header_in_proxied_request_with_empty_content() public async Task FluentMockServer_Proxy_Should_preserve_content_header_in_proxied_request_with_empty_content()
{ {
// given // given
_serverForProxyForwarding = FluentMockServer.Start(); _serverForProxyForwarding = FluentMockServer.Start();
@@ -95,7 +95,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task FluentMockServer_Should_preserve_content_header_in_proxied_response() public async Task FluentMockServer_Proxy_Should_preserve_content_header_in_proxied_response()
{ {
// given // given
_serverForProxyForwarding = FluentMockServer.Start(); _serverForProxyForwarding = FluentMockServer.Start();
@@ -125,7 +125,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task FluentMockServer_Should_change_absolute_location_header_in_proxied_response() public async Task FluentMockServer_Proxy_Should_change_absolute_location_header_in_proxied_response()
{ {
// given // given
_serverForProxyForwarding = FluentMockServer.Start(); _serverForProxyForwarding = FluentMockServer.Start();
@@ -155,7 +155,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task FluentMockServer_Should_preserve_cookie_header_in_proxied_request() public async Task FluentMockServer_Proxy_Should_preserve_cookie_header_in_proxied_request()
{ {
// given // given
_serverForProxyForwarding = FluentMockServer.Start(); _serverForProxyForwarding = FluentMockServer.Start();
@@ -184,5 +184,35 @@ namespace WireMock.Net.Tests
Check.That(receivedRequest.Cookies).IsNotNull(); Check.That(receivedRequest.Cookies).IsNotNull();
Check.That(receivedRequest.Cookies).ContainsPair("name", "value"); Check.That(receivedRequest.Cookies).ContainsPair("name", "value");
} }
[Fact]
public async Task FluentMockServer_Proxy_Should_set_BodyAsJson_in_proxied_response()
{
// Assign
_serverForProxyForwarding = FluentMockServer.Start();
_serverForProxyForwarding
.Given(Request.Create().WithPath("/*"))
.RespondWith(Response.Create()
.WithBodyAsJson(new { i = 42 })
.WithHeader("Content-Type", "application/json; charset=utf-8"));
_server = FluentMockServer.Start();
_server
.Given(Request.Create().WithPath("/*"))
.RespondWith(Response.Create().WithProxy(_serverForProxyForwarding.Urls[0]));
// Act
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(_server.Urls[0])
};
var response = await new HttpClient().SendAsync(requestMessage);
// Assert
string content = await response.Content.ReadAsStringAsync();
Check.That(content).IsEqualTo("{\"i\":42}");
Check.That(response.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/json; charset=utf-8");
}
} }
} }