Fix issues with Bytes for Response

This commit is contained in:
Stef Heyenrath
2017-10-14 10:48:58 +02:00
parent 7c289d44a7
commit 07f03997c0
16 changed files with 815 additions and 498 deletions

View File

@@ -1,5 +1,4 @@
using System; using System;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
@@ -21,7 +20,7 @@ namespace WireMock.Net.ConsoleApplication
{ {
Urls = new[] { url1, url2, url3 }, Urls = new[] { url1, url2, url3 },
StartAdminInterface = true, StartAdminInterface = true,
ReadStaticMappings = true ReadStaticMappings = false
}); });
System.Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls)); System.Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls));
@@ -29,6 +28,14 @@ namespace WireMock.Net.ConsoleApplication
// server.AllowPartialMapping(); // server.AllowPartialMapping();
server
.Given(Request.Create().WithPath("/bodyasbytes.png")
.UsingGet())
.RespondWith(Response.Create()
.WithHeader("Content-Type", "image/png")
.WithBody(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMTczbp9jAAAAJ0lEQVQoU2NgUPuPD6Hz0RCEAtJoiAxpCCBXGgmRIo0TofORkdp/AMiMdRVnV6O0AAAAAElFTkSuQmCC"))
);
server server
.Given(Request.Create().WithPath("/oauth2/access").UsingPost().WithBody("grant_type=password;username=u;password=p")) .Given(Request.Create().WithPath("/oauth2/access").UsingPost().WithBody("grant_type=password;username=u;password=p"))
.RespondWith(Response.Create() .RespondWith(Response.Create()

View File

@@ -15,6 +15,11 @@ namespace WireMock.Admin.Mappings
/// </value> /// </value>
public int? StatusCode { get; set; } public int? StatusCode { get; set; }
/// <summary>
/// Gets or sets the body destination (SameAsSource, String or Bytes).
/// </summary>
public string BodyDestination { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body. /// Gets or sets the body.
/// </summary> /// </summary>
@@ -29,7 +34,7 @@ namespace WireMock.Admin.Mappings
/// <value> /// <value>
/// The body. /// The body.
/// </value> /// </value>
public string BodyAsBase64 { get; set; } public string BodyFromBase64 { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body (as JSON object). /// Gets or sets the body (as JSON object).
@@ -39,6 +44,14 @@ namespace WireMock.Admin.Mappings
/// </value> /// </value>
public object BodyAsJson { get; set; } public object BodyAsJson { get; set; }
/// <summary>
/// Gets or sets the body (as bytearray).
/// </summary>
/// <value>
/// The body.
/// </value>
public byte[] BodyAsBytes { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body encoding. /// Gets or sets the body encoding.
/// </summary> /// </summary>

View File

@@ -18,11 +18,21 @@ namespace WireMock.Admin.Requests
/// </summary> /// </summary>
public IDictionary<string, string> Headers { get; set; } public IDictionary<string, string> Headers { get; set; }
/// <summary>
/// Gets or sets the body destination (SameAsSource, String or Bytes).
/// </summary>
public string BodyDestination { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body. /// Gets or sets the body.
/// </summary> /// </summary>
public string Body { get; set; } public string Body { get; set; }
/// <summary>
/// Gets or sets the body.
/// </summary>
public byte[] BodyAsBytes { get; set; }
/// <summary> /// <summary>
/// Gets or sets the original body. /// Gets or sets the original body.
/// </summary> /// </summary>

View File

@@ -71,11 +71,12 @@ 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 // Transform response
var responseMessage = new ResponseMessage var responseMessage = new ResponseMessage
{ {
StatusCode = (int)httpResponseMessage.StatusCode, StatusCode = (int)httpResponseMessage.StatusCode,
// TODO : what about BodyAsBytes ???
Body = await httpResponseMessage.Content.ReadAsStringAsync() Body = await httpResponseMessage.Content.ReadAsStringAsync()
}; };

View File

@@ -0,0 +1,96 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace WireMock.Http
{
/// <summary>
/// Copied from https://raw.githubusercontent.com/dotnet/corefx/master/src/Common/src/System/Net/HttpKnownHeaderNames.cs
/// </summary>
internal static class HttpKnownHeaderNames
{
public const string Accept = "Accept";
public const string AcceptCharset = "Accept-Charset";
public const string AcceptEncoding = "Accept-Encoding";
public const string AcceptLanguage = "Accept-Language";
public const string AcceptPatch = "Accept-Patch";
public const string AcceptRanges = "Accept-Ranges";
public const string AccessControlAllowCredentials = "Access-Control-Allow-Credentials";
public const string AccessControlAllowHeaders = "Access-Control-Allow-Headers";
public const string AccessControlAllowMethods = "Access-Control-Allow-Methods";
public const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
public const string AccessControlExposeHeaders = "Access-Control-Expose-Headers";
public const string AccessControlMaxAge = "Access-Control-Max-Age";
public const string Age = "Age";
public const string Allow = "Allow";
public const string AltSvc = "Alt-Svc";
public const string Authorization = "Authorization";
public const string CacheControl = "Cache-Control";
public const string Connection = "Connection";
public const string ContentDisposition = "Content-Disposition";
public const string ContentEncoding = "Content-Encoding";
public const string ContentLanguage = "Content-Language";
public const string ContentLength = "Content-Length";
public const string ContentLocation = "Content-Location";
public const string ContentMD5 = "Content-MD5";
public const string ContentRange = "Content-Range";
public const string ContentSecurityPolicy = "Content-Security-Policy";
public const string ContentType = "Content-Type";
public const string Cookie = "Cookie";
public const string Cookie2 = "Cookie2";
public const string Date = "Date";
public const string ETag = "ETag";
public const string Expect = "Expect";
public const string Expires = "Expires";
public const string From = "From";
public const string Host = "Host";
public const string IfMatch = "If-Match";
public const string IfModifiedSince = "If-Modified-Since";
public const string IfNoneMatch = "If-None-Match";
public const string IfRange = "If-Range";
public const string IfUnmodifiedSince = "If-Unmodified-Since";
public const string KeepAlive = "Keep-Alive";
public const string LastModified = "Last-Modified";
public const string Link = "Link";
public const string Location = "Location";
public const string MaxForwards = "Max-Forwards";
public const string Origin = "Origin";
public const string P3P = "P3P";
public const string Pragma = "Pragma";
public const string ProxyAuthenticate = "Proxy-Authenticate";
public const string ProxyAuthorization = "Proxy-Authorization";
public const string ProxyConnection = "Proxy-Connection";
public const string PublicKeyPins = "Public-Key-Pins";
public const string Range = "Range";
public const string Referer = "Referer"; // NB: The spelling-mistake "Referer" for "Referrer" must be matched.
public const string RetryAfter = "Retry-After";
public const string SecWebSocketAccept = "Sec-WebSocket-Accept";
public const string SecWebSocketExtensions = "Sec-WebSocket-Extensions";
public const string SecWebSocketKey = "Sec-WebSocket-Key";
public const string SecWebSocketProtocol = "Sec-WebSocket-Protocol";
public const string SecWebSocketVersion = "Sec-WebSocket-Version";
public const string Server = "Server";
public const string SetCookie = "Set-Cookie";
public const string SetCookie2 = "Set-Cookie2";
public const string StrictTransportSecurity = "Strict-Transport-Security";
public const string TE = "TE";
public const string TSV = "TSV";
public const string Trailer = "Trailer";
public const string TransferEncoding = "Transfer-Encoding";
public const string Upgrade = "Upgrade";
public const string UpgradeInsecureRequests = "Upgrade-Insecure-Requests";
public const string UserAgent = "User-Agent";
public const string Vary = "Vary";
public const string Via = "Via";
public const string WWWAuthenticate = "WWW-Authenticate";
public const string Warning = "Warning";
public const string XAspNetVersion = "X-AspNet-Version";
public const string XContentDuration = "X-Content-Duration";
public const string XContentTypeOptions = "X-Content-Type-Options";
public const string XFrameOptions = "X-Frame-Options";
public const string XMSEdgeRef = "X-MSEdge-Ref";
public const string XPoweredBy = "X-Powered-By";
public const string XRequestID = "X-Request-ID";
public const string XUACompatible = "X-UA-Compatible";
}
}

View File

@@ -46,7 +46,7 @@ namespace WireMock.Owin
string bodyAsString = null; string bodyAsString = null;
byte[] body = null; byte[] body = null;
Encoding bodyEncoding = null; Encoding bodyEncoding = null;
if (request.Body != null) if (ParseBody(method) && request.Body != null)
{ {
using (var streamReader = new StreamReader(request.Body)) using (var streamReader = new StreamReader(request.Body))
{ {
@@ -57,18 +57,42 @@ namespace WireMock.Owin
body = bodyEncoding.GetBytes(bodyAsString); body = bodyEncoding.GetBytes(bodyAsString);
} }
var listenerHeaders = request.Headers; Dictionary<string, string> headers = null;
if (request.Headers.Any())
{
headers = new Dictionary<string, string>();
foreach (var header in request.Headers)
{
headers.Add(header.Key, header.Value.FirstOrDefault());
}
}
var headers = new Dictionary<string, string>(); IDictionary<string, string> cookies = null;
foreach (var header in listenerHeaders) if (request.Cookies.Any())
headers.Add(header.Key, header.Value.FirstOrDefault()); {
cookies = new Dictionary<string, string>();
var cookies = new Dictionary<string, string>(); foreach (var cookie in request.Cookies)
{
foreach (var cookie in request.Cookies) cookies.Add(cookie.Key, cookie.Value);
cookies.Add(cookie.Key, cookie.Value); }
}
return new RequestMessage(url, method, clientIP, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now }; return new RequestMessage(url, method, clientIP, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now };
} }
private bool ParseBody(string method)
{
/*
HEAD - No defined body semantics.
GET - No defined body semantics.
PUT - Body supported.
POST - Body supported.
DELETE - No defined body semantics.
TRACE - Body not supported.
OPTIONS - Body supported but no semantics on usage (maybe in the future).
CONNECT - No defined body semantics
*/
return new[] { "PUT", "POST", "OPTIONS" }.Contains(method.ToUpper());
}
} }
} }

View File

@@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using WireMock.Http;
#if !NETSTANDARD #if !NETSTANDARD
using Microsoft.Owin; using Microsoft.Owin;
#else #else
@@ -32,15 +33,28 @@ namespace WireMock.Owin
{ {
response.StatusCode = responseMessage.StatusCode; response.StatusCode = responseMessage.StatusCode;
responseMessage.Headers.ToList().ForEach(pair => response.Headers.Append(pair.Key, pair.Value)); if (responseMessage.Headers.ContainsKey(HttpKnownHeaderNames.ContentType))
{
response.ContentType = responseMessage.Headers[HttpKnownHeaderNames.ContentType];
}
responseMessage.Headers.Where(h => h.Key != HttpKnownHeaderNames.ContentType).ToList().ForEach(pair => response.Headers.Append(pair.Key, pair.Value));
if (responseMessage.Body == null) if (responseMessage.Body == null && responseMessage.BodyAsBytes == null)
{
return; return;
}
if (responseMessage.BodyAsBytes != null)
{
await response.Body.WriteAsync(responseMessage.BodyAsBytes, 0, responseMessage.BodyAsBytes.Length);
return;
}
Encoding encoding = responseMessage.BodyEncoding ?? _utf8NoBom; Encoding encoding = responseMessage.BodyEncoding ?? _utf8NoBom;
using (var writer = new StreamWriter(response.Body, encoding)) using (var writer = new StreamWriter(response.Body, encoding))
{ {
await writer.WriteAsync(responseMessage.Body); await writer.WriteAsync(responseMessage.Body);
// TODO : response.ContentLength = responseMessage.Body.Length;
} }
} }
} }

View File

@@ -51,7 +51,7 @@ namespace WireMock
/// <summary> /// <summary>
/// Gets the query. /// Gets the query.
/// </summary> /// </summary>
public IDictionary<string, WireMockList<string>> Query { get; } = new Dictionary<string, WireMockList<string>>(); public IDictionary<string, WireMockList<string>> Query { get; }
/// <summary> /// <summary>
/// Gets the bodyAsBytes. /// Gets the bodyAsBytes.
@@ -94,34 +94,38 @@ namespace WireMock
BodyEncoding = bodyEncoding; BodyEncoding = bodyEncoding;
Headers = headers; Headers = headers;
Cookies = cookies; Cookies = cookies;
Query = ParseQuery(url.Query);
}
string query = url.Query; private IDictionary<string, WireMockList<string>> ParseQuery(string queryString)
if (!string.IsNullOrEmpty(query)) {
if (string.IsNullOrEmpty(queryString))
{ {
if (query.StartsWith("?")) return null;
{
query = query.Substring(1);
}
Query = query.Split('&').Aggregate(
new Dictionary<string, WireMockList<string>>(),
(dict, term) =>
{
var parts = term.Split('=');
string key = parts[0];
if (!dict.ContainsKey(key))
{
dict.Add(key, new WireMockList<string>());
}
if (parts.Length == 2)
{
dict[key].Add(parts[1]);
}
return dict;
});
} }
if (queryString.StartsWith("?"))
{
queryString = queryString.Substring(1);
}
return queryString.Split('&').Aggregate(new Dictionary<string, WireMockList<string>>(),
(dict, term) =>
{
var parts = term.Split('=');
string key = parts[0];
if (!dict.ContainsKey(key))
{
dict.Add(key, new WireMockList<string>());
}
if (parts.Length == 2)
{
dict[key].Add(parts[1]);
}
return dict;
});
} }
/// <summary> /// <summary>
@@ -131,6 +135,11 @@ namespace WireMock
/// <returns>The query parameter.</returns> /// <returns>The query parameter.</returns>
public List<string> GetParameter(string key) public List<string> GetParameter(string key)
{ {
if (Query == null)
{
return null;
}
return Query.ContainsKey(key) ? Query[key] : null; return Query.ContainsKey(key) ? Query[key] : null;
} }
} }

