mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-26 11:21:51 +01:00
Increase code coverage (#107)
This commit is contained in:
61
test/WireMock.Net.Tests/Matchers/ExactMatcherTests.cs
Normal file
61
test/WireMock.Net.Tests/Matchers/ExactMatcherTests.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers
|
||||
{
|
||||
public class ExactMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void ExactMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher("X");
|
||||
|
||||
// Act
|
||||
string name = matcher.GetName();
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("ExactMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExactMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher("X");
|
||||
|
||||
// Act
|
||||
string[] patterns = matcher.GetPatterns();
|
||||
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("X");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExactMatcher_IsMatch_MultiplePatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher("x", "y");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("x");
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(0.5d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyExactMatcher_false()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher("cat");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("caR");
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsStrictlyLessThan(1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
74
test/WireMock.Net.Tests/Matchers/RegexMatcherTests.cs
Normal file
74
test/WireMock.Net.Tests/Matchers/RegexMatcherTests.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers
|
||||
{
|
||||
public class RegexMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void RegexMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new RegexMatcher("");
|
||||
|
||||
// Act
|
||||
string name = matcher.GetName();
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("RegexMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegexMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new RegexMatcher("X");
|
||||
|
||||
// Act
|
||||
string[] patterns = matcher.GetPatterns();
|
||||
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("X");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegexMatcher_IsMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new RegexMatcher("H.*o");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("Hello world!");
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegexMatcher_IsMatch_NullInput()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new RegexMatcher("H.*o");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch(null);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(0.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegexMatcher_IsMatch_IgnoreCase()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new RegexMatcher("H.*o", true);
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("hello world!");
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(1.0d);
|
||||
}
|
||||
}
|
||||
}
|
||||
61
test/WireMock.Net.Tests/Matchers/SimMetricsMatcherTests.cs
Normal file
61
test/WireMock.Net.Tests/Matchers/SimMetricsMatcherTests.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers
|
||||
{
|
||||
public class SimMetricsMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void SimMetricsMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SimMetricsMatcher("X");
|
||||
|
||||
// Act
|
||||
string name = matcher.GetName();
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("SimMetricsMatcher.Levenstein");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimMetricsMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SimMetricsMatcher("X");
|
||||
|
||||
// Act
|
||||
string[] patterns = matcher.GetPatterns();
|
||||
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("X");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimMetricsMatcher_IsMatch_1()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SimMetricsMatcher("The cat walks in the street.");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("The car drives in the street.");
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsStrictlyLessThan(1.0).And.IsStrictlyGreaterThan(0.5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimMetricsMatcher_IsMatch_2()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new SimMetricsMatcher("The cat walks in the street.");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("Hello");
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsStrictlyLessThan(0.1).And.IsStrictlyGreaterThan(0.05);
|
||||
}
|
||||
}
|
||||
}
|
||||
87
test/WireMock.Net.Tests/Matchers/WildcardMatcherTest.cs
Normal file
87
test/WireMock.Net.Tests/Matchers/WildcardMatcherTest.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers
|
||||
{
|
||||
public class WildcardMatcherTest
|
||||
{
|
||||
[Fact]
|
||||
public void WildcardMatcher_IsMatch_Positive()
|
||||
{
|
||||
var tests = new[]
|
||||
{
|
||||
new {p = "*", i = ""},
|
||||
new {p = "?", i = " "},
|
||||
new {p = "*", i = "a"},
|
||||
new {p = "*", i = "ab"},
|
||||
new {p = "?", i = "a"},
|
||||
new {p = "*?", i = "abc"},
|
||||
new {p = "?*", i = "abc"},
|
||||
new {p = "abc", i = "abc"},
|
||||
new {p = "abc*", i = "abc"},
|
||||
new {p = "abc*", i = "abcd"},
|
||||
new {p = "*abc*", i = "abc"},
|
||||
new {p = "*a*bc*", i = "abc"},
|
||||
new {p = "*a*b?", i = "aXXXbc"}
|
||||
};
|
||||
|
||||
foreach (var test in tests)
|
||||
{
|
||||
var matcher = new WildcardMatcher(test.p);
|
||||
Check.That(matcher.IsMatch(test.i)).IsEqualTo(1.0d);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WildcardMatcher_IsMatch_Negative()
|
||||
{
|
||||
var tests = new[]
|
||||
{
|
||||
new {p = "*a", i = ""},
|
||||
new {p = "a*", i = ""},
|
||||
new {p = "?", i = ""},
|
||||
new {p = "*b*", i = "a"},
|
||||
new {p = "b*a", i = "ab"},
|
||||
new {p = "??", i = "a"},
|
||||
new {p = "*?", i = ""},
|
||||
new {p = "??*", i = "a"},
|
||||
new {p = "*abc", i = "abX"},
|
||||
new {p = "*abc*", i = "Xbc"},
|
||||
new {p = "*a*bc*", i = "ac"}
|
||||
};
|
||||
|
||||
foreach (var test in tests)
|
||||
{
|
||||
var matcher = new WildcardMatcher(test.p);
|
||||
Check.That(matcher.IsMatch(test.i)).IsEqualTo(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WildcardMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new WildcardMatcher("x");
|
||||
|
||||
// Act
|
||||
string name = matcher.GetName();
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("WildcardMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WildcardMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new WildcardMatcher("x");
|
||||
|
||||
// Act
|
||||
string[] patterns = matcher.GetPatterns();
|
||||
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("x");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Moq;
|
||||
using NFluent;
|
||||
using WireMock.Matchers.Request;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestMatchers
|
||||
{
|
||||
public class RequestMessageCompositeMatcherTests
|
||||
{
|
||||
private class Helper : RequestMessageCompositeMatcher
|
||||
{
|
||||
public Helper(IEnumerable<IRequestMatcher> requestMatchers, CompositeMatcherType type = CompositeMatcherType.And) : base(requestMatchers, type)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageCompositeMatcher_GetMatchingScore_EmptyArray()
|
||||
{
|
||||
// Assign
|
||||
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1");
|
||||
var matcher = new Helper(Enumerable.Empty<IRequestMatcher>());
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(0.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageCompositeMatcher_GetMatchingScore_CompositeMatcherType_And()
|
||||
{
|
||||
// Assign
|
||||
var requestMatcher1Mock = new Mock<IRequestMatcher>();
|
||||
requestMatcher1Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(1.0d);
|
||||
var requestMatcher2Mock = new Mock<IRequestMatcher>();
|
||||
requestMatcher2Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(0.8d);
|
||||
|
||||
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1");
|
||||
var matcher = new Helper(new[] { requestMatcher1Mock.Object, requestMatcher2Mock.Object });
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(0.9d);
|
||||
|
||||
// Verify
|
||||
requestMatcher1Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
||||
requestMatcher2Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageCompositeMatcher_GetMatchingScore_CompositeMatcherType_Or()
|
||||
{
|
||||
// Assign
|
||||
var requestMatcher1Mock = new Mock<IRequestMatcher>();
|
||||
requestMatcher1Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(1.0d);
|
||||
var requestMatcher2Mock = new Mock<IRequestMatcher>();
|
||||
requestMatcher2Mock.Setup(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>())).Returns(0.8d);
|
||||
|
||||
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1");
|
||||
var matcher = new Helper(new[] { requestMatcher1Mock.Object, requestMatcher2Mock.Object }, CompositeMatcherType.Or);
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
|
||||
// Verify
|
||||
requestMatcher1Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
||||
requestMatcher2Mock.Verify(rm => rm.GetMatchingScore(It.IsAny<RequestMessage>(), It.IsAny<RequestMatchResult>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestMatchers
|
||||
{
|
||||
public class RequestMessageCookieMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void RequestMessageCookieMatcher_GetMatchingScore_IStringMatcher_Match()
|
||||
{
|
||||
// Assign
|
||||
var cookies = new Dictionary<string, string> { { "cook", "x" } };
|
||||
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, null, cookies);
|
||||
var matcher = new RequestMessageCookieMatcher("cook", new ExactMatcher("x"));
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageCookieMatcher_GetMatchingScore_Func_Match()
|
||||
{
|
||||
// Assign
|
||||
var cookies = new Dictionary<string, string> { { "cook", "x" } };
|
||||
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, null, cookies);
|
||||
var matcher = new RequestMessageCookieMatcher(x => x.ContainsKey("cook"));
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestMatchers
|
||||
{
|
||||
public class RequestMessageHeaderMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void RequestMessageHeaderMatcher_GetMatchingScore_IStringMatcher_Match()
|
||||
{
|
||||
// Assign
|
||||
var headers = new Dictionary<string, string[]> { { "h", new [] { "x" } } };
|
||||
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, headers);
|
||||
var matcher = new RequestMessageHeaderMatcher("h", new ExactMatcher("x"));
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageHeaderMatcher_GetMatchingScore_Func_Match()
|
||||
{
|
||||
// Assign
|
||||
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
|
||||
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, headers);
|
||||
var matcher = new RequestMessageHeaderMatcher(x => x.ContainsKey("h"));
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,70 +85,6 @@ namespace WireMock.Net.Tests
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyExactMatcher_multiplePatterns()
|
||||
{
|
||||
// given
|
||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat", "dog"));
|
||||
|
||||
// when
|
||||
string bodyAsString = "cat";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyExactMatcher_false()
|
||||
{
|
||||
// given
|
||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat"));
|
||||
|
||||
// when
|
||||
string bodyAsString = "caR";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodySimMetricsMatcher1()
|
||||
{
|
||||
// given
|
||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("The cat walks in the street."));
|
||||
|
||||
// when
|
||||
string bodyAsString = "The car drives in the street.";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(1.0).And.IsStrictlyGreaterThan(0.5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodySimMetricsMatcher2()
|
||||
{
|
||||
// given
|
||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("The cat walks in the street."));
|
||||
|
||||
// when
|
||||
string bodyAsString = "Hello";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(0.1).And.IsStrictlyGreaterThan(0.05);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyWildcardMatcher()
|
||||
{
|
||||
@@ -165,22 +101,6 @@ namespace WireMock.Net.Tests
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyRegexMatcher()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().UsingAnyVerb().WithBody(new RegexMatcher("H.*o"));
|
||||
|
||||
// when
|
||||
string bodyAsString = "Hello world!";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyXPathMatcher_true()
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using WireMock.ResponseBuilders;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||
{
|
||||
public class ResponseWithBodyHandlebarsTests
|
||||
{
|
||||
@@ -3,9 +3,10 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||
{
|
||||
public class ResponseWithBodyTests
|
||||
{
|
||||
@@ -86,5 +87,59 @@ namespace WireMock.Net.Tests
|
||||
Check.That(responseMessage.BodyAsJson).Equals(x);
|
||||
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBody_String_SameAsSource_Encoding()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", ClientIp);
|
||||
|
||||
var response = Response.Create().WithBody("r", BodyDestinationFormat.SameAsSource, Encoding.ASCII);
|
||||
|
||||
// Act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(responseMessage.BodyAsBytes).IsNull();
|
||||
Check.That(responseMessage.BodyAsJson).IsNull();
|
||||
Check.That(responseMessage.Body).Equals("r");
|
||||
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBody_String_Bytes_Encoding()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", ClientIp);
|
||||
|
||||
var response = Response.Create().WithBody("r", BodyDestinationFormat.Bytes, Encoding.ASCII);
|
||||
|
||||
// Act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(responseMessage.Body).IsNull();
|
||||
Check.That(responseMessage.BodyAsJson).IsNull();
|
||||
Check.That(responseMessage.BodyAsBytes).IsNotNull();
|
||||
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBody_String_Json_Encoding()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", ClientIp);
|
||||
|
||||
var response = Response.Create().WithBody("{ \"value\": 42 }", BodyDestinationFormat.Json, Encoding.ASCII);
|
||||
|
||||
// Act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(responseMessage.Body).IsNull();
|
||||
Check.That(responseMessage.BodyAsBytes).IsNull();
|
||||
Check.That(((dynamic) responseMessage.BodyAsJson).value).Equals(42);
|
||||
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||
{
|
||||
public class ResponseWithHeadersTests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithHeaders_SingleValue()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", ClientIp);
|
||||
var headers = new Dictionary<string, string> { { "h", "x" } };
|
||||
var response = Response.Create().WithHeaders(headers);
|
||||
|
||||
// Act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(responseMessage.Headers["h"]).ContainsExactly("x");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithHeaders_MultipleValues()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", ClientIp);
|
||||
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
|
||||
var response = Response.Create().WithHeaders(headers);
|
||||
|
||||
// Act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(responseMessage.Headers["h"]).ContainsExactly("x");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithHeaders_WiremockList()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", ClientIp);
|
||||
var headers = new Dictionary<string, WireMockList<string>> { { "h", new WireMockList<string>("x") } };
|
||||
var response = Response.Create().WithHeaders(headers);
|
||||
|
||||
// Act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(responseMessage.Headers["h"]).ContainsExactly("x");
|
||||
}
|
||||
}
|
||||
}
|
||||
156
test/WireMock.Net.Tests/Serialization/MatcherModelMapperTests.cs
Normal file
156
test/WireMock.Net.Tests/Serialization/MatcherModelMapperTests.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using NFluent;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Serialization;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization
|
||||
{
|
||||
public class MatcherModelMapperTests
|
||||
{
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_Null()
|
||||
{
|
||||
// Act
|
||||
IMatcher matcher = MatcherModelMapper.Map(null);
|
||||
|
||||
// Assert
|
||||
Check.That(matcher).IsNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_ExactMatcher_Pattern()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "ExactMatcher",
|
||||
Patterns = new[] { "x" }
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (ExactMatcher)MatcherModelMapper.Map(model);
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_ExactMatcher_Patterns()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "ExactMatcher",
|
||||
Patterns = new[] { "x", "y" }
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (ExactMatcher)MatcherModelMapper.Map(model);
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_RegexMatcher()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "RegexMatcher",
|
||||
Patterns = new[] { "x", "y" },
|
||||
IgnoreCase = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (RegexMatcher)MatcherModelMapper.Map(model);
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_WildcardMatcher()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "WildcardMatcher",
|
||||
Patterns = new[] { "x", "y" },
|
||||
IgnoreCase = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (WildcardMatcher)MatcherModelMapper.Map(model);
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_SimMetricsMatcher()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "SimMetricsMatcher",
|
||||
Pattern = "x"
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (SimMetricsMatcher)MatcherModelMapper.Map(model);
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_SimMetricsMatcher_BlockDistance()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "SimMetricsMatcher.BlockDistance",
|
||||
Pattern = "x"
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (SimMetricsMatcher)MatcherModelMapper.Map(model);
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws1()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "error",
|
||||
Pattern = "x"
|
||||
};
|
||||
|
||||
// Act
|
||||
Check.ThatCode(() => MatcherModelMapper.Map(model)).Throws<NotSupportedException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws2()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "SimMetricsMatcher.error",
|
||||
Pattern = "x"
|
||||
};
|
||||
|
||||
// Act
|
||||
Check.ThatCode(() => MatcherModelMapper.Map(model)).Throws<NotSupportedException>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
using NFluent;
|
||||
using Xunit;
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
//[TestFixture]
|
||||
public class WildcardMatcherTest
|
||||
{
|
||||
[Fact]
|
||||
public void WildcardMatcher_patterns_positive()
|
||||
{
|
||||
var tests = new[]
|
||||
{
|
||||
new { p = "*", i = "" },
|
||||
new { p = "?", i = " " },
|
||||
new { p = "*", i = "a" },
|
||||
new { p = "*", i = "ab" },
|
||||
new { p = "?", i = "a" },
|
||||
new { p = "*?", i = "abc" },
|
||||
new { p = "?*", i = "abc" },
|
||||
new { p = "abc", i = "abc" },
|
||||
new { p = "abc*", i = "abc" },
|
||||
new { p = "abc*", i = "abcd" },
|
||||
new { p = "*abc*", i = "abc" },
|
||||
new { p = "*a*bc*", i = "abc" },
|
||||
new { p = "*a*b?", i = "aXXXbc" }
|
||||
};
|
||||
|
||||
foreach (var test in tests)
|
||||
{
|
||||
var matcher = new WildcardMatcher(test.p);
|
||||
Check.That(matcher.IsMatch(test.i)).Equals(1.0);
|
||||
//Assert.AreEqual(1.0, matcher.IsMatch(test.i), "p = " + test.p + ", i = " + test.i);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WildcardMatcher_patterns_negative()
|
||||
{
|
||||
var tests = new[]
|
||||
{
|
||||
new { p = "*a", i = ""},
|
||||
new { p = "a*", i = ""},
|
||||
new { p = "?", i = ""},
|
||||
new { p = "*b*", i = "a"},
|
||||
new { p = "b*a", i = "ab"},
|
||||
new { p = "??", i = "a"},
|
||||
new { p = "*?", i = ""},
|
||||
new { p = "??*", i = "a"},
|
||||
new { p = "*abc", i = "abX"},
|
||||
new { p = "*abc*", i = "Xbc"},
|
||||
new { p = "*a*bc*", i = "ac"}
|
||||
};
|
||||
|
||||
foreach (var test in tests)
|
||||
{
|
||||
var matcher = new WildcardMatcher(test.p);
|
||||
//Assert.AreEqual(0.0, matcher.IsMatch(test.i), "p = " + test.p + ", i = " + test.i);
|
||||
Check.That(matcher.IsMatch(test.i)).Equals(0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user