Files
WireMock.Net/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithHandlebarsRegexTests.cs
Stef Heyenrath d971144426 WireMock.Net version 1.1.x (#363)
* refactor

* rename api

* -preview-01

* logger

* move

* RandomDataGenerator.Net

* .

* ISettings

* renames...

* refactor CommandlineParser logic

* remove standalone

* Remove Interfaces

* Update tests

* WireMock.Net.StandAlone

* .

* fix

* .

* _settings

* Admin

* WireMock.Net.Abstractions

* fix build

* rename WireMockServer

* fix compile errors
2019-12-27 16:01:13 +01:00

148 lines
5.6 KiB
C#

using System;
using System.Threading.Tasks;
using NFluent;
using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.ResponseBuilders
{
public class ResponseWithHandlebarsRegexTests
{
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
private const string ClientIp = "::1";
[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 response = Response.Create()
.WithBody("{{Regex.Match request.body \"^(?<x>\\w+)$\"}}")
.WithTransformer();
// Act
var responseMessage = await response.ProvideResponseAsync(request, _settings);
// assert
Check.That(responseMessage.BodyData.BodyAsString).Equals("abc");
}
[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 response = Response.Create()
.WithBody("{{Regex.Match request.body \"^?0$\"}}")
.WithTransformer();
// Act
var responseMessage = await response.ProvideResponseAsync(request, _settings);
// assert
Check.That(responseMessage.BodyData.BodyAsString).Equals("");
}
[Fact]
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch_NoMatch_WithDefaultValue()
{
// Assign
var body = new BodyData { BodyAsString = "abc", DetectedBodyType = BodyType.String };
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
var response = Response.Create()
.WithBody("{{Regex.Match request.body \"^?0$\" \"d\"}}")
.WithTransformer();
// Act
var responseMessage = await response.ProvideResponseAsync(request, _settings);
// assert
Check.That(responseMessage.BodyData.BodyAsString).Equals("d");
}
[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 response = Response.Create()
.WithBody("{{#Regex.Match request.body \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{this.port}}-{{this.proto}}{{/Regex.Match}}")
.WithTransformer();
// Act
var responseMessage = await response.ProvideResponseAsync(request, _settings);
// assert
Check.That(responseMessage.BodyData.BodyAsString).Equals("5000-https");
}
[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 response = Response.Create()
.WithBody("{{#Regex.Match request.body \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{this}}{{/Regex.Match}}")
.WithTransformer();
// Act
var responseMessage = await response.ProvideResponseAsync(request, _settings);
// assert
Check.That(responseMessage.BodyData.BodyAsString).Equals("");
}
[Fact]
public async Task Response_ProvideResponseAsync_Handlebars_RegexMatch2_NoMatch_WithDefaultValue()
{
// Assign
var body = new BodyData { BodyAsString = "{{\\test", DetectedBodyType = BodyType.String };
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "POST", ClientIp, body);
var response = Response.Create()
.WithBody("{{#Regex.Match request.body \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\" \"x\"}}{{this}}{{/Regex.Match}}")
.WithTransformer();
// Act
var responseMessage = await response.ProvideResponseAsync(request, _settings);
// assert
Check.That(responseMessage.BodyData.BodyAsString).Equals("x");
}
[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 response = Response.Create()
.WithBody("{{#Regex.Match request.bodyAsJson \"^(?<proto>\\w+)://[^/]+?(?<port>\\d+)/?\"}}{{/Regex.Match}}")
.WithTransformer();
// Act and Assert
Check.ThatAsyncCode(() => response.ProvideResponseAsync(request, _settings)).Throws<NotSupportedException>();
}
}
}