Files
WireMock.Net/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithHandlebarsRegexTests.cs
Stef Heyenrath 3df4161329 Add HandleBars Random functionality (#230 #219)
Add HandleBars Random functionality (#219)
2018-11-09 10:06:09 +00:00

145 lines
5.4 KiB
C#

using System;
using System.Threading.Tasks;
using NFluent;
using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.ResponseBuilders
{
public class ResponseWithHandlebarsRegexTests
{
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);
// 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);
// 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);
// 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);
// 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);
// 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);
// 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)).Throws<NotSupportedException>();
}
}
}