mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 07:51:41 +02:00
Fix Request.Create().WithBodyAsJson(...) (#1111)
* Fix Request.Create().WithBodyAsJson(...) * [CodeFactor] Apply fixes --------- Co-authored-by: codefactor-io <support@codefactor.io>
This commit is contained in:
@@ -97,34 +97,30 @@ message HelloReply {
|
|||||||
|
|
||||||
private static void RunOnLocal()
|
private static void RunOnLocal()
|
||||||
{
|
{
|
||||||
var localIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);
|
|
||||||
|
|
||||||
//try
|
|
||||||
//{
|
|
||||||
// var server = WireMockServer.Start(new WireMockServerSettings
|
|
||||||
// {
|
|
||||||
// Urls = new[] { $"http://{localIP}:9091" },
|
|
||||||
// StartAdminInterface = true
|
|
||||||
// });
|
|
||||||
// System.Console.WriteLine($"1: {string.Join(", ", server.Urls)}");
|
|
||||||
|
|
||||||
// System.Console.WriteLine("Press any key to stop...");
|
|
||||||
// System.Console.ReadKey();
|
|
||||||
// server.Stop();
|
|
||||||
//}
|
|
||||||
//catch (Exception e)
|
|
||||||
//{
|
|
||||||
// System.Console.WriteLine(e);
|
|
||||||
//}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
{
|
{
|
||||||
Port = 9091,
|
Port = 9091,
|
||||||
StartAdminInterface = true
|
StartAdminInterface = true,
|
||||||
|
Logger = new WireMockConsoleLogger()
|
||||||
});
|
});
|
||||||
System.Console.WriteLine($"2: {string.Join(", ", server.Urls)}");
|
System.Console.WriteLine(string.Join(", ", server.Urls));
|
||||||
|
|
||||||
|
var requestJson = new { PricingContext = new { Market = "USA" } };
|
||||||
|
var responseJson = new { Market = "{{JsonPath.SelectToken request.body \"$.PricingContext.Market\"}}" };
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
//.WithBody(new JsonMatcher(requestJson))
|
||||||
|
.WithBodyAsJson(requestJson)
|
||||||
|
.WithPath("/pricing")
|
||||||
|
.UsingPost()
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBodyAsJson(responseJson)
|
||||||
|
.WithTransformer(true)
|
||||||
|
);
|
||||||
|
|
||||||
System.Console.WriteLine("Press any key to stop...");
|
System.Console.WriteLine("Press any key to stop...");
|
||||||
System.Console.ReadKey();
|
System.Console.ReadKey();
|
||||||
@@ -134,24 +130,6 @@ message HelloReply {
|
|||||||
{
|
{
|
||||||
System.Console.WriteLine(e);
|
System.Console.WriteLine(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
//try
|
|
||||||
//{
|
|
||||||
// var server = WireMockServer.Start(new WireMockServerSettings
|
|
||||||
// {
|
|
||||||
// Urls = new[] { "http://*:9091" },
|
|
||||||
// StartAdminInterface = true
|
|
||||||
// });
|
|
||||||
// System.Console.WriteLine($"3: {string.Join(", ", server.Urls)}");
|
|
||||||
|
|
||||||
// System.Console.WriteLine("Press any key to stop...");
|
|
||||||
// System.Console.ReadKey();
|
|
||||||
// server.Stop();
|
|
||||||
//}
|
|
||||||
//catch (Exception e)
|
|
||||||
//{
|
|
||||||
// System.Console.WriteLine(e);
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Run()
|
public static void Run()
|
||||||
@@ -251,7 +229,9 @@ message HelloReply {
|
|||||||
server.SetBasicAuthentication("a", "b");
|
server.SetBasicAuthentication("a", "b");
|
||||||
//server.SetAzureADAuthentication("6c2a4722-f3b9-4970-b8fc-fac41e29stef", "8587fde1-7824-42c7-8592-faf92b04stef");
|
//server.SetAzureADAuthentication("6c2a4722-f3b9-4970-b8fc-fac41e29stef", "8587fde1-7824-42c7-8592-faf92b04stef");
|
||||||
|
|
||||||
// server.AllowPartialMapping();
|
//var http = new HttpClient();
|
||||||
|
//var response = await http.GetAsync($"{_wireMockServer.Url}/pricing");
|
||||||
|
//var value = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
#if PROTOBUF
|
#if PROTOBUF
|
||||||
var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { name = "*" });
|
var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { name = "*" });
|
||||||
@@ -395,7 +375,6 @@ message HelloReply {
|
|||||||
.WithHeader("Content-Type", "text/plain")
|
.WithHeader("Content-Type", "text/plain")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
.UsingMethod("GET")
|
.UsingMethod("GET")
|
||||||
|
|||||||
@@ -51,23 +51,13 @@ public interface IBodyRequestBuilder : IProtoBufRequestBuilder
|
|||||||
IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WithBody : Body as a string response based on a object (which will be converted to a JSON string using NewtonSoft.Json).
|
/// WithBodyAsJson: A <see cref="JsonMatcher"/> will be used to match this object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</param>
|
||||||
/// <param name="matchBehaviour">The match behaviour [default is AcceptOnMatch].</param>
|
/// <param name="matchBehaviour">The match behaviour [default is AcceptOnMatch].</param>
|
||||||
/// <returns>A <see cref="IRequestBuilder"/>.</returns>
|
/// <returns>A <see cref="IRequestBuilder"/>.</returns>
|
||||||
IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// WithBody : Body as a string response based on a object (which will be converted to a JSON string using the <see cref="IJsonConverter"/>).
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="body">The body.</param>
|
|
||||||
/// <param name="converter">The JsonConverter.</param>
|
|
||||||
/// <param name="options">The <see cref="JsonConverterOptions"/> [optional].</param>
|
|
||||||
/// <param name="matchBehaviour">The match behaviour [default is AcceptOnMatch].</param>
|
|
||||||
/// <returns>A <see cref="IRequestBuilder"/>.</returns>
|
|
||||||
IRequestBuilder WithBodyAsJson(object body, IJsonConverter converter, JsonConverterOptions? options = null, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WithBody: func (string)
|
/// WithBody: func (string)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
|
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using JsonConverter.Abstractions;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
@@ -37,19 +35,7 @@ public partial class Request
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
public IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
{
|
{
|
||||||
var bodyAsJsonString = JsonConvert.SerializeObject(body);
|
return WithBody(new IMatcher[] { new JsonMatcher(matchBehaviour, body) });
|
||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, bodyAsJsonString));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ using FluentAssertions;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using JsonConverter.Abstractions;
|
|
||||||
using Moq;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
@@ -293,7 +291,7 @@ public class RequestBuilderWithBodyTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Request_WithBodyAsJson_Object_JsonPathMatcher_true()
|
public void Request_WithBody_Object_JsonPathMatcher_true()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
||||||
@@ -316,7 +314,7 @@ public class RequestBuilderWithBodyTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Request_WithBodyAsJson_Array_JsonPathMatcher_1()
|
public void Request_WithBody_Array_JsonPathMatcher_1()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..books[?(@.price < 10)]"));
|
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..books[?(@.price < 10)]"));
|
||||||
@@ -339,7 +337,7 @@ public class RequestBuilderWithBodyTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Request_WithBodyAsJson_Array_JsonPathMatcher_2()
|
public void Request_WithBody_Array_JsonPathMatcher_2()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..[?(@.Id == 1)]"));
|
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..[?(@.Id == 1)]"));
|
||||||
@@ -363,7 +361,7 @@ public class RequestBuilderWithBodyTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Request_WithBodyAsObject_ExactObjectMatcher_true()
|
public void Request_WithBody_ExactObjectMatcher_true()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
object body = DateTime.MinValue;
|
object body = DateTime.MinValue;
|
||||||
@@ -384,7 +382,7 @@ public class RequestBuilderWithBodyTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Request_WithBodyAsJson_UsingObject()
|
public void Request_WithBodyAsJson_UsingObject_UsesJsonMatcher()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
object body = new
|
object body = new
|
||||||
@@ -395,34 +393,8 @@ public class RequestBuilderWithBodyTests
|
|||||||
|
|
||||||
var bodyData = new BodyData
|
var bodyData = new BodyData
|
||||||
{
|
{
|
||||||
BodyAsString = JsonConvert.SerializeObject(body),
|
BodyAsJson = body,
|
||||||
DetectedBodyType = BodyType.String
|
DetectedBodyType = BodyType.Json
|
||||||
};
|
|
||||||
|
|
||||||
// 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
|
// Act
|
||||||
|
|||||||
Reference in New Issue
Block a user