diff --git a/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs
index 1477adf6..a6459c45 100644
--- a/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs
+++ b/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using JsonConverter.Abstractions;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Util;
@@ -46,10 +47,28 @@ public interface IBodyRequestBuilder : IRequestMatcher
/// WithBody: Body as object
///
/// The body.
- /// The match behaviour.
+ /// The match behaviour [default is AcceptOnMatch].
/// The .
IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
+ ///
+ /// WithBody : Body as a string response based on a object (which will be converted to a JSON string using NewtonSoft.Json).
+ ///
+ /// The body.
+ /// The match behaviour [default is AcceptOnMatch].
+ /// A .
+ IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
+
+ ///
+ /// WithBody : Body as a string response based on a object (which will be converted to a JSON string using the ).
+ ///
+ /// The body.
+ /// The JsonConverter.
+ /// The [optional].
+ /// The match behaviour [default is AcceptOnMatch].
+ /// A .
+ IRequestBuilder WithBodyAsJson(object body, IJsonConverter converter, JsonConverterOptions? options = null, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
+
///
/// WithBody: func (string)
///
diff --git a/src/WireMock.Net/RequestBuilders/Request.WithBody.cs b/src/WireMock.Net/RequestBuilders/Request.WithBody.cs
index ff1165c4..08ffa7b3 100644
--- a/src/WireMock.Net/RequestBuilders/Request.WithBody.cs
+++ b/src/WireMock.Net/RequestBuilders/Request.WithBody.cs
@@ -2,10 +2,12 @@
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System;
using System.Collections.Generic;
+using JsonConverter.Abstractions;
+using Newtonsoft.Json;
+using Stef.Validation;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Util;
-using Stef.Validation;
namespace WireMock.RequestBuilders;
@@ -32,6 +34,24 @@ public partial class Request
return this;
}
+ ///
+ public IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
+ {
+ var bodyAsJsonString = JsonConvert.SerializeObject(body);
+ _requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, bodyAsJsonString));
+ return this;
+ }
+
+ ///
+ public IRequestBuilder WithBodyAsJson(object body, IJsonConverter converter, JsonConverterOptions? options = null, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
+ {
+ Guard.NotNull(converter);
+
+ var bodyAsJsonString = converter.Serialize(body, options);
+ _requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, bodyAsJsonString));
+ return this;
+ }
+
///
public IRequestBuilder WithBody(IMatcher matcher)
{
diff --git a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs
index a9946f25..65318d21 100644
--- a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs
+++ b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs
@@ -92,7 +92,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
///
/// The body.
/// The JsonConverter.
- /// The IJsonConverterOption [optional].
+ /// The [optional].
/// A .
IResponseBuilder WithBody(object body, IJsonConverter converter, JsonConverterOptions? options = null);
@@ -102,7 +102,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
/// The body.
/// The body encoding, can be null.
/// The JsonConverter.
- /// The IJsonConverterOption [optional].
+ /// The [optional].
/// A .
IResponseBuilder WithBody(object body, Encoding? encoding, IJsonConverter converter, JsonConverterOptions? options = null);
}
\ No newline at end of file
diff --git a/test/WireMock.Net.Tests/RequestWithBodyTests.cs b/test/WireMock.Net.Tests/RequestWithBodyTests.cs
index e09817d9..561febe1 100644
--- a/test/WireMock.Net.Tests/RequestWithBodyTests.cs
+++ b/test/WireMock.Net.Tests/RequestWithBodyTests.cs
@@ -2,6 +2,8 @@ using System;
using System.Collections.Generic;
using System.Text;
using FluentAssertions;
+using JsonConverter.Abstractions;
+using Moq;
using Newtonsoft.Json;
using NFluent;
using WireMock.Matchers;
@@ -134,7 +136,7 @@ namespace WireMock.Net.Tests
}
[Fact]
- public void Request_BodyAsString_Using_WildcardMatcher()
+ public void Request_WithBody_BodyDataAsString_Using_WildcardMatcher()
{
// Arrange
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("H*o*"));
@@ -153,7 +155,7 @@ namespace WireMock.Net.Tests
}
[Fact]
- public void Request_BodyAsJson_Using_WildcardMatcher()
+ public void Request_WithBody_BodyDataAsJson_Using_WildcardMatcher()
{
// Arrange
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("*Hello*"));
@@ -349,6 +351,56 @@ namespace WireMock.Net.Tests
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
+ [Fact]
+ public void Request_WithBodyAsJson_UsingObject()
+ {
+ // Assign
+ object body = new
+ {
+ Test = "abc"
+ };
+ var requestBuilder = Request.Create().UsingAnyMethod().WithBodyAsJson(body);
+
+ var bodyData = new BodyData
+ {
+ BodyAsString = JsonConvert.SerializeObject(body),
+ DetectedBodyType = BodyType.String
+ };
+
+ // Act
+ var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, bodyData);
+
+ // Assert
+ var requestMatchResult = new RequestMatchResult();
+ Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
+ }
+
+ [Fact]
+ public void Request_WithBodyAsJson_WithIJsonConverter_UsingObject()
+ {
+ // Assign
+ var jsonConverterMock = new Mock();
+ jsonConverterMock.Setup(j => j.Serialize(It.IsAny