mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-13 19:30:55 +02:00
23
src/WireMock.Net/Admin/Mappings/EncodingModel.cs
Normal file
23
src/WireMock.Net/Admin/Mappings/EncodingModel.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace WireMock.Admin.Mappings
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// EncodingModel
|
||||||
|
/// </summary>
|
||||||
|
public class EncodingModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Encoding CodePage
|
||||||
|
/// </summary>
|
||||||
|
public int CodePage { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Encoding EncodingName
|
||||||
|
/// </summary>
|
||||||
|
public string EncodingName { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Encoding WebName
|
||||||
|
/// </summary>
|
||||||
|
public string WebName { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace WireMock.Admin.Mappings
|
namespace WireMock.Admin.Mappings
|
||||||
{
|
{
|
||||||
@@ -39,6 +40,14 @@ namespace WireMock.Admin.Mappings
|
|||||||
/// </value>
|
/// </value>
|
||||||
public object BodyAsJson { get; set; }
|
public object BodyAsJson { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the body encoding.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The body encoding.
|
||||||
|
/// </value>
|
||||||
|
public EncodingModel BodyEncoding { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether [use transformer].
|
/// Gets or sets a value indicating whether [use transformer].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.Admin.Requests
|
namespace WireMock.Admin.Requests
|
||||||
@@ -66,5 +68,13 @@ namespace WireMock.Admin.Requests
|
|||||||
/// The body.
|
/// The body.
|
||||||
/// </value>
|
/// </value>
|
||||||
public string Body { get; set; }
|
public string Body { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the body encoding.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The body encoding.
|
||||||
|
/// </value>
|
||||||
|
public EncodingModel BodyEncoding { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using WireMock.Admin.Mappings;
|
||||||
|
|
||||||
namespace WireMock.Admin.Requests
|
namespace WireMock.Admin.Requests
|
||||||
{
|
{
|
||||||
@@ -26,5 +28,10 @@ namespace WireMock.Admin.Requests
|
|||||||
/// Gets or sets the original body.
|
/// Gets or sets the original body.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string BodyOriginal { get; set; }
|
public string BodyOriginal { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the body.
|
||||||
|
/// </summary>
|
||||||
|
public EncodingModel BodyEncoding { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace WireMock
|
namespace WireMock
|
||||||
{
|
{
|
||||||
@@ -21,16 +22,16 @@ namespace WireMock
|
|||||||
Uri url = listenerRequest.Url;
|
Uri url = listenerRequest.Url;
|
||||||
string verb = listenerRequest.HttpMethod;
|
string verb = listenerRequest.HttpMethod;
|
||||||
byte[] body = GetRequestBody(listenerRequest);
|
byte[] body = GetRequestBody(listenerRequest);
|
||||||
string bodyAsString = body != null ? listenerRequest.ContentEncoding.GetString(body) : null;
|
Encoding bodyEncoding = body != null ? listenerRequest.ContentEncoding : null;
|
||||||
|
string bodyAsString = bodyEncoding?.GetString(body);
|
||||||
var listenerHeaders = listenerRequest.Headers;
|
var listenerHeaders = listenerRequest.Headers;
|
||||||
var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]);
|
var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]);
|
||||||
var cookies = new Dictionary<string, string>();
|
var cookies = new Dictionary<string, string>();
|
||||||
|
|
||||||
foreach (Cookie cookie in listenerRequest.Cookies)
|
foreach (Cookie cookie in listenerRequest.Cookies)
|
||||||
cookies.Add(cookie.Name, cookie.Value);
|
cookies.Add(cookie.Name, cookie.Value);
|
||||||
|
|
||||||
var message = new RequestMessage(url, verb, body, bodyAsString, headers, cookies) { DateTime = DateTime.Now };
|
return new RequestMessage(url, verb, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now };
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -24,14 +24,16 @@ namespace WireMock
|
|||||||
|
|
||||||
responseMessage.Headers.ToList().ForEach(pair => listenerResponse.AddHeader(pair.Key, pair.Value));
|
responseMessage.Headers.ToList().ForEach(pair => listenerResponse.AddHeader(pair.Key, pair.Value));
|
||||||
|
|
||||||
if (responseMessage.Body != null)
|
if (responseMessage.Body == null)
|
||||||
{
|
return;
|
||||||
byte[] buffer = _utf8NoBom.GetBytes(responseMessage.Body);
|
|
||||||
listenerResponse.ContentEncoding = _utf8NoBom;
|
var encoding = responseMessage.BodyEncoding ?? _utf8NoBom;
|
||||||
listenerResponse.ContentLength64 = buffer.Length;
|
byte[] buffer = encoding.GetBytes(responseMessage.Body);
|
||||||
listenerResponse.OutputStream.Write(buffer, 0, buffer.Length);
|
|
||||||
listenerResponse.OutputStream.Flush();
|
listenerResponse.ContentEncoding = encoding;
|
||||||
}
|
listenerResponse.ContentLength64 = buffer.Length;
|
||||||
|
listenerResponse.OutputStream.Write(buffer, 0, buffer.Length);
|
||||||
|
listenerResponse.OutputStream.Flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace WireMock
|
namespace WireMock
|
||||||
{
|
{
|
||||||
@@ -57,6 +58,11 @@ namespace WireMock
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Body { get; }
|
public string Body { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the body encoding.
|
||||||
|
/// </summary>
|
||||||
|
public Encoding BodyEncoding { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -64,9 +70,10 @@ namespace WireMock
|
|||||||
/// <param name="verb">The verb.</param>
|
/// <param name="verb">The verb.</param>
|
||||||
/// <param name="bodyAsBytes">The bodyAsBytes byte[].</param>
|
/// <param name="bodyAsBytes">The bodyAsBytes byte[].</param>
|
||||||
/// <param name="body">The body string.</param>
|
/// <param name="body">The body string.</param>
|
||||||
|
/// <param name="bodyEncoding">The body encoding</param>
|
||||||
/// <param name="headers">The headers.</param>
|
/// <param name="headers">The headers.</param>
|
||||||
/// <param name="cookies">The cookies.</param>
|
/// <param name="cookies">The cookies.</param>
|
||||||
public RequestMessage([NotNull] Uri url, [NotNull] string verb, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] IDictionary<string, string> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
|
public RequestMessage([NotNull] Uri url, [NotNull] string verb, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] Encoding bodyEncoding = null, [CanBeNull] IDictionary<string, string> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
|
||||||
{
|
{
|
||||||
Check.NotNull(url, nameof(url));
|
Check.NotNull(url, nameof(url));
|
||||||
Check.NotNull(verb, nameof(verb));
|
Check.NotNull(verb, nameof(verb));
|
||||||
@@ -76,6 +83,7 @@ namespace WireMock
|
|||||||
Method = verb.ToLower();
|
Method = verb.ToLower();
|
||||||
BodyAsBytes = bodyAsBytes;
|
BodyAsBytes = bodyAsBytes;
|
||||||
Body = body;
|
Body = body;
|
||||||
|
BodyEncoding = bodyEncoding;
|
||||||
Headers = headers;
|
Headers = headers;
|
||||||
Cookies = cookies;
|
Cookies = cookies;
|
||||||
|
|
||||||
|
|||||||
@@ -12,15 +12,17 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// The with body.
|
/// The with body.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</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);
|
IResponseBuilder WithBody([NotNull] string body, [CanBeNull] Encoding encoding = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The with body.
|
/// The with body.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</param>
|
||||||
|
/// <param name="encoding">The body encoding.</param>
|
||||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
IResponseBuilder WithBodyAsJson([NotNull] object body);
|
IResponseBuilder WithBodyAsJson([NotNull] object body, [CanBeNull] Encoding encoding = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The with body as base64.
|
/// The with body as base64.
|
||||||
|
|||||||
@@ -143,12 +143,15 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// The with body.
|
/// The with body.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</param>
|
||||||
|
/// <param name="encoding">The body encoding.</param>
|
||||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
public IResponseBuilder WithBody(string body)
|
public IResponseBuilder WithBody(string body, Encoding encoding = null)
|
||||||
{
|
{
|
||||||
Check.NotNull(body, nameof(body));
|
Check.NotNull(body, nameof(body));
|
||||||
|
|
||||||
ResponseMessage.Body = body;
|
ResponseMessage.Body = body;
|
||||||
|
ResponseMessage.BodyEncoding = encoding ?? Encoding.UTF8;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,12 +159,22 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// The with body (AsJson object).
|
/// The with body (AsJson object).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</param>
|
||||||
|
/// <param name="encoding">The body encoding.</param>
|
||||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
public IResponseBuilder WithBodyAsJson(object body)
|
public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null)
|
||||||
{
|
{
|
||||||
Check.NotNull(body, nameof(body));
|
Check.NotNull(body, nameof(body));
|
||||||
|
|
||||||
ResponseMessage.Body = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore });
|
string jsonBody = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore });
|
||||||
|
|
||||||
|
if (encoding != null && !encoding.Equals(Encoding.UTF8))
|
||||||
|
{
|
||||||
|
jsonBody = encoding.GetString(Encoding.UTF8.GetBytes(jsonBody));
|
||||||
|
ResponseMessage.BodyEncoding = encoding;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResponseMessage.Body = jsonBody;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +188,11 @@ namespace WireMock.ResponseBuilders
|
|||||||
{
|
{
|
||||||
Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
|
Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
|
||||||
|
|
||||||
ResponseMessage.Body = (encoding ?? Encoding.UTF8).GetString(Convert.FromBase64String(bodyAsbase64));
|
encoding = encoding ?? Encoding.UTF8;
|
||||||
|
|
||||||
|
ResponseMessage.Body = encoding.GetString(Convert.FromBase64String(bodyAsbase64));
|
||||||
|
ResponseMessage.BodyEncoding = encoding;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace WireMock
|
namespace WireMock
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -27,6 +29,11 @@ namespace WireMock
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Body { get; set; }
|
public string Body { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the body encoding.
|
||||||
|
/// </summary>
|
||||||
|
public Encoding BodyEncoding { get; set; } = new UTF8Encoding(false);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The add header.
|
/// The add header.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using SimMetrics.Net;
|
using SimMetrics.Net;
|
||||||
@@ -292,14 +293,26 @@ namespace WireMock.Server
|
|||||||
Method = logEntry.RequestMessage.Method,
|
Method = logEntry.RequestMessage.Method,
|
||||||
Body = logEntry.RequestMessage.Body,
|
Body = logEntry.RequestMessage.Body,
|
||||||
Headers = logEntry.RequestMessage.Headers,
|
Headers = logEntry.RequestMessage.Headers,
|
||||||
Cookies = logEntry.RequestMessage.Cookies
|
Cookies = logEntry.RequestMessage.Cookies,
|
||||||
|
BodyEncoding = logEntry.RequestMessage.BodyEncoding != null ? new EncodingModel
|
||||||
|
{
|
||||||
|
EncodingName = logEntry.RequestMessage.BodyEncoding.EncodingName,
|
||||||
|
CodePage = logEntry.RequestMessage.BodyEncoding.CodePage,
|
||||||
|
WebName = logEntry.RequestMessage.BodyEncoding.WebName
|
||||||
|
} : null
|
||||||
},
|
},
|
||||||
Response = new LogResponseModel
|
Response = new LogResponseModel
|
||||||
{
|
{
|
||||||
StatusCode = logEntry.ResponseMessage.StatusCode,
|
StatusCode = logEntry.ResponseMessage.StatusCode,
|
||||||
Body = logEntry.ResponseMessage.Body,
|
Body = logEntry.ResponseMessage.Body,
|
||||||
BodyOriginal = logEntry.ResponseMessage.BodyOriginal,
|
BodyOriginal = logEntry.ResponseMessage.BodyOriginal,
|
||||||
Headers = logEntry.ResponseMessage.Headers
|
Headers = logEntry.ResponseMessage.Headers,
|
||||||
|
BodyEncoding = logEntry.ResponseMessage.BodyEncoding != null ? new EncodingModel
|
||||||
|
{
|
||||||
|
EncodingName = logEntry.ResponseMessage.BodyEncoding.EncodingName,
|
||||||
|
CodePage = logEntry.ResponseMessage.BodyEncoding.CodePage,
|
||||||
|
WebName = logEntry.ResponseMessage.BodyEncoding.WebName
|
||||||
|
} : null
|
||||||
},
|
},
|
||||||
MappingGuid = logEntry.MappingGuid,
|
MappingGuid = logEntry.MappingGuid,
|
||||||
RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel
|
RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel
|
||||||
@@ -418,11 +431,11 @@ namespace WireMock.Server
|
|||||||
responseBuilder = responseBuilder.WithHeaders(responseModel.Headers);
|
responseBuilder = responseBuilder.WithHeaders(responseModel.Headers);
|
||||||
|
|
||||||
if (responseModel.Body != null)
|
if (responseModel.Body != null)
|
||||||
responseBuilder = responseBuilder.WithBody(responseModel.Body);
|
responseBuilder = responseBuilder.WithBody(responseModel.Body, ToEncoding(responseModel.BodyEncoding));
|
||||||
else if (responseModel.BodyAsJson != null)
|
else if (responseModel.BodyAsJson != null)
|
||||||
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson);
|
responseBuilder = responseBuilder.WithBodyAsJson(responseModel.BodyAsJson, ToEncoding(responseModel.BodyEncoding));
|
||||||
else if (responseModel.BodyAsBase64 != null)
|
else if (responseModel.BodyAsBase64 != null)
|
||||||
responseBuilder = responseBuilder.WithBodyAsBase64(responseModel.BodyAsBase64);
|
responseBuilder = responseBuilder.WithBodyAsBase64(responseModel.BodyAsBase64, ToEncoding(responseModel.BodyEncoding));
|
||||||
|
|
||||||
if (responseModel.UseTransformer)
|
if (responseModel.UseTransformer)
|
||||||
responseBuilder = responseBuilder.WithTransformer();
|
responseBuilder = responseBuilder.WithTransformer();
|
||||||
@@ -500,7 +513,14 @@ namespace WireMock.Server
|
|||||||
Headers = response.ResponseMessage.Headers,
|
Headers = response.ResponseMessage.Headers,
|
||||||
Body = response.ResponseMessage.Body,
|
Body = response.ResponseMessage.Body,
|
||||||
UseTransformer = response.UseTransformer,
|
UseTransformer = response.UseTransformer,
|
||||||
Delay = response.Delay?.Milliseconds
|
Delay = response.Delay?.Milliseconds,
|
||||||
|
|
||||||
|
BodyEncoding = response.ResponseMessage.BodyEncoding != null ? new EncodingModel
|
||||||
|
{
|
||||||
|
EncodingName = response.ResponseMessage.BodyEncoding.EncodingName,
|
||||||
|
CodePage = response.ResponseMessage.BodyEncoding.CodePage,
|
||||||
|
WebName = response.ResponseMessage.BodyEncoding.WebName
|
||||||
|
} : null
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -590,5 +610,10 @@ namespace WireMock.Server
|
|||||||
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
|
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Encoding ToEncoding(EncodingModel encodingModel)
|
||||||
|
{
|
||||||
|
return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
@@ -67,6 +68,31 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
|
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Should_map_encoded_body_from_original_response()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var response = new ResponseMessage
|
||||||
|
{
|
||||||
|
Body = "Hello !!!",
|
||||||
|
BodyEncoding = Encoding.ASCII
|
||||||
|
};
|
||||||
|
|
||||||
|
var httpListenerResponse = CreateHttpListenerResponse();
|
||||||
|
|
||||||
|
// when
|
||||||
|
new HttpListenerResponseMapper().Map(response, httpListenerResponse);
|
||||||
|
|
||||||
|
// then
|
||||||
|
Check.That(httpListenerResponse.ContentEncoding).Equals(Encoding.ASCII);
|
||||||
|
|
||||||
|
var responseMessage = ToResponseMessage(httpListenerResponse);
|
||||||
|
Check.That(responseMessage).IsNotNull();
|
||||||
|
|
||||||
|
var contentTask = responseMessage.Content.ReadAsStringAsync();
|
||||||
|
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
|
||||||
|
}
|
||||||
|
|
||||||
[TearDown]
|
[TearDown]
|
||||||
public void StopServer()
|
public void StopServer()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ namespace WireMock.Net.Tests
|
|||||||
// given
|
// given
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(request.GetParameter("foo")).Contains("bar");
|
Check.That(request.GetParameter("foo")).Contains("bar");
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -204,7 +204,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary <string, string> { { "X-toto", "tata" } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -220,7 +220,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary <string, string> { { "X-toto", "tata" } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -236,7 +236,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "ABC" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary <string, string> { { "X-toto", "ABC" } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -252,7 +252,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "TaTa" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "TaTa" } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -266,7 +266,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*");
|
var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", null, null, null, new Dictionary<string, string> { { "session", "abc" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", null, null, null, null, new Dictionary<string, string> { { "session", "abc" } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -282,7 +282,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello world!";
|
string bodyAsString = "Hello world!";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -298,7 +298,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "cat";
|
string bodyAsString = "cat";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -314,7 +314,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "cat";
|
string bodyAsString = "cat";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -330,7 +330,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "caR";
|
string bodyAsString = "caR";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -346,7 +346,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "The car drives in the street.";
|
string bodyAsString = "The car drives in the street.";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -362,7 +362,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello";
|
string bodyAsString = "Hello";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -378,7 +378,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello world!";
|
string bodyAsString = "Hello world!";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "tatata" } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -394,7 +394,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello world!";
|
string bodyAsString = "Hello world!";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -415,7 +415,7 @@ namespace WireMock.Net.Tests
|
|||||||
<todo-item id='a3'>xyz</todo-item>
|
<todo-item id='a3'>xyz</todo-item>
|
||||||
</todo-list>";
|
</todo-list>";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -436,7 +436,7 @@ namespace WireMock.Net.Tests
|
|||||||
<todo-item id='a3'>xyz</todo-item>
|
<todo-item id='a3'>xyz</todo-item>
|
||||||
</todo-list>";
|
</todo-list>";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -452,7 +452,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -468,7 +468,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }";
|
string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -484,7 +484,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "xxx";
|
string bodyAsString = "xxx";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "tatata" } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace WireMock.Net.Tests
|
|||||||
// 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", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
var response = Response.Create()
|
var response = Response.Create()
|
||||||
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
|
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
|
||||||
@@ -36,7 +36,7 @@ namespace WireMock.Net.Tests
|
|||||||
// 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?a=1&a=2&b=5"), "POST", body, bodyAsString);
|
var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
var response = Response.Create()
|
var response = Response.Create()
|
||||||
.WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}")
|
.WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}")
|
||||||
@@ -55,7 +55,7 @@ namespace WireMock.Net.Tests
|
|||||||
// 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", body, bodyAsString, new Dictionary<string, string> { { "Content-Type", "text/plain" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "Content-Type", "text/plain" } });
|
||||||
|
|
||||||
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer();
|
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer();
|
||||||
|
|
||||||
@@ -66,5 +66,41 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(responseMessage.Body).Equals("test");
|
Check.That(responseMessage.Body).Equals("test");
|
||||||
Check.That(responseMessage.Headers).Contains(new KeyValuePair<string,string>("x", "text/plain"));
|
Check.That(responseMessage.Headers).Contains(new KeyValuePair<string,string>("x", "text/plain"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Response_ProvideResponse_Encoding_Body()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
string bodyAsString = "abc";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
var response = Response.Create().WithBody("test", Encoding.ASCII);
|
||||||
|
|
||||||
|
// act
|
||||||
|
var responseMessage = await response.ProvideResponse(request);
|
||||||
|
|
||||||
|
// then
|
||||||
|
Check.That(responseMessage.Body).Equals("test");
|
||||||
|
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Response_ProvideResponse_Encoding_JsonBody()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
string bodyAsString = "abc";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII);
|
||||||
|
|
||||||
|
// act
|
||||||
|
var responseMessage = await response.ProvideResponse(request);
|
||||||
|
|
||||||
|
// then
|
||||||
|
Check.That(responseMessage.Body).Equals("{\"value\":\"test\"}");
|
||||||
|
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user