mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-18 22:49:45 +02:00
Add support to use 'mapping' object in in reponse templating (#798)
* mapping * . * .
This commit is contained in:
@@ -10,113 +10,114 @@ using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilders
|
||||
namespace WireMock.Net.Tests.ResponseBuilders;
|
||||
|
||||
public class ResponseWithHandlebarsRegexTests
|
||||
{
|
||||
public class ResponseWithHandlebarsRegexTests
|
||||
private const string ClientIp = "::1";
|
||||
private readonly WireMockServerSettings _settings = new();
|
||||
|
||||
private readonly Mock<IMapping> _mappingMock;
|
||||
|
||||
public ResponseWithHandlebarsRegexTests()
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
_mappingMock = new Mock<IMapping>();
|
||||
|
||||
private readonly Mock<IFileSystemHandler> _filesystemHandlerMock;
|
||||
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
|
||||
var filesystemHandlerMock = new Mock<IFileSystemHandler>(MockBehavior.Strict);
|
||||
filesystemHandlerMock.Setup(fs => fs.ReadResponseBodyAsString(It.IsAny<string>())).Returns("abc");
|
||||
|
||||
public ResponseWithHandlebarsRegexTests()
|
||||
{
|
||||
_filesystemHandlerMock = new Mock<IFileSystemHandler>(MockBehavior.Strict);
|
||||
_filesystemHandlerMock.Setup(fs => fs.ReadResponseBodyAsString(It.IsAny<string>())).Returns("abc");
|
||||
_settings.FileSystemHandler = filesystemHandlerMock.Object;
|
||||
}
|
||||
|
||||
_settings.FileSystemHandler = _filesystemHandlerMock.Object;
|
||||
}
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "abc", DetectedBodyType = BodyType.String };
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "abc", DetectedBodyType = BodyType.String };
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{Regex.Match request.body \"^(\\w+)$\"}}")
|
||||
.WithTransformer();
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{Regex.Match request.body \"^(\\w+)$\"}}")
|
||||
.WithTransformer();
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(request, _settings).ConfigureAwait(false);
|
||||
// assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("abc");
|
||||
}
|
||||
|
||||
// assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("abc");
|
||||
}
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch_NoMatch()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "abc", DetectedBodyType = BodyType.String };
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch_NoMatch()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "abc", DetectedBodyType = BodyType.String };
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{Regex.Match request.body \"^?0$\"}}")
|
||||
.WithTransformer();
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{Regex.Match request.body \"^?0$\"}}")
|
||||
.WithTransformer();
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(request, _settings).ConfigureAwait(false);
|
||||
// assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("");
|
||||
}
|
||||
|
||||
// assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("");
|
||||
}
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch2()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "https://localhost:5000/", DetectedBodyType = BodyType.String };
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch2()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "https://localhost:5000/", DetectedBodyType = BodyType.String };
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{#Regex.Match request.body \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{this.port}}-{{this.proto}}{{/Regex.Match}}")
|
||||
.WithTransformer();
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{#Regex.Match request.body \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{this.port}}-{{this.proto}}{{/Regex.Match}}")
|
||||
.WithTransformer();
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(request, _settings).ConfigureAwait(false);
|
||||
// assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("5000-https");
|
||||
}
|
||||
|
||||
// assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("5000-https");
|
||||
}
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch2_NoMatch()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "{{\\test", DetectedBodyType = BodyType.String };
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch2_NoMatch()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "{{\\test", DetectedBodyType = BodyType.String };
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{#Regex.Match request.body \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{this}}{{/Regex.Match}}")
|
||||
.WithTransformer();
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{#Regex.Match request.body \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{this}}{{/Regex.Match}}")
|
||||
.WithTransformer();
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(request, _settings).ConfigureAwait(false);
|
||||
// assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("");
|
||||
}
|
||||
|
||||
// assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("");
|
||||
}
|
||||
[Fact]
|
||||
public void Response_ProvideResponseAsync_Handlebars_RegexMatch2_Throws()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "{{\\test", DetectedBodyType = BodyType.String };
|
||||
|
||||
[Fact]
|
||||
public void Response_ProvideResponseAsync_Handlebars_RegexMatch2_Throws()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData { BodyAsString = "{{\\test", DetectedBodyType = BodyType.String };
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{#Regex.Match request.bodyAsJson \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{/Regex.Match}}")
|
||||
.WithTransformer();
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{#Regex.Match request.bodyAsJson \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{/Regex.Match}}")
|
||||
.WithTransformer();
|
||||
|
||||
// Act and Assert
|
||||
Check.ThatAsyncCode(() => responseBuilder.ProvideResponseAsync(request, _settings)).Throws<ArgumentNullException>();
|
||||
}
|
||||
// Act and Assert
|
||||
Check.ThatAsyncCode(() => responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings)).Throws<ArgumentNullException>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user