mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-04 22:55:06 +02:00
wip (#594)
This commit is contained in:
@@ -10,6 +10,7 @@ using WireMock.RequestBuilders;
|
|||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.Net.StandAlone.NETCoreApp
|
namespace WireMock.Net.StandAlone.NETCoreApp
|
||||||
{
|
{
|
||||||
@@ -34,6 +35,14 @@ namespace WireMock.Net.StandAlone.NETCoreApp
|
|||||||
|
|
||||||
_server = WireMockServer.Start(settings);
|
_server = WireMockServer.Start(settings);
|
||||||
|
|
||||||
|
_server.Given(Request.Create().WithPath("/api/sap")
|
||||||
|
.UsingPost()
|
||||||
|
.WithBody((IBodyData xmlData) => {
|
||||||
|
//xmlData is always null
|
||||||
|
return true;
|
||||||
|
}))
|
||||||
|
.RespondWith(Response.Create().WithStatusCode(System.Net.HttpStatusCode.OK));
|
||||||
|
|
||||||
_server
|
_server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
.UsingAnyMethod())
|
.UsingAnyMethod())
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using JetBrains.Annotations;
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.Matchers.Request
|
namespace WireMock.Matchers.Request
|
||||||
@@ -26,6 +27,11 @@ namespace WireMock.Matchers.Request
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<object, bool> JsonFunc { get; }
|
public Func<object, bool> JsonFunc { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The body data function for BodyData
|
||||||
|
/// </summary>
|
||||||
|
public Func<IBodyData, bool> BodyDataFunc { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The matchers.
|
/// The matchers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -88,6 +94,16 @@ namespace WireMock.Matchers.Request
|
|||||||
JsonFunc = func;
|
JsonFunc = func;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function.</param>
|
||||||
|
public RequestMessageBodyMatcher([NotNull] Func<IBodyData, bool> func)
|
||||||
|
{
|
||||||
|
Check.NotNull(func, nameof(func));
|
||||||
|
BodyDataFunc = func;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -158,6 +174,11 @@ namespace WireMock.Matchers.Request
|
|||||||
return MatchScores.ToScore(DataFunc(requestMessage?.BodyData?.BodyAsBytes));
|
return MatchScores.ToScore(DataFunc(requestMessage?.BodyData?.BodyAsBytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (BodyDataFunc != null)
|
||||||
|
{
|
||||||
|
return MatchScores.ToScore(BodyDataFunc(requestMessage?.BodyData));
|
||||||
|
}
|
||||||
|
|
||||||
return MatchScores.Mismatch;
|
return MatchScores.Mismatch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.RequestBuilders
|
namespace WireMock.RequestBuilders
|
||||||
{
|
{
|
||||||
@@ -63,10 +64,17 @@ namespace WireMock.RequestBuilders
|
|||||||
IRequestBuilder WithBody([NotNull] Func<byte[], bool> func);
|
IRequestBuilder WithBody([NotNull] Func<byte[], bool> func);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WithBody: func (object)
|
/// WithBody: func (json object)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="func">The function.</param>
|
/// <param name="func">The function.</param>
|
||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
IRequestBuilder WithBody([NotNull] Func<object, bool> func);
|
IRequestBuilder WithBody([NotNull] Func<object, bool> func);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WithBody: func (BodyData object)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func">The function.</param>
|
||||||
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
|
IRequestBuilder WithBody([NotNull] Func<IBodyData, bool> func);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.RequestBuilders
|
namespace WireMock.RequestBuilders
|
||||||
@@ -71,5 +72,14 @@ namespace WireMock.RequestBuilders
|
|||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{IBodyData, bool})"/>
|
||||||
|
public IRequestBuilder WithBody(Func<IBodyData, bool> func)
|
||||||
|
{
|
||||||
|
Check.NotNull(func, nameof(func));
|
||||||
|
|
||||||
|
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,25 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBody_FuncBodyData()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IBodyData b) => b != null);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsJson = 123,
|
||||||
|
DetectedBodyType = BodyType.Json
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Request_WithBody_FuncByteArray()
|
public void Request_WithBody_FuncByteArray()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user