mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 16:58:27 +02:00
bug: fix supporting the Patch method and logging the body (#67)
* bug: fix supporting the Patch method and logging the body - Patch not configured to support Body - Add a unit test * chore: typo fixed * Added / reordered tests for PATCH method
This commit is contained in:
committed by
Stef Heyenrath
parent
208303729e
commit
798603118c
@@ -6,7 +6,6 @@ using System.Threading.Tasks;
|
|||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Admin.Requests;
|
using WireMock.Admin.Requests;
|
||||||
using WireMock.Admin.Settings;
|
using WireMock.Admin.Settings;
|
||||||
using WireMock.Logging;
|
|
||||||
|
|
||||||
namespace WireMock.Client
|
namespace WireMock.Client
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -91,8 +91,9 @@ namespace WireMock.Owin
|
|||||||
TRACE - Body not supported.
|
TRACE - Body not supported.
|
||||||
OPTIONS - Body supported but no semantics on usage (maybe in the future).
|
OPTIONS - Body supported but no semantics on usage (maybe in the future).
|
||||||
CONNECT - No defined body semantics
|
CONNECT - No defined body semantics
|
||||||
|
PATCH - Body supported.
|
||||||
*/
|
*/
|
||||||
return new[] { "PUT", "POST", "OPTIONS" }.Contains(method.ToUpper());
|
return new[] { "PUT", "POST", "OPTIONS", "PATCH" }.Contains(method.ToUpper());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,14 @@ namespace WireMock.RequestBuilders
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IMethodRequestBuilder : IHeadersAndCookiesRequestBuilder
|
public interface IMethodRequestBuilder : IHeadersAndCookiesRequestBuilder
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The using delete.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// The <see cref="IRequestBuilder"/>.
|
||||||
|
/// </returns>
|
||||||
|
IRequestBuilder UsingDelete();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The using get.
|
/// The using get.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -15,6 +23,14 @@ namespace WireMock.RequestBuilders
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
IRequestBuilder UsingGet();
|
IRequestBuilder UsingGet();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The using head.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// The <see cref="IRequestBuilder"/>.
|
||||||
|
/// </returns>
|
||||||
|
IRequestBuilder UsingHead();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The using post.
|
/// The using post.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -24,12 +40,12 @@ namespace WireMock.RequestBuilders
|
|||||||
IRequestBuilder UsingPost();
|
IRequestBuilder UsingPost();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The using delete.
|
/// The using patch.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// The <see cref="IRequestBuilder"/>.
|
/// The <see cref="IRequestBuilder"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
IRequestBuilder UsingDelete();
|
IRequestBuilder UsingPatch();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The using put.
|
/// The using put.
|
||||||
@@ -39,14 +55,6 @@ namespace WireMock.RequestBuilders
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
IRequestBuilder UsingPut();
|
IRequestBuilder UsingPut();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The using head.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// The <see cref="IRequestBuilder"/>.
|
|
||||||
/// </returns>
|
|
||||||
IRequestBuilder UsingHead();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The using any verb.
|
/// The using any verb.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -171,70 +171,49 @@ namespace WireMock.RequestBuilders
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingDelete"/>
|
||||||
/// The using get.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// The <see cref="IRequestBuilder"/>.
|
|
||||||
/// </returns>
|
|
||||||
public IRequestBuilder UsingGet()
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher("get"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The using post.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// The <see cref="IRequestBuilder"/>.
|
|
||||||
/// </returns>
|
|
||||||
public IRequestBuilder UsingPost()
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher("post"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The using put.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// The <see cref="IRequestBuilder"/>.
|
|
||||||
/// </returns>
|
|
||||||
public IRequestBuilder UsingPut()
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher("put"));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The using delete.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// The <see cref="IRequestBuilder"/>.
|
|
||||||
/// </returns>
|
|
||||||
public IRequestBuilder UsingDelete()
|
public IRequestBuilder UsingDelete()
|
||||||
{
|
{
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher("delete"));
|
_requestMatchers.Add(new RequestMessageMethodMatcher("delete"));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingGet"/>
|
||||||
/// The using head.
|
public IRequestBuilder UsingGet()
|
||||||
/// </summary>
|
{
|
||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
_requestMatchers.Add(new RequestMessageMethodMatcher("get"));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingHead"/>
|
||||||
public IRequestBuilder UsingHead()
|
public IRequestBuilder UsingHead()
|
||||||
{
|
{
|
||||||
_requestMatchers.Add(new RequestMessageMethodMatcher("head"));
|
_requestMatchers.Add(new RequestMessageMethodMatcher("head"));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingPost"/>
|
||||||
/// The using any verb.
|
public IRequestBuilder UsingPost()
|
||||||
/// </summary>
|
{
|
||||||
/// <returns>
|
_requestMatchers.Add(new RequestMessageMethodMatcher("post"));
|
||||||
/// The <see cref="IRequestBuilder"/>.
|
return this;
|
||||||
/// </returns>
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingPatch"/>
|
||||||
|
public IRequestBuilder UsingPatch()
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher("patch"));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingPut"/>
|
||||||
|
public IRequestBuilder UsingPut()
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageMethodMatcher("put"));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingAnyVerb"/>
|
||||||
public IRequestBuilder UsingAnyVerb()
|
public IRequestBuilder UsingAnyVerb()
|
||||||
{
|
{
|
||||||
var matchers = _requestMatchers.Where(m => m is RequestMessageMethodMatcher).ToList();
|
var matchers = _requestMatchers.Where(m => m is RequestMessageMethodMatcher).ToList();
|
||||||
@@ -246,11 +225,7 @@ namespace WireMock.RequestBuilders
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc cref="IMethodRequestBuilder.UsingVerb"/>
|
||||||
/// The using verb.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="verbs">The verbs.</param>
|
|
||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
|
||||||
public IRequestBuilder UsingVerb(params string[] verbs)
|
public IRequestBuilder UsingVerb(params string[] verbs)
|
||||||
{
|
{
|
||||||
Check.NotEmpty(verbs, nameof(verbs));
|
Check.NotEmpty(verbs, nameof(verbs));
|
||||||
|
|||||||
@@ -175,6 +175,34 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(requestLogged.RequestMessage.BodyAsBytes).IsNull();
|
Check.That(requestLogged.RequestMessage.BodyAsBytes).IsNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FluentMockServer_Should_respond_to_request_methodPatch()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
|
_server.Given(Request.Create().WithPath("/foo").UsingVerb("patch"))
|
||||||
|
.RespondWith(Response.Create().WithBody("hello patch"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
var msg = new HttpRequestMessage(new HttpMethod("patch"), new Uri("http://localhost:" + _server.Ports[0] + "/foo"))
|
||||||
|
{
|
||||||
|
Content = new StringContent("{\"data\": {\"attr\":\"value\"}}")
|
||||||
|
};
|
||||||
|
var response = await new HttpClient().SendAsync(msg);
|
||||||
|
|
||||||
|
// then
|
||||||
|
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
|
||||||
|
var responseBody = await response.Content.ReadAsStringAsync();
|
||||||
|
Check.That(responseBody).IsEqualTo("hello patch");
|
||||||
|
|
||||||
|
Check.That(_server.LogEntries).HasSize(1);
|
||||||
|
var requestLogged = _server.LogEntries.First();
|
||||||
|
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("patch");
|
||||||
|
Check.That(requestLogged.RequestMessage.Body).IsNotNull();
|
||||||
|
Check.That(requestLogged.RequestMessage.Body).IsEqualTo("{\"data\": {\"attr\":\"value\"}}");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task FluentMockServer_Should_respond_to_request_bodyAsString()
|
public async Task FluentMockServer_Should_respond_to_request_bodyAsString()
|
||||||
{
|
{
|
||||||
|
|||||||
113
test/WireMock.Net.Tests/RequestTests.PathAndMethod.cs
Normal file
113
test/WireMock.Net.Tests/RequestTests.PathAndMethod.cs
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using NFluent;
|
||||||
|
using Xunit;
|
||||||
|
using WireMock.RequestBuilders;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests
|
||||||
|
{
|
||||||
|
//[TestFixture]
|
||||||
|
public partial class RequestTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Should_specify_requests_matching_given_path_and_method_delete()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingDelete();
|
||||||
|
|
||||||
|
// when
|
||||||
|
string bodyAsString = "whatever";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_specify_requests_matching_given_path_and_method_get()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingGet();
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_specify_requests_matching_given_path_and_method_head()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingHead();
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_specify_requests_matching_given_path_and_method_post()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingPost();
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_specify_requests_matching_given_path_and_method_put()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingPut();
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_specify_requests_matching_given_path_and_method_patch()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingPatch();
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PATCH", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_exclude_requests_matching_given_path_but_not_http_method()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingPut();
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -97,92 +97,6 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_path_and_method_put()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo").UsingPut();
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_path_and_method_post()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo").UsingPost();
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_path_and_method_get()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo").UsingGet();
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_path_and_method_delete()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo").UsingDelete();
|
|
||||||
|
|
||||||
// when
|
|
||||||
string bodyAsString = "whatever";
|
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_path_and_method_head()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo").UsingHead();
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_exclude_requests_matching_given_path_but_not_http_method()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo").UsingPut();
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_exclude_requests_matching_given_http_method_but_not_url()
|
public void Should_exclude_requests_matching_given_http_method_but_not_url()
|
||||||
{
|
{
|
||||||
@@ -206,7 +120,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", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new [] { "tata" } } });
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
|||||||
Reference in New Issue
Block a user