mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 17:41:01 +01:00
* Fixed #133 * tests: add parametrised tests that cover various body matchers for json * Code review comments (BodyAsStringOriginal) * tests: minor method name change
This commit is contained in:
@@ -105,27 +105,38 @@ namespace WireMock.Matchers.Request
|
||||
|
||||
private double IsMatch(RequestMessage requestMessage)
|
||||
{
|
||||
if (requestMessage.Body != null)
|
||||
{
|
||||
if (Matcher is IStringMatcher stringMatcher)
|
||||
{
|
||||
return stringMatcher.IsMatch(requestMessage.Body);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the matcher is a IObjectMatcher
|
||||
if (Matcher is IObjectMatcher objectMatcher)
|
||||
{
|
||||
// If the body is a JSON object, try to match.
|
||||
if (requestMessage.BodyAsJson != null)
|
||||
{
|
||||
return objectMatcher.IsMatch(requestMessage.BodyAsJson);
|
||||
}
|
||||
|
||||
// If the body is a byte array, try to match.
|
||||
if (requestMessage.BodyAsBytes != null)
|
||||
{
|
||||
return objectMatcher.IsMatch(requestMessage.BodyAsBytes);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the matcher is a IStringMatcher
|
||||
if (Matcher is IStringMatcher stringMatcher)
|
||||
{
|
||||
// If the body is a JSON object, try to use Body (string) to match.
|
||||
if (requestMessage.BodyAsJson != null && requestMessage.Body != null)
|
||||
{
|
||||
return stringMatcher.IsMatch(requestMessage.Body);
|
||||
}
|
||||
|
||||
// If the string body is defined, try to match.
|
||||
if (requestMessage.Body != null)
|
||||
{
|
||||
return stringMatcher.IsMatch(requestMessage.Body);
|
||||
}
|
||||
}
|
||||
|
||||
if (Func != null)
|
||||
{
|
||||
return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body));
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace WireMock
|
||||
public string RawQuery { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The body as string.
|
||||
/// The original body as string, this is defined when Body or BodyAsJson are not null.
|
||||
/// </summary>
|
||||
public string Body { get; }
|
||||
|
||||
@@ -113,7 +113,7 @@ namespace WireMock
|
||||
/// <param name="body">The body.</param>
|
||||
/// <param name="headers">The headers.</param>
|
||||
/// <param name="cookies">The cookies.</param>
|
||||
public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyData body, [CanBeNull] IDictionary<string, string[]> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
|
||||
public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyData body = null, [CanBeNull] IDictionary<string, string[]> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
|
||||
{
|
||||
Check.NotNull(url, nameof(url));
|
||||
Check.NotNull(method, nameof(method));
|
||||
@@ -140,43 +140,6 @@ namespace WireMock
|
||||
Query = ParseQuery(RawQuery);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
||||
/// </summary>
|
||||
/// <param name="url">The original url.</param>
|
||||
/// <param name="method">The HTTP method.</param>
|
||||
/// <param name="clientIP">The client IP Address.</param>
|
||||
/// <param name="bodyAsBytes">The bodyAsBytes byte[].</param>
|
||||
/// <param name="body">The body string.</param>
|
||||
/// <param name="bodyEncoding">The body encoding</param>
|
||||
/// <param name="headers">The headers.</param>
|
||||
/// <param name="cookies">The cookies.</param>
|
||||
public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [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(method, nameof(method));
|
||||
Check.NotNull(clientIP, nameof(clientIP));
|
||||
|
||||
Url = url.ToString();
|
||||
Protocol = url.Scheme;
|
||||
Host = url.Host;
|
||||
Port = url.Port;
|
||||
Origin = $"{url.Scheme}://{url.Host}:{url.Port}";
|
||||
Path = WebUtility.UrlDecode(url.AbsolutePath);
|
||||
PathSegments = Path.Split('/').Skip(1).ToArray();
|
||||
Method = method.ToLower();
|
||||
ClientIP = clientIP;
|
||||
|
||||
BodyAsBytes = bodyAsBytes;
|
||||
Body = body;
|
||||
BodyEncoding = bodyEncoding;
|
||||
|
||||
Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
|
||||
Cookies = cookies;
|
||||
RawQuery = WebUtility.UrlDecode(url.Query);
|
||||
Query = ParseQuery(RawQuery);
|
||||
}
|
||||
|
||||
private static IDictionary<string, WireMockList<string>> ParseQuery(string queryString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(queryString))
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace WireMock.Util
|
||||
public Encoding Encoding { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The body as string.
|
||||
/// The body as string, this is defined when BodyAsString or BodyAsJson are not null.
|
||||
/// </summary>
|
||||
public string BodyAsString { get; set; }
|
||||
|
||||
|
||||
@@ -58,6 +58,7 @@ namespace WireMock.Util
|
||||
else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var stringData = await ReadStringAsync(stream);
|
||||
data.BodyAsString = stringData.Item1;
|
||||
data.Encoding = stringData.Item2;
|
||||
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user