RequestBuilder : add WithBodyAsJson and WithBody (with IJsonConverter) (#908)

* RequestBuilder : add WithBodyAsJson and WithBody (with IJsonConverter)

* tests
This commit is contained in:
Stef Heyenrath
2023-03-22 15:47:58 +01:00
committed by GitHub
parent da6cb9fe0a
commit 66245409f9
4 changed files with 97 additions and 6 deletions

View File

@@ -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<IJsonConverter>();
jsonConverterMock.Setup(j => j.Serialize(It.IsAny<object>(), It.IsAny<JsonConverterOptions>())).Returns("test");
object body = new
{
Any = "key"
};
var requestBuilder = Request.Create().UsingAnyMethod().WithBodyAsJson(body, jsonConverterMock.Object);
var bodyData = new BodyData
{
BodyAsString = "test",
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);
}
[Theory]
[InlineData(new byte[] { 1 }, BodyType.Bytes)]
[InlineData(new byte[] { 48, 49, 50 }, BodyType.Bytes)]