View File

@@ -0,0 +1,20 @@
namespace WireMock.ResponseBuilders
{
public static class BodyDestinationFormat
{
/// <summary>
/// Same as source (no conversion)
/// </summary>
public const string SameAsSource = "SameAsSource";
/// <summary>
/// Convert to string
/// </summary>
public const string String = "String";
/// <summary>
/// Convert to bytes
/// </summary>
public const string Bytes = "Bytes";
}
}

View File

@@ -1,4 +1,5 @@
using System.Text; using System;
using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
namespace WireMock.ResponseBuilders namespace WireMock.ResponseBuilders
@@ -8,16 +9,34 @@ namespace WireMock.ResponseBuilders
/// </summary> /// </summary>
public interface IBodyResponseBuilder : ITransformResponseBuilder public interface IBodyResponseBuilder : ITransformResponseBuilder
{ {
/// <summary> ///// <summary>
/// The with body. ///// WithBody : Create a string response based on a string.
/// </summary> ///// </summary>
/// <param name="body">The body.</param> ///// <param name="body">The body.</param>
/// <param name="encoding">The body encoding.</param> ///// <param name="encoding">The body encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns> ///// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithBody([NotNull] string body, [CanBeNull] Encoding encoding = null); //// IResponseBuilder WithBody([NotNull] string body, [CanBeNull] Encoding encoding = null);
/// <summary> /// <summary>
/// The with body as Json. /// WithBody : Create a ... response based on a string.
/// </summary>
/// <param name="body">The body.</param>
/// <param name="destination">The Body Destination format (SameAsSource, String or Bytes).</param>
/// <param name="encoding">The body encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithBody([NotNull] string body, [CanBeNull] string destination = BodyDestinationFormat.SameAsSource, [CanBeNull] Encoding encoding = null);
/// <summary>
/// WithBody : Create a ... response based on a bytearray.
/// </summary>
/// <param name="body">The body.</param>
/// <param name="destination">The Body Destination format (SameAsSource, String or Bytes).</param>
/// <param name="encoding">The body encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithBody([NotNull] byte[] body, [CanBeNull] string destination = BodyDestinationFormat.SameAsSource, [CanBeNull] Encoding encoding = null);
/// <summary>
/// WithBody : Create a string response based on a object (which will be converted to a JSON string).
/// </summary> /// </summary>
/// <param name="body">The body.</param> /// <param name="body">The body.</param>
/// <param name="encoding">The body encoding.</param> /// <param name="encoding">The body encoding.</param>
@@ -25,11 +44,12 @@ namespace WireMock.ResponseBuilders
IResponseBuilder WithBodyAsJson([NotNull] object body, [CanBeNull] Encoding encoding = null); IResponseBuilder WithBodyAsJson([NotNull] object body, [CanBeNull] Encoding encoding = null);
/// <summary> /// <summary>
/// The with body as base64. /// WithBody : Create a string response based on a Base64 string (which will be decoded to a normal string).
/// </summary> /// </summary>
/// <param name="bodyAsbase64">The body asbase64.</param> /// <param name="bodyAsbase64">The body.</param>
/// <param name="encoding">The Encoding.</param> /// <param name="encoding">The Encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns> /// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null); [Obsolete]
IResponseBuilder WithBodyFromBase64([NotNull] string bodyAsbase64, [CanBeNull] Encoding encoding = null);
} }
} }

