mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 07:24:34 +01:00
* Version 2.x * Setup .NET 9 * 12 * cleanup some #if for NETSTANDARD1_3 * cleanup + fix tests for net8 * openapi * NO ConfigureAwait(false) + cleanup * . * #endif * HashSet * WireMock.Net.NUnit * HttpContext * Add WebSockets (#1423) * Add WebSockets * Add tests * fix * more tests * Add tests * ... * remove IOwin * - * tests * fluent * ok * match * . * byte[] * x * func * func * byte * trans * ... * frameworks......... * jmes * xxx * sc * using var httpClient = new HttpClient(); * usings * maxRetries * up * xunit v3 * ct * --- * ct * ct2 * T Unit * WireMock.Net.TUnitTests / 10 * t unit first * --project * no tunit * t2 * --project * --project * ci - --project * publish ./test/wiremock-coverage.xml * windows * . * log * ... * log * goed * BodyType * . * . * --scenario * ... * pact * ct * . * WireMock.Net.RestClient.AwesomeAssertions (#1427) * WireMock.Net.RestClient.AwesomeAssertions * ok * atpath * fix test * sonar fixes * ports * proxy test * FIX? * --- * await Task.Delay(100, _ct); * ? * --project * Aspire: use IDistributedApplicationEventingSubscriber (#1428) * broadcast * ok * more tsts * . * Collection * up * . * 2 * remove nfluent * <VersionPrefix>2.0.0-preview-02</VersionPrefix> * ... * . * nuget icon * . * <PackageReference Include="JmesPath.Net" Version="1.1.0" /> * x * 500 * . * fix some warnings * ws
236 lines
8.1 KiB
C#
236 lines
8.1 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using HandlebarsDotNet;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Moq;
|
|
using Newtonsoft.Json.Linq;
|
|
using WireMock.Handlers;
|
|
using WireMock.Models;
|
|
using WireMock.ResponseBuilders;
|
|
using WireMock.Settings;
|
|
using WireMock.Types;
|
|
using WireMock.Util;
|
|
|
|
namespace WireMock.Net.Tests.ResponseBuilders;
|
|
|
|
public class ResponseWithHandlebarsLinqTests
|
|
{
|
|
private readonly WireMockServerSettings _settings = new();
|
|
|
|
private readonly Mock<IMapping> _mappingMock;
|
|
|
|
public ResponseWithHandlebarsLinqTests()
|
|
{
|
|
_mappingMock = new Mock<IMapping>();
|
|
|
|
var filesystemHandlerMock = new Mock<IFileSystemHandler>(MockBehavior.Strict);
|
|
filesystemHandlerMock.Setup(fs => fs.ReadResponseBodyAsString(It.IsAny<string>())).Returns("abc");
|
|
|
|
_settings.FileSystemHandler = filesystemHandlerMock.Object;
|
|
}
|
|
|
|
[Fact(Skip = "DynamicLinq")]
|
|
public async Task Response_ProvideResponse_Handlebars_Linq1_String0()
|
|
{
|
|
// Assign
|
|
var body = new BodyData();
|
|
|
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234/pathtest"), "POST", "::1", body);
|
|
|
|
var responseBuilder = Response.Create()
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBodyAsJson(new { x = "{{Linq request.Path 'it'}}" })
|
|
.WithTransformer();
|
|
|
|
// Act
|
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
|
|
|
// Assert
|
|
var j = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
|
j["x"].Should().NotBeNull();
|
|
j["x"]?.ToString().Should().Be("/pathtest");
|
|
}
|
|
|
|
[Fact(Skip = "DynamicLinq")]
|
|
public async Task Response_ProvideResponse_Handlebars_Linq1_String1()
|
|
{
|
|
// Assign
|
|
var body = new BodyData
|
|
{
|
|
BodyAsJson = new JObject
|
|
{
|
|
{ "Id", new JValue(9) },
|
|
{ "Name", new JValue("Test") }
|
|
},
|
|
DetectedBodyType = BodyType.Json
|
|
};
|
|
|
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", "::1", body);
|
|
|
|
var responseBuilder = Response.Create()
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBodyAsJson(new { x = "{{Linq request.bodyAsJson 'it.Name + \"_123\"' }}" })
|
|
.WithTransformer();
|
|
|
|
// Act
|
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
|
|
|
// Assert
|
|
var j = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
|
j["x"].Should().NotBeNull().And.Subject.ToString().Should().Be("Test_123");
|
|
}
|
|
|
|
[Fact(Skip = "DynamicLinq")]
|
|
public async Task Response_ProvideResponse_Handlebars_Linq1_String2()
|
|
{
|
|
// Assign
|
|
var body = new BodyData
|
|
{
|
|
BodyAsJson = new JObject
|
|
{
|
|
{ "Id", new JValue(9) },
|
|
{ "Name", new JValue("Test") }
|
|
},
|
|
DetectedBodyType = BodyType.Json
|
|
};
|
|
|
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", "::1", body);
|
|
|
|
var responseBuilder = Response.Create()
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBodyAsJson(new { x = "{{Linq request.bodyAsJson 'new(it.Name + \"_123\" as N, it.Id as I)' }}" })
|
|
.WithTransformer();
|
|
|
|
// Act
|
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
|
|
|
// Assert
|
|
var j = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
|
j["x"]?.ToString().Should().Be("{ N = Test_123, I = 9 }");
|
|
}
|
|
|
|
[Fact(Skip = "DynamicLinq")]
|
|
public async Task Response_ProvideResponse_Handlebars_Linq2_Object()
|
|
{
|
|
// Assign
|
|
var body = new BodyData
|
|
{
|
|
BodyAsJson = new JObject
|
|
{
|
|
{ "Id", new JValue(9) },
|
|
{ "Name", new JValue("Test") }
|
|
},
|
|
DetectedBodyType = BodyType.Json
|
|
};
|
|
|
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", "::1", body);
|
|
|
|
var responseBuilder = Response.Create()
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBodyAsJson(new { x = "{{#Linq request.bodyAsJson 'new(it.Name + \"_123\" as N, it.Id as I)' }}{{this}}{{/Linq}}" })
|
|
.WithTransformer();
|
|
|
|
// Act
|
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
|
|
|
// Assert
|
|
var j = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
|
j["x"]?.ToString().Should().Be("{ N = Test_123 }");
|
|
}
|
|
|
|
[Fact(Skip = "DynamicLinq")]
|
|
public void Response_ProvideResponse_Handlebars_Linq_Throws_ArgumentException()
|
|
{
|
|
// Assign
|
|
var body = new BodyData
|
|
{
|
|
BodyAsJson = new { x = "x" },
|
|
DetectedBodyType = BodyType.Json
|
|
};
|
|
|
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", "::1", body);
|
|
|
|
var responseBuilder = Response.Create()
|
|
.WithBodyAsJson(new { x = "{{Linq request.bodyAsJson 1}}" })
|
|
.WithTransformer();
|
|
|
|
// Act
|
|
Func<Task> act = () => responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
|
|
|
// Assert
|
|
act.Should().ThrowAsync<HandlebarsException>();
|
|
}
|
|
|
|
[Fact]
|
|
public void Response_ProvideResponse_Handlebars_Linq1_Throws_HandlebarsException()
|
|
{
|
|
// Assign
|
|
var body = new BodyData();
|
|
|
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", "::1", body);
|
|
|
|
var responseBuilder = Response.Create()
|
|
.WithBodyAsJson(new { x = "{{Linq request.bodyAsJson}} ''" })
|
|
.WithTransformer();
|
|
|
|
// Act
|
|
Func<Task> act = () => responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
|
|
|
// Assert
|
|
act.Should().ThrowAsync<HandlebarsException>();
|
|
}
|
|
|
|
[Fact]
|
|
public void Response_ProvideResponse_Handlebars_Linq1_ParseError_Throws_ExceptionMessage()
|
|
{
|
|
// Assign
|
|
var body = new BodyData
|
|
{
|
|
BodyAsJson = new JObject
|
|
{
|
|
{ "Id", new JValue(9) },
|
|
{ "Name", new JValue("Test") }
|
|
},
|
|
DetectedBodyType = BodyType.Json
|
|
};
|
|
|
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", "::1", body);
|
|
|
|
var responseBuilder = Response.Create()
|
|
.WithBodyAsJson(new { x = "{{Linq request.bodyAsJson '---' }}" })
|
|
.WithTransformer();
|
|
|
|
// Act
|
|
Func<Task> a = async () => await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
|
|
|
// Assert
|
|
a.Should().ThrowAsync<HandlebarsException>();
|
|
}
|
|
|
|
[Fact]
|
|
public void Response_ProvideResponse_Handlebars_Linq2_ParseError_Throws_ExceptionMessage()
|
|
{
|
|
// Assign
|
|
var body = new BodyData
|
|
{
|
|
BodyAsJson = new JObject
|
|
{
|
|
{ "Id", new JValue(9) },
|
|
{ "Name", new JValue("Test") }
|
|
},
|
|
DetectedBodyType = BodyType.Json
|
|
};
|
|
|
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", "::1", body);
|
|
|
|
var responseBuilder = Response.Create()
|
|
.WithBodyAsJson(new { x = "{{#Linq request.bodyAsJson '---' }}{{this}}{{/Linq}}" })
|
|
.WithTransformer();
|
|
|
|
// Act
|
|
Func<Task> act = async () => await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
|
|
|
// Assert
|
|
act.Should().ThrowAsync<HandlebarsException>();
|
|
}
|
|
} |