Skip to content

Commit 6624540

Browse files
authored
RequestBuilder : add WithBodyAsJson and WithBody (with IJsonConverter) (#908)
* RequestBuilder : add WithBodyAsJson and WithBody (with IJsonConverter) * tests
1 parent da6cb9f commit 6624540

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using JsonConverter.Abstractions;
34
using WireMock.Matchers;
45
using WireMock.Matchers.Request;
56
using WireMock.Util;
@@ -46,10 +47,28 @@ public interface IBodyRequestBuilder : IRequestMatcher
4647
/// WithBody: Body as object
4748
/// </summary>
4849
/// <param name="body">The body.</param>
49-
/// <param name="matchBehaviour">The match behaviour.</param>
50+
/// <param name="matchBehaviour">The match behaviour [default is AcceptOnMatch].</param>
5051
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
5152
IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
5253

54+
/// <summary>
55+
/// WithBody : Body as a string response based on a object (which will be converted to a JSON string using NewtonSoft.Json).
56+
/// </summary>
57+
/// <param name="body">The body.</param>
58+
/// <param name="matchBehaviour">The match behaviour [default is AcceptOnMatch].</param>
59+
/// <returns>A <see cref="IRequestBuilder"/>.</returns>
60+
IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
61+
62+
/// <summary>
63+
/// WithBody : Body as a string response based on a object (which will be converted to a JSON string using the <see cref="IJsonConverter"/>).
64+
/// </summary>
65+
/// <param name="body">The body.</param>
66+
/// <param name="converter">The JsonConverter.</param>
67+
/// <param name="options">The <see cref="JsonConverterOptions"/> [optional].</param>
68+
/// <param name="matchBehaviour">The match behaviour [default is AcceptOnMatch].</param>
69+
/// <returns>A <see cref="IRequestBuilder"/>.</returns>
70+
IRequestBuilder WithBodyAsJson(object body, IJsonConverter converter, JsonConverterOptions? options = null, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
71+
5372
/// <summary>
5473
/// WithBody: func (string)
5574
/// </summary>

src/WireMock.Net/RequestBuilders/Request.WithBody.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
33
using System;
44
using System.Collections.Generic;
5+
using JsonConverter.Abstractions;
6+
using Newtonsoft.Json;
7+
using Stef.Validation;
58
using WireMock.Matchers;
69
using WireMock.Matchers.Request;
710
using WireMock.Util;
8-
using Stef.Validation;
911

1012
namespace WireMock.RequestBuilders;
1113

@@ -32,6 +34,24 @@ public IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = Mat
3234
return this;
3335
}
3436

37+
/// <inheritdoc />
38+
public IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
39+
{
40+
var bodyAsJsonString = JsonConvert.SerializeObject(body);
41+
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, bodyAsJsonString));
42+
return this;
43+
}
44+
45+
/// <inheritdoc />
46+
public IRequestBuilder WithBodyAsJson(object body, IJsonConverter converter, JsonConverterOptions? options = null, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
47+
{
48+
Guard.NotNull(converter);
49+
50+
var bodyAsJsonString = converter.Serialize(body, options);
51+
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, bodyAsJsonString));
52+
return this;
53+
}
54+
3555
/// <inheritdoc />
3656
public IRequestBuilder WithBody(IMatcher matcher)
3757
{

src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
9292
/// </summary>
9393
/// <param name="body">The body.</param>
9494
/// <param name="converter">The JsonConverter.</param>
95-
/// <param name="options">The IJsonConverterOption [optional].</param>
95+
/// <param name="options">The <see cref="JsonConverterOptions"/> [optional].</param>
9696
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
9797
IResponseBuilder WithBody(object body, IJsonConverter converter, JsonConverterOptions? options = null);
9898

@@ -102,7 +102,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
102102
/// <param name="body">The body.</param>
103103
/// <param name="encoding">The body encoding, can be <c>null</c>.</param>
104104
/// <param name="converter">The JsonConverter.</param>
105-
/// <param name="options">The IJsonConverterOption [optional].</param>
105+
/// <param name="options">The <see cref="JsonConverterOptions"/> [optional].</param>
106106
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
107107
IResponseBuilder WithBody(object body, Encoding? encoding, IJsonConverter converter, JsonConverterOptions? options = null);
108108
}

test/WireMock.Net.Tests/RequestWithBodyTests.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using FluentAssertions;
5+
using JsonConverter.Abstractions;
6+
using Moq;
57
using Newtonsoft.Json;
68
using NFluent;
79
using WireMock.Matchers;
@@ -134,7 +136,7 @@ public void Request_WithBodyExactMatcher()
134136
}
135137

136138
[Fact]
137-
public void Request_BodyAsString_Using_WildcardMatcher()
139+
public void Request_WithBody_BodyDataAsString_Using_WildcardMatcher()
138140
{
139141
// Arrange
140142
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("H*o*"));
@@ -153,7 +155,7 @@ public void Request_BodyAsString_Using_WildcardMatcher()
153155
}
154156

155157
[Fact]
156-
public void Request_BodyAsJson_Using_WildcardMatcher()
158+
public void Request_WithBody_BodyDataAsJson_Using_WildcardMatcher()
157159
{
158160
// Arrange
159161
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("*Hello*"));
@@ -349,6 +351,56 @@ public void Request_WithBodyAsObject_ExactObjectMatcher_true()
349351
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
350352
}
351353

354+
[Fact]
355+
public void Request_WithBodyAsJson_UsingObject()
356+
{
357+
// Assign
358+
object body = new
359+
{
360+
Test = "abc"
361+
};
362+
var requestBuilder = Request.Create().UsingAnyMethod().WithBodyAsJson(body);
363+
364+
var bodyData = new BodyData
365+
{
366+
BodyAsString = JsonConvert.SerializeObject(body),
367+
DetectedBodyType = BodyType.String
368+
};
369+
370+
// Act
371+
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, bodyData);
372+
373+
// Assert
374+
var requestMatchResult = new RequestMatchResult();
375+
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
376+
}
377+
378+
[Fact]
379+
public void Request_WithBodyAsJson_WithIJsonConverter_UsingObject()
380+
{
381+
// Assign
382+
var jsonConverterMock = new Mock<IJsonConverter>();
383+
jsonConverterMock.Setup(j => j.Serialize(It.IsAny<object>(), It.IsAny<JsonConverterOptions>())).Returns("test");
384+
object body = new
385+
{
386+
Any = "key"
387+
};
388+
var requestBuilder = Request.Create().UsingAnyMethod().WithBodyAsJson(body, jsonConverterMock.Object);
389+
390+
var bodyData = new BodyData
391+
{
392+
BodyAsString = "test",
393+
DetectedBodyType = BodyType.String
394+
};
395+
396+
// Act
397+
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, bodyData);
398+
399+
// Assert
400+
var requestMatchResult = new RequestMatchResult();
401+
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
402+
}
403+
352404
[Theory]
353405
[InlineData(new byte[] { 1 }, BodyType.Bytes)]
354406
[InlineData(new byte[] { 48, 49, 50 }, BodyType.Bytes)]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy