mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 23:33:47 +01:00
BodyAsJson (#80)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using WireMock.Validation;
|
||||
|
||||
namespace WireMock.Http
|
||||
@@ -29,7 +31,6 @@ namespace WireMock.Http
|
||||
var x509Certificate2 = CertificateUtil.GetCertificate(clientX509Certificate2ThumbprintOrSubjectName);
|
||||
handler.ClientCertificates.Add(x509Certificate2);
|
||||
#else
|
||||
|
||||
var webRequestHandler = new WebRequestHandler
|
||||
{
|
||||
ClientCertificateOptions = ClientCertificateOption.Manual,
|
||||
@@ -86,23 +87,46 @@ namespace WireMock.Http
|
||||
// Call the URL
|
||||
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);
|
||||
|
||||
// Transform response
|
||||
var responseMessage = new ResponseMessage
|
||||
{
|
||||
StatusCode = (int)httpResponseMessage.StatusCode,
|
||||
|
||||
BodyAsBytes = await httpResponseMessage.Content.ReadAsByteArrayAsync(),
|
||||
Body = await httpResponseMessage.Content.ReadAsStringAsync()
|
||||
};
|
||||
// Create transform response
|
||||
var responseMessage = new ResponseMessage { StatusCode = (int)httpResponseMessage.StatusCode };
|
||||
|
||||
// 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)
|
||||
{
|
||||
// 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.
|
||||
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)
|
||||
&& string.Equals(absoluteLocationUri.Host, requiredUri.Host, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using WireMock.Util;
|
||||
#if !NETSTANDARD
|
||||
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;
|
||||
}
|
||||
@@ -84,11 +85,20 @@ namespace WireMock.Owin
|
||||
return;
|
||||
}
|
||||
|
||||
Encoding encoding = responseMessage.BodyEncoding ?? _utf8NoBom;
|
||||
using (var writer = new StreamWriter(response.Body, encoding))
|
||||
if (responseMessage.BodyAsJson != null)
|
||||
{
|
||||
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);
|
||||
// TODO : response.ContentLength = responseMessage.Body.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,11 @@ namespace WireMock
|
||||
/// </summary>
|
||||
public string Body { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body as a json object.
|
||||
/// </summary>
|
||||
public object BodyAsJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body as bytes.
|
||||
/// </summary>
|
||||
|
||||
@@ -97,6 +97,7 @@ namespace WireMock.Serialization
|
||||
mappingModel.Response.StatusCode = null;
|
||||
mappingModel.Response.Headers = null;
|
||||
mappingModel.Response.BodyDestination = null;
|
||||
mappingModel.Response.BodyAsJson = null;
|
||||
mappingModel.Response.Body = null;
|
||||
mappingModel.Response.BodyAsBytes = null;
|
||||
mappingModel.Response.BodyAsFile = null;
|
||||
@@ -110,6 +111,7 @@ namespace WireMock.Serialization
|
||||
mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
|
||||
mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
|
||||
mappingModel.Response.Headers = Map(response.ResponseMessage.Headers);
|
||||
mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyAsJson;
|
||||
mappingModel.Response.Body = response.ResponseMessage.Body;
|
||||
mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes;
|
||||
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile;
|
||||
@@ -150,7 +152,9 @@ namespace WireMock.Serialization
|
||||
private static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
|
||||
{
|
||||
if (matchers == null || !matchers.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return matchers.Select(Map).Where(x => x != null).ToArray();
|
||||
}
|
||||
@@ -158,7 +162,9 @@ namespace WireMock.Serialization
|
||||
private static MatcherModel Map([CanBeNull] IMatcher matcher)
|
||||
{
|
||||
if (matcher == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var patterns = matcher.GetPatterns();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user