View File

@@ -150,28 +150,59 @@ namespace WireMock.ResponseBuilders
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/>
/// The with body. public IResponseBuilder WithBody(byte[] body, string destination, Encoding encoding = null)
/// </summary>
/// <param name="body">The body.</param>
/// <param name="encoding">The body encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
public IResponseBuilder WithBody(string body, Encoding encoding = null)
{ {
Check.NotNull(body, nameof(body)); Check.NotNull(body, nameof(body));
ResponseMessage.Body = body; ResponseMessage.BodyDestination = destination;
ResponseMessage.BodyEncoding = encoding ?? Encoding.UTF8;
switch (destination)
{
case BodyDestinationFormat.String:
var enc = encoding ?? Encoding.UTF8;
ResponseMessage.BodyAsBytes = null;
ResponseMessage.Body = enc.GetString(body);
ResponseMessage.BodyEncoding = enc;
break;
default:
ResponseMessage.BodyAsBytes = body;
ResponseMessage.BodyEncoding = null;
break;
}
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IBodyResponseBuilder.WithBody(string, string, Encoding)"/>
/// The with body (AsJson object). public IResponseBuilder WithBody(string body, string destination = BodyDestinationFormat.SameAsSource, Encoding encoding = null)
/// </summary> {
/// <param name="body">The body.</param> Check.NotNull(body, nameof(body));
/// <param name="encoding">The body encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns> encoding = encoding ?? Encoding.UTF8;
ResponseMessage.BodyDestination = destination;
switch (destination)
{
case BodyDestinationFormat.Bytes:
ResponseMessage.Body = null;
ResponseMessage.BodyAsBytes = encoding.GetBytes(body);
ResponseMessage.BodyEncoding = encoding;
break;
default:
ResponseMessage.Body = body;
ResponseMessage.BodyAsBytes = null;
ResponseMessage.BodyEncoding = encoding;
break;
}
return this;
}
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson"/>
public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null) public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null)
{ {
Check.NotNull(body, nameof(body)); Check.NotNull(body, nameof(body));
@@ -184,23 +215,20 @@ namespace WireMock.ResponseBuilders
ResponseMessage.BodyEncoding = encoding; ResponseMessage.BodyEncoding = encoding;
} }
ResponseMessage.BodyDestination = null;
ResponseMessage.Body = jsonBody; ResponseMessage.Body = jsonBody;
return this; return this;
} }
/// <summary> /// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromBase64"/>
/// The with body as base64. public IResponseBuilder WithBodyFromBase64(string bodyAsbase64, Encoding encoding = null)
/// </summary>
/// <param name="bodyAsbase64">The body asbase64.</param>
/// <param name="encoding">The Encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
public IResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null)
{ {
Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
encoding = encoding ?? Encoding.UTF8; encoding = encoding ?? Encoding.UTF8;
ResponseMessage.BodyDestination = null;
ResponseMessage.Body = encoding.GetString(Convert.FromBase64String(bodyAsbase64)); ResponseMessage.Body = encoding.GetString(Convert.FromBase64String(bodyAsbase64));
ResponseMessage.BodyEncoding = encoding; ResponseMessage.BodyEncoding = encoding;

View File

@@ -24,11 +24,21 @@ namespace WireMock
/// </summary> /// </summary>
public string BodyOriginal { get; set; } public string BodyOriginal { get; set; }
/// <summary>
/// Gets or sets the body destination (SameAsSource, String or Bytes).
/// </summary>
public string BodyDestination { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body. /// Gets or sets the body.
/// </summary> /// </summary>
public string Body { get; set; } public string Body { get; set; }
/// <summary>
/// Gets or sets the body.
/// </summary>
public byte[] BodyAsBytes { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body encoding. /// Gets or sets the body encoding.
/// </summary> /// </summary>

View File

@@ -95,16 +95,20 @@ 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.Body = null; mappingModel.Response.Body = null;
mappingModel.Response.BodyAsBytes = null;
mappingModel.Response.UseTransformer = false; mappingModel.Response.UseTransformer = false;
mappingModel.Response.BodyEncoding = null; mappingModel.Response.BodyEncoding = null;
mappingModel.Response.ProxyUrl = response.ProxyUrl; mappingModel.Response.ProxyUrl = response.ProxyUrl;
} }
else else
{ {
mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode; mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
mappingModel.Response.Headers = response.ResponseMessage.Headers; mappingModel.Response.Headers = response.ResponseMessage.Headers;
mappingModel.Response.Body = response.ResponseMessage.Body; mappingModel.Response.Body = response.ResponseMessage.Body;
mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes;
mappingModel.Response.UseTransformer = response.UseTransformer; mappingModel.Response.UseTransformer = response.UseTransformer;
mappingModel.Response.BodyEncoding = response.ResponseMessage.BodyEncoding != null mappingModel.Response.BodyEncoding = response.ResponseMessage.BodyEncoding != null
? new EncodingModel ? new EncodingModel

View File

@@ -412,7 +412,9 @@ namespace WireMock.Server
Response = new LogResponseModel Response = new LogResponseModel
{ {
StatusCode = logEntry.ResponseMessage.StatusCode, StatusCode = logEntry.ResponseMessage.StatusCode,
BodyDestination = logEntry.ResponseMessage.BodyDestination,
Body = logEntry.ResponseMessage.Body, Body = logEntry.ResponseMessage.Body,
BodyAsBytes = logEntry.ResponseMessage.BodyAsBytes,
BodyOriginal = logEntry.ResponseMessage.BodyOriginal, BodyOriginal = logEntry.ResponseMessage.BodyOriginal,
Headers = logEntry.ResponseMessage.Headers, Headers = logEntry.ResponseMessage.Headers,
BodyEncoding = logEntry.ResponseMessage.BodyEncoding != null ? new EncodingModel BodyEncoding = logEntry.ResponseMessage.BodyEncoding != null ? new EncodingModel
@@ -612,17 +614,21 @@ namespace WireMock.Server
} }
} }
if (responseModel.Body != null) if (responseModel.BodyAsBytes != null)
{ {
responseBuilder = responseBuilder.WithBody(responseModel.Body, ToEncoding(responseModel.BodyEncoding)); responseBuilder = responseBuilder.WithBody(responseModel.BodyAsBytes, responseModel.BodyDestination, ToEncoding(responseModel.BodyEncoding));
}
else if (responseModel.Body != null)
{
responseBuilder = responseBuilder.WithBody(responseModel.Body, responseModel.BodyDestination, ToEncoding(responseModel.BodyEncoding));
} }
else if (responseModel.BodyAsJson != null) else if (responseModel.BodyAsJson != null)
{ {
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding)); responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding));
} }
else if (responseModel.BodyAsBase64 != null) else if (responseModel.BodyFromBase64 != null)
{ {
responseBuilder = responseBuilder.WithBodyAsBase64(responseModel.BodyAsBase64, ToEncoding(responseModel.BodyEncoding)); responseBuilder = responseBuilder.WithBodyFromBase64(responseModel.BodyFromBase64, ToEncoding(responseModel.BodyEncoding));
} }
if (responseModel.UseTransformer) if (responseModel.UseTransformer)

