mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-10 19:27:09 +02: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)
|
private double IsMatch(RequestMessage requestMessage)
|
||||||
{
|
{
|
||||||
if (requestMessage.Body != null)
|
// Check if the matcher is a IObjectMatcher
|
||||||
{
|
|
||||||
if (Matcher is IStringMatcher stringMatcher)
|
|
||||||
{
|
|
||||||
return stringMatcher.IsMatch(requestMessage.Body);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Matcher is IObjectMatcher objectMatcher)
|
if (Matcher is IObjectMatcher objectMatcher)
|
||||||
{
|
{
|
||||||
|
// If the body is a JSON object, try to match.
|
||||||
if (requestMessage.BodyAsJson != null)
|
if (requestMessage.BodyAsJson != null)
|
||||||
{
|
{
|
||||||
return objectMatcher.IsMatch(requestMessage.BodyAsJson);
|
return objectMatcher.IsMatch(requestMessage.BodyAsJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the body is a byte array, try to match.
|
||||||
if (requestMessage.BodyAsBytes != null)
|
if (requestMessage.BodyAsBytes != null)
|
||||||
{
|
{
|
||||||
return objectMatcher.IsMatch(requestMessage.BodyAsBytes);
|
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)
|
if (Func != null)
|
||||||
{
|
{
|
||||||
return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body));
|
return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body));
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ namespace WireMock
|
|||||||
public string RawQuery { get; }
|
public string RawQuery { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The body as string.
|
/// The original body as string, this is defined when Body or BodyAsJson are not null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Body { get; }
|
public string Body { get; }
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ namespace WireMock
|
|||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</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 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(url, nameof(url));
|
||||||
Check.NotNull(method, nameof(method));
|
Check.NotNull(method, nameof(method));
|
||||||
@@ -140,43 +140,6 @@ namespace WireMock
|
|||||||
Query = ParseQuery(RawQuery);
|
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)
|
private static IDictionary<string, WireMockList<string>> ParseQuery(string queryString)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(queryString))
|
if (string.IsNullOrEmpty(queryString))
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace WireMock.Util
|
|||||||
public Encoding Encoding { get; set; }
|
public Encoding Encoding { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The body as string.
|
/// The body as string, this is defined when BodyAsString or BodyAsJson are not null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string BodyAsString { get; set; }
|
public string BodyAsString { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ namespace WireMock.Util
|
|||||||
else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
|
else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
var stringData = await ReadStringAsync(stream);
|
var stringData = await ReadStringAsync(stream);
|
||||||
|
data.BodyAsString = stringData.Item1;
|
||||||
data.Encoding = stringData.Item2;
|
data.Encoding = stringData.Item2;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
@@ -18,6 +20,7 @@ namespace WireMock.Net.Tests
|
|||||||
public partial class FluentMockServerTests : IDisposable
|
public partial class FluentMockServerTests : IDisposable
|
||||||
{
|
{
|
||||||
private FluentMockServer _server;
|
private FluentMockServer _server;
|
||||||
|
private static string jsonRequestMessage = @"{ ""message"" : ""Hello server"" }";
|
||||||
|
|
||||||
// For for AppVeyor + OpenCover
|
// For for AppVeyor + OpenCover
|
||||||
private string GetCurrentFolder()
|
private string GetCurrentFolder()
|
||||||
@@ -348,6 +351,39 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(responseAsBytes).ContainsExactly(new byte[] { 48, 49 });
|
Check.That(responseAsBytes).ContainsExactly(new byte[] { 48, 49 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> ValidMatchersForHelloServerJsonMessage =>
|
||||||
|
new List<object[]>
|
||||||
|
{
|
||||||
|
new object[] { new WildcardMatcher("*Hello server*"), "application/json" },
|
||||||
|
new object[] { new WildcardMatcher("*Hello server*"), "text/plain" },
|
||||||
|
new object[] { new ExactMatcher(jsonRequestMessage), "application/json" },
|
||||||
|
new object[] { new ExactMatcher(jsonRequestMessage), "text/plain" },
|
||||||
|
new object[] { new RegexMatcher("Hello server"), "application/json" },
|
||||||
|
new object[] { new RegexMatcher("Hello server"), "text/plain" },
|
||||||
|
new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "application/json" },
|
||||||
|
new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "text/plain" }
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(ValidMatchersForHelloServerJsonMessage))]
|
||||||
|
public async Task FluentMockServer_Should_respond_to_valid_matchers_when_sent_json(IMatcher matcher, string contentType)
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
|
_server
|
||||||
|
.Given(Request.Create().WithPath("/foo").WithBody(matcher))
|
||||||
|
.RespondWith(Response.Create().WithBody("Hello client"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var content = new StringContent(jsonRequestMessage, Encoding.UTF8, contentType);
|
||||||
|
var response = await new HttpClient().PostAsync("http://localhost:" + _server.Ports[0] + "/foo", content);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var responseString = await response.Content.ReadAsStringAsync();
|
||||||
|
Check.That(responseString).Equals("Hello client");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task FluentMockServer_Should_respond_404_for_unexpected_request()
|
public async Task FluentMockServer_Should_respond_404_for_unexpected_request()
|
||||||
{
|
{
|
||||||
@@ -569,4 +605,4 @@ namespace WireMock.Net.Tests
|
|||||||
_serverForProxyForwarding?.Stop();
|
_serverForProxyForwarding?.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithCookie("session", "a*");
|
var spec = Request.Create().UsingAnyMethod().WithCookie("session", "a*");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, null, null, null, null, new Dictionary<string, string> { { "session", "abc" } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, null, null, new Dictionary<string, string> { { "session", "abc" } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
|||||||
@@ -87,7 +87,34 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
Check.That(score).IsEqualTo(0.0d);
|
Check.That(score).IsEqualTo(0.0d);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Never);
|
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_and_BodyAsString_IStringMatcher()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsJson = new { value = 42 },
|
||||||
|
BodyAsString = "orig"
|
||||||
|
};
|
||||||
|
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||||
|
|
||||||
|
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using NFluent;
|
|||||||
using Xunit;
|
using Xunit;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
@@ -33,9 +34,11 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "tatata");
|
var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "tatata");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
BodyAsString = "whatever"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -49,9 +52,11 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "abc", false);
|
var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "abc", false);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "ABC" } } });
|
BodyAsString = "whatever"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary<string, string[]> { { "X-toto", new[] { "ABC" } } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -65,17 +70,17 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "tata*");
|
var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "tata*");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "TaTa" } } });
|
BodyAsString = "whatever"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary<string, string[]> { { "X-toto", new[] { "TaTa" } } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_specify_requests_matching_given_body()
|
public void Should_specify_requests_matching_given_body()
|
||||||
{
|
{
|
||||||
@@ -83,16 +88,17 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithBody("Hello world!");
|
var spec = Request.Create().UsingAnyMethod().WithBody("Hello world!");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello world!";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "Hello world!"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_exclude_requests_not_matching_given_body()
|
public void Should_exclude_requests_not_matching_given_body()
|
||||||
{
|
{
|
||||||
@@ -100,9 +106,11 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithBody(" Hello world! ");
|
var spec = Request.Create().UsingAnyMethod().WithBody(" Hello world! ");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "xxx";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
BodyAsString = "xxx"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
@@ -76,9 +75,11 @@ namespace WireMock.Net.Tests
|
|||||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(new ExactMatcher("cat"));
|
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(new ExactMatcher("cat"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "cat";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "cat"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -92,9 +93,11 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("H*o*"));
|
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("H*o*"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello world!";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tatata" } } });
|
BodyAsString = "Hello world!"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -108,14 +111,16 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"));
|
var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string xmlBodyAsString = @"
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsString = @"
|
||||||
<todo-list>
|
<todo-list>
|
||||||
<todo-item id='a1'>abc</todo-item>
|
<todo-item id='a1'>abc</todo-item>
|
||||||
<todo-item id='a2'>def</todo-item>
|
<todo-item id='a2'>def</todo-item>
|
||||||
<todo-item id='a3'>xyz</todo-item>
|
<todo-item id='a3'>xyz</todo-item>
|
||||||
</todo-list>";
|
</todo-list>"
|
||||||
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
|
};
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, xmlBodyAsString, Encoding.UTF8);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -129,14 +134,16 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]"));
|
var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string xmlBodyAsString = @"
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsString = @"
|
||||||
<todo-list>
|
<todo-list>
|
||||||
<todo-item id='a1'>abc</todo-item>
|
<todo-item id='a1'>abc</todo-item>
|
||||||
<todo-item id='a2'>def</todo-item>
|
<todo-item id='a2'>def</todo-item>
|
||||||
<todo-item id='a3'>xyz</todo-item>
|
<todo-item id='a3'>xyz</todo-item>
|
||||||
</todo-list>";
|
</todo-list>"
|
||||||
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
|
};
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, xmlBodyAsString, Encoding.UTF8);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -150,9 +157,11 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -166,9 +175,11 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -186,6 +197,7 @@ namespace WireMock.Net.Tests
|
|||||||
var bodyData = new BodyData
|
var bodyData = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||||
|
BodyAsString = jsonString,
|
||||||
Encoding = Encoding.UTF8
|
Encoding = Encoding.UTF8
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -195,6 +207,7 @@ namespace WireMock.Net.Tests
|
|||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Request_WithBodyAsJson_Array_JsonPathMatcher_1()
|
public void Request_WithBodyAsJson_Array_JsonPathMatcher_1()
|
||||||
{
|
{
|
||||||
@@ -206,6 +219,7 @@ namespace WireMock.Net.Tests
|
|||||||
var bodyData = new BodyData
|
var bodyData = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||||
|
BodyAsString = jsonString,
|
||||||
Encoding = Encoding.UTF8
|
Encoding = Encoding.UTF8
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -227,6 +241,7 @@ namespace WireMock.Net.Tests
|
|||||||
var bodyData = new BodyData
|
var bodyData = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||||
|
BodyAsString = jsonString,
|
||||||
Encoding = Encoding.UTF8
|
Encoding = Encoding.UTF8
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
@@ -20,9 +20,11 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithHeader("X-toto", "tata");
|
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithHeader("X-toto", "tata");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -105,9 +107,11 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo").UsingDelete();
|
var spec = Request.Create().WithPath("/foo").UsingDelete();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "whatever"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", ClientIp, body);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
|||||||
@@ -41,9 +41,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "whatever"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body);
|
||||||
|
|
||||||
var response = Response.Create()
|
var response = Response.Create()
|
||||||
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
|
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
|
||||||
@@ -60,9 +62,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_Handlebars_Query()
|
public async Task Response_ProvideResponse_Handlebars_Query()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", ClientIp, body);
|
||||||
|
|
||||||
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}}")
|
||||||
@@ -79,9 +83,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_Handlebars_Header()
|
public async Task Response_ProvideResponse_Handlebars_Header()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "Content-Type", new[] { "text/plain" } } });
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, new Dictionary<string, string[]> { { "Content-Type", new[] { "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();
|
||||||
|
|
||||||
@@ -98,9 +104,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_Handlebars_Headers()
|
public async Task Response_ProvideResponse_Handlebars_Headers()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "Content-Type", new[] { "text/plain" } } });
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, new Dictionary<string, string[]> { { "Content-Type", new[] { "text/plain" } } });
|
||||||
|
|
||||||
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}", "{{request.url}}").WithBody("test").WithTransformer();
|
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}", "{{request.url}}").WithBody("test").WithTransformer();
|
||||||
|
|
||||||
@@ -118,9 +126,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_Handlebars_Origin_Port_Protocol_Host()
|
public async Task Response_ProvideResponse_Handlebars_Origin_Port_Protocol_Host()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost:1234"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost:1234"), "POST", ClientIp, body);
|
||||||
|
|
||||||
var response = Response.Create()
|
var response = Response.Create()
|
||||||
.WithBody("test {{request.origin}} {{request.port}} {{request.protocol}} {{request.host}}")
|
.WithBody("test {{request.origin}} {{request.port}} {{request.protocol}} {{request.host}}")
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
|
using WireMock.Util;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.ResponseBuilderTests
|
namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||||
@@ -15,9 +16,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_String()
|
public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_String()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body);
|
||||||
|
|
||||||
var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.String, Encoding.ASCII);
|
var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.String, Encoding.ASCII);
|
||||||
|
|
||||||
@@ -34,9 +37,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_Bytes()
|
public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_Bytes()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body);
|
||||||
|
|
||||||
var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.SameAsSource, Encoding.ASCII);
|
var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.SameAsSource, Encoding.ASCII);
|
||||||
|
|
||||||
@@ -53,9 +58,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_WithBody_String_Encoding()
|
public async Task Response_ProvideResponse_WithBody_String_Encoding()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body);
|
||||||
|
|
||||||
var response = Response.Create().WithBody("test", null, Encoding.ASCII);
|
var response = Response.Create().WithBody("test", null, Encoding.ASCII);
|
||||||
|
|
||||||
@@ -71,9 +78,11 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
public async Task Response_ProvideResponse_WithBody_Object_Encoding()
|
public async Task Response_ProvideResponse_WithBody_Object_Encoding()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
var body = new BodyData
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
{
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body);
|
||||||
|
|
||||||
object x = new { value = "test" };
|
object x = new { value = "test" };
|
||||||
var response = Response.Create().WithBodyAsJson(x, Encoding.ASCII);
|
var response = Response.Create().WithBodyAsJson(x, Encoding.ASCII);
|
||||||
@@ -137,7 +146,7 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
|||||||
// Assert
|
// Assert
|
||||||
Check.That(responseMessage.Body).IsNull();
|
Check.That(responseMessage.Body).IsNull();
|
||||||
Check.That(responseMessage.BodyAsBytes).IsNull();
|
Check.That(responseMessage.BodyAsBytes).IsNull();
|
||||||
Check.That(((dynamic) responseMessage.BodyAsJson).value).Equals(42);
|
Check.That(((dynamic)responseMessage.BodyAsJson).value).Equals(42);
|
||||||
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user