View File

@@ -174,11 +174,11 @@ namespace WireMock.Net.Tests
Check.That(_server.LogEntries).HasSize(1); Check.That(_server.LogEntries).HasSize(1);
var requestLogged = _server.LogEntries.First(); var requestLogged = _server.LogEntries.First();
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("get"); Check.That(requestLogged.RequestMessage.Method).IsEqualTo("get");
Check.That(requestLogged.RequestMessage.BodyAsBytes).IsEmpty(); Check.That(requestLogged.RequestMessage.BodyAsBytes).IsNull();
} }
[Fact] [Fact]
public async Task Should_respond_to_request() public async Task Should_respond_to_request_bodyAsString()
{ {
// given // given
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
@@ -189,13 +189,13 @@ namespace WireMock.Net.Tests
.UsingGet()) .UsingGet())
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(200) .WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}")); .WithBody("Hello world!"));
// when // when
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo"); var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
// then // then
Check.That(response).IsEqualTo(@"{ msg: ""Hello world!""}"); Check.That(response).IsEqualTo("Hello world!");
} }
[Fact] [Fact]
@@ -204,7 +204,7 @@ namespace WireMock.Net.Tests
// given // given
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
_server.Given(Request.Create().WithPath("/foo").UsingGet()).RespondWith(Response.Create().WithBodyAsBase64("SGVsbG8gV29ybGQ/")); _server.Given(Request.Create().WithPath("/foo").UsingGet()).RespondWith(Response.Create().WithBodyFromBase64("SGVsbG8gV29ybGQ/"));
// when // when
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo"); var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
@@ -213,6 +213,23 @@ namespace WireMock.Net.Tests
Check.That(response).IsEqualTo("Hello World?"); Check.That(response).IsEqualTo("Hello World?");
} }
[Fact]
public async Task Should_respond_to_request_bodyAsBytes()
{
// given
_server = FluentMockServer.Start();
_server.Given(Request.Create().WithPath("/foo").UsingGet()).RespondWith(Response.Create().WithBody(new byte[] { 48, 49 }));
// when
var responseAsString = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
var responseAsBytes = await new HttpClient().GetByteArrayAsync("http://localhost:" + _server.Ports[0] + "/foo");
// then
Check.That(responseAsString).IsEqualTo("01");
Check.That(responseAsBytes).ContainsExactly(new byte[] { 48, 49 });
}
[Fact] [Fact]
public async Task Should_respond_404_for_unexpected_request() public async Task Should_respond_404_for_unexpected_request()
{ {

View File

@@ -70,14 +70,52 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task Response_ProvideResponse_Encoding_Body() public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_String()
{ {
// given // given
string bodyAsString = "abc"; string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString); byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
var response = Response.Create().WithBody("test", Encoding.ASCII); var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.String, Encoding.ASCII);
// act
var responseMessage = await response.ProvideResponseAsync(request);
// then
Check.That(responseMessage.Body).Equals("01");
Check.That(responseMessage.BodyAsBytes).IsNull();
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
}
[Fact]
public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_Bytes()
{
// given
string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.SameAsSource, Encoding.ASCII);
// act
var responseMessage = await response.ProvideResponseAsync(request);
// then
Check.That(responseMessage.BodyAsBytes).ContainsExactly(new byte[] { 48, 49 });
Check.That(responseMessage.Body).IsNull();
Check.That(responseMessage.BodyEncoding).IsNull();
}
[Fact]
public async Task Response_ProvideResponse_WithBody_String_Encoding()
{
// given
string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
var response = Response.Create().WithBody("test", null, Encoding.ASCII);
// act // act
var responseMessage = await response.ProvideResponseAsync(request); var responseMessage = await response.ProvideResponseAsync(request);
@@ -88,7 +126,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task Response_ProvideResponse_Encoding_JsonBody() public async Task Response_ProvideResponse_WithBody_Object_Encoding()
{ {
// given // given
string bodyAsString = "abc"; string bodyAsString = "abc";