mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 16:44:31 +01:00
Add MultiPart/MimePart Request Matcher (#981)
* wip * . * mm * x * . * . * . * tests * . * more tests * trans * x * win * fix * . * tests
This commit is contained in:
99
test/WireMock.Net.Tests/Matchers/MimePartMatcherTests.cs
Normal file
99
test/WireMock.Net.Tests/Matchers/MimePartMatcherTests.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
#if MIMEKIT
|
||||
using System;
|
||||
using System.Linq;
|
||||
using FluentAssertions;
|
||||
using MimeKit;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class MimePartMatcherTests
|
||||
{
|
||||
private const string TestMultiPart = @"From:
|
||||
Date: Sun, 23 Jul 2023 16:13:13 +0200
|
||||
Subject:
|
||||
Message-Id: <HZ3K1HEAJKU4.IO57XCVO4BWV@desktop-6dd5qi2>
|
||||
MIME-Version: 1.0
|
||||
Content-Type: multipart/mixed; boundary=""=-5XgmpXt0XOfzdtcgNJc2ZQ==""
|
||||
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
This is some plain text
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: text/json; charset=utf-8
|
||||
|
||||
{
|
||||
""Key"": ""Value""
|
||||
}
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: image/png; name=image.png
|
||||
Content-Disposition: attachment; filename=image.png
|
||||
Content-Transfer-Encoding: base64
|
||||
|
||||
iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjl
|
||||
AAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC
|
||||
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==--
|
||||
";
|
||||
|
||||
[Fact]
|
||||
public void MimePartMatcher_IsMatch_Part_TextPlain()
|
||||
{
|
||||
// Arrange
|
||||
var message = MimeMessage.Load(StreamUtils.CreateStream(TestMultiPart));
|
||||
var part = (MimePart)message.BodyParts.ToArray()[0];
|
||||
|
||||
// Act
|
||||
var contentTypeMatcher = new ContentTypeMatcher("text/plain");
|
||||
var contentMatcher = new ExactMatcher("This is some plain text");
|
||||
|
||||
var matcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, contentTypeMatcher, null, null, contentMatcher);
|
||||
var result = matcher.IsMatch(part);
|
||||
|
||||
// Assert
|
||||
matcher.Name.Should().Be("MimePartMatcher");
|
||||
result.Should().Be(MatchScores.Perfect);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MimePartMatcher_IsMatch_Part_TextJson()
|
||||
{
|
||||
// Arrange
|
||||
var message = MimeMessage.Load(StreamUtils.CreateStream(TestMultiPart));
|
||||
var part = (MimePart)message.BodyParts.ToArray()[1];
|
||||
|
||||
// Act
|
||||
var contentTypeMatcher = new ContentTypeMatcher("text/json");
|
||||
var contentMatcher = new JsonMatcher(new { Key = "Value" }, true);
|
||||
|
||||
var matcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, contentTypeMatcher, null, null, contentMatcher);
|
||||
var result = matcher.IsMatch(part);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(MatchScores.Perfect);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MimePartMatcher_IsMatch_Part_ImagePng()
|
||||
{
|
||||
// Arrange
|
||||
var message = MimeMessage.Load(StreamUtils.CreateStream(TestMultiPart));
|
||||
var part = (MimePart)message.BodyParts.ToArray()[2];
|
||||
|
||||
// Act
|
||||
var contentTypeMatcher = new ContentTypeMatcher("image/png");
|
||||
var contentDispositionMatcher = new ExactMatcher("attachment; filename=\"image.png\"");
|
||||
var contentTransferEncodingMatcher = new ExactMatcher("base64");
|
||||
var contentMatcher = new ExactObjectMatcher(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC"));
|
||||
|
||||
var matcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, contentTypeMatcher, contentDispositionMatcher, contentTransferEncodingMatcher, contentMatcher);
|
||||
var result = matcher.IsMatch(part);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(MatchScores.Perfect);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -8,9 +8,9 @@ using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests;
|
||||
namespace WireMock.Net.Tests.RequestBuilders;
|
||||
|
||||
public class RequestTests
|
||||
public class RequestBuilderTests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
using System;
|
||||
using FluentAssertions;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using JsonConverter.Abstractions;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestBuilders;
|
||||
|
||||
public class RequestBuilderWithBodyTests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public void RequestBuilder_WithBody_IMatcher()
|
||||
public void Request_WithBody_IMatcher()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new WildcardMatcher("x");
|
||||
@@ -26,7 +37,7 @@ public class RequestBuilderWithBodyTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestBuilder_WithBody_IMatchers()
|
||||
public void Request_WithBody_IMatchers()
|
||||
{
|
||||
// Assign
|
||||
var matcher1 = new WildcardMatcher("x");
|
||||
@@ -40,4 +51,409 @@ public class RequestBuilderWithBodyTests
|
||||
matchers.Should().HaveCount(1);
|
||||
((RequestMessageBodyMatcher)matchers[0]).Matchers.Should().Contain(new[] { matcher1, matcher2 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncString()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(b => b != null && b.Contains("b"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "b",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncJson()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(b => b != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsJson = 123,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncFormUrlEncoded()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IDictionary<string, string>? values) => values != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsFormUrlEncoded = new Dictionary<string, string>(),
|
||||
DetectedBodyTypeFromContentType = BodyType.FormUrlEncoded,
|
||||
DetectedBodyType = BodyType.FormUrlEncoded
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncBodyData()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IBodyData? b) => b != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsJson = 123,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncByteArray()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((byte[]? b) => b != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsBytes = new byte[0],
|
||||
DetectedBodyType = BodyType.Bytes
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyExactMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(new ExactMatcher("cat"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "cat",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_BodyDataAsString_Using_WildcardMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("H*o*"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "Hello world!",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_BodyDataAsJson_Using_WildcardMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("*Hello*"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsJson = new { Hi = "Hello world!" },
|
||||
BodyAsString = "{ Hi = \"Hello world!\" }",
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_XPathMatcher_true()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = @"
|
||||
<todo-list>
|
||||
<todo-item id='a1'>abc</todo-item>
|
||||
<todo-item id='a2'>def</todo-item>
|
||||
<todo-item id='a3'>xyz</todo-item>
|
||||
</todo-list>",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_XPathMatcher_false()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = @"
|
||||
<todo-list>
|
||||
<todo-item id='a1'>abc</todo-item>
|
||||
<todo-item id='a2'>def</todo-item>
|
||||
<todo-item id='a3'>xyz</todo-item>
|
||||
</todo-list>",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_JsonPathMatcher_true()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyJson_PathMatcher_false()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_Object_JsonPathMatcher_true()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_Array_JsonPathMatcher_1()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..books[?(@.price < 10)]"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{ \"books\": [ { \"category\": \"test1\", \"price\": 8.95 }, { \"category\": \"test2\", \"price\": 20 } ] }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_Array_JsonPathMatcher_2()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..[?(@.Id == 1)]"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{ \"Id\": 1, \"Name\": \"Test\" }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
double result = spec.GetMatchingScore(request, requestMatchResult);
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsObject_ExactObjectMatcher_true()
|
||||
{
|
||||
// Assign
|
||||
object body = DateTime.MinValue;
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(body);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = DateTime.MinValue,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_UsingObject()
|
||||
{
|
||||
// Assign
|
||||
object body = new
|
||||
{
|
||||
Test = "abc"
|
||||
};
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBodyAsJson(body);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = JsonConvert.SerializeObject(body),
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_WithIJsonConverter_UsingObject()
|
||||
{
|
||||
// Assign
|
||||
var jsonConverterMock = new Mock<IJsonConverter>();
|
||||
jsonConverterMock.Setup(j => j.Serialize(It.IsAny<object>(), It.IsAny<JsonConverterOptions>())).Returns("test");
|
||||
object body = new
|
||||
{
|
||||
Any = "key"
|
||||
};
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBodyAsJson(body, jsonConverterMock.Object);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "test",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new byte[] { 1 }, BodyType.Bytes)]
|
||||
[InlineData(new byte[] { 48, 49, 50 }, BodyType.Bytes)]
|
||||
[InlineData(new byte[] { 48, 49, 50 }, BodyType.String)]
|
||||
public void Request_WithBodyAsBytes_ExactObjectMatcher_true(byte[] bytes, BodyType detectedBodyType)
|
||||
{
|
||||
// Assign
|
||||
byte[] body = bytes;
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(body);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsBytes = bytes,
|
||||
DetectedBodyType = detectedBodyType
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
requestBuilder.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestBuilders;
|
||||
|
||||
public class RequestBuilderWithClientIPTests
|
||||
{
|
||||
[Fact]
|
||||
public void Request_WithClientIP_Match_Ok()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithClientIP("127.0.0.2", "1.1.1.1");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.2");
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithClientIP_Match_Fail()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithClientIP("127.0.0.2");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "192.1.1.1");
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithClientIP_WildcardMatcher()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithClientIP(new WildcardMatcher("127.0.0.2"));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.2");
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithClientIP_Func()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithClientIP(c => c.Contains("."));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.2");
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#if MIMEKIT
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestBuilders;
|
||||
|
||||
public class RequestBuilderWithMultiPartTests
|
||||
{
|
||||
[Fact]
|
||||
public void RequestBuilder_WithMultiPart_MimePartMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, null, null, null, null);
|
||||
|
||||
// Act
|
||||
var requestBuilder = (Request)Request.Create().WithMultiPart(matcher);
|
||||
|
||||
// Assert
|
||||
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||
matchers.Should().HaveCount(1);
|
||||
((RequestMessageMultiPartMatcher)matchers[0]).Matchers.Should().HaveCount(1).And.ContainItemsAssignableTo<MimePartMatcher>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestBuilder_WithMultiPart_MimePartMatchers()
|
||||
{
|
||||
// Arrange
|
||||
var matcher1 = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, null, null, null, null);
|
||||
var matcher2 = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, null, null, null, null);
|
||||
|
||||
// Act
|
||||
var requestBuilder = (Request)Request.Create().WithMultiPart(MatchBehaviour.RejectOnMatch, matcher1, matcher2);
|
||||
|
||||
// Assert
|
||||
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||
matchers.Should().HaveCount(1);
|
||||
|
||||
var x = ((RequestMessageMultiPartMatcher)matchers[0]);
|
||||
x.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
||||
x.Matchers.Should().HaveCount(2).And.ContainItemsAssignableTo<MimePartMatcher>();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,238 @@
|
||||
using System.Collections.Generic;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestBuilders;
|
||||
|
||||
public class RequestBuilderWithPathTests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Spaces()
|
||||
{
|
||||
// Assign
|
||||
var spec = Request.Create().WithPath("/path/a b").UsingAnyMethod();
|
||||
|
||||
// when
|
||||
var body = new BodyData();
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/path/a b"), "GET", ClientIp, body);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_WithHeader_Match()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithHeader("X-toto", "tata");
|
||||
|
||||
// when
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "abc"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPaths()
|
||||
{
|
||||
var requestBuilder = Request.Create().WithPath("/x1", "/x2");
|
||||
|
||||
var request1 = new RequestMessage(new UrlDetails("http://localhost/x1"), "blabla", ClientIp);
|
||||
var request2 = new RequestMessage(new UrlDetails("http://localhost/x2"), "blabla", ClientIp);
|
||||
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request1, requestMatchResult)).IsEqualTo(1.0);
|
||||
Check.That(requestBuilder.GetMatchingScore(request2, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPathFunc()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath(url => url.EndsWith("/foo"));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_RegexMatcher_HasMatch()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo/bar"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPathRegexMatcher_HasNoMatch()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/bar"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_RegexMatcher_WithPatternAsFile_HasMatch()
|
||||
{
|
||||
// Arrange
|
||||
var pattern = new StringPattern
|
||||
{
|
||||
Pattern = "^/foo",
|
||||
PatternAsFile = "c:\\x.txt"
|
||||
};
|
||||
var spec = Request.Create().WithPath(new RegexMatcher(pattern));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo/bar"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Should_specify_requests_matching_given_path_and_method_delete()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingDelete();
|
||||
|
||||
// when
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "whatever"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "Delete", ClientIp, body);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Should_specify_requests_matching_given_path_and_method_get()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingGet();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Should_specify_requests_matching_given_path_and_method_head()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingHead();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "HEAD", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Should_specify_requests_matching_given_path_and_method_post()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingPost();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Should_specify_requests_matching_given_path_and_method_put()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Should_specify_requests_matching_given_path_and_method_patch()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingPatch();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PATCH", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Should_exclude_requests_matching_given_path_but_not_http_method()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "HEAD", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
#if MIMEKIT
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestMatchers;
|
||||
|
||||
public class RequestMessageMultiPartMatcherTests
|
||||
{
|
||||
private const string TestMultiPart = @"--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
This is some plain text
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: text/json; charset=utf-8
|
||||
|
||||
{
|
||||
""Key"": ""Value""
|
||||
}
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: image/png; name=image.png
|
||||
Content-Disposition: attachment; filename=image.png
|
||||
Content-Transfer-Encoding: base64
|
||||
|
||||
iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjl
|
||||
AAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC
|
||||
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==--
|
||||
";
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsMultiPart()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = TestMultiPart,
|
||||
DetectedBodyType = BodyType.MultiPart
|
||||
};
|
||||
|
||||
var textPlainContentTypeMatcher = new ContentTypeMatcher("text/plain");
|
||||
var textPlainContentMatcher = new ExactMatcher("This is some plain text");
|
||||
var textPlainMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textPlainContentTypeMatcher, null, null, textPlainContentMatcher);
|
||||
|
||||
var partTextJsonContentTypeMatcher = new ContentTypeMatcher("text/json");
|
||||
var partTextJsonContentMatcher = new JsonMatcher(new { Key = "Value" }, true);
|
||||
var partTextMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, partTextJsonContentTypeMatcher, null, null, partTextJsonContentMatcher);
|
||||
|
||||
var imagePngContentTypeMatcher = new ContentTypeMatcher("image/png");
|
||||
var imagePngContentDispositionMatcher = new ExactMatcher("attachment; filename=\"image.png\"");
|
||||
var imagePngContentTransferEncodingMatcher = new ExactMatcher("base64");
|
||||
var imagePngContentMatcher = new ExactObjectMatcher(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC"));
|
||||
var imagePngMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, imagePngContentTypeMatcher, imagePngContentDispositionMatcher, imagePngContentTransferEncodingMatcher, imagePngContentMatcher);
|
||||
|
||||
var matchers = new IMatcher[]
|
||||
{
|
||||
textPlainMatcher,
|
||||
partTextMatcher,
|
||||
imagePngMatcher
|
||||
};
|
||||
|
||||
var headers = new Dictionary<string, string[]>
|
||||
{
|
||||
{ "Content-Type", new[] { @"multipart/mixed; boundary=""=-5XgmpXt0XOfzdtcgNJc2ZQ==""" } }
|
||||
};
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body, headers);
|
||||
|
||||
var matcher = new RequestMessageMultiPartMatcher(matchers);
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
var score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
score.Should().Be(MatchScores.Perfect);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,428 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using FluentAssertions;
|
||||
using JsonConverter.Abstractions;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
public class RequestWithBodyTests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncString()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(b => b.Contains("b"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "b",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncJson()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(b => b != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsJson = 123,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncFormUrlEncoded()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IDictionary<string, string>? values) => values != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsFormUrlEncoded = new Dictionary<string, string>(),
|
||||
DetectedBodyTypeFromContentType = BodyType.FormUrlEncoded,
|
||||
DetectedBodyType = BodyType.FormUrlEncoded
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncBodyData()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IBodyData? b) => b != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsJson = 123,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_FuncByteArray()
|
||||
{
|
||||
// Assign
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody((byte[]? b) => b != null);
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsBytes = new byte[0],
|
||||
DetectedBodyType = BodyType.Bytes
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyExactMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(new ExactMatcher("cat"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "cat",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_BodyDataAsString_Using_WildcardMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("H*o*"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "Hello world!",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBody_BodyDataAsJson_Using_WildcardMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("*Hello*"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsJson = new { Hi = "Hello world!" },
|
||||
BodyAsString = "{ Hi = \"Hello world!\" }",
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
spec.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyXPathMatcher_true()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = @"
|
||||
<todo-list>
|
||||
<todo-item id='a1'>abc</todo-item>
|
||||
<todo-item id='a2'>def</todo-item>
|
||||
<todo-item id='a3'>xyz</todo-item>
|
||||
</todo-list>",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyXPathMatcher_false()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = @"
|
||||
<todo-list>
|
||||
<todo-item id='a1'>abc</todo-item>
|
||||
<todo-item id='a2'>def</todo-item>
|
||||
<todo-item id='a3'>xyz</todo-item>
|
||||
</todo-list>",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyJsonPathMatcher_true()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyJsonPathMatcher_false()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// Act
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_Object_JsonPathMatcher_true()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_Array_JsonPathMatcher_1()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..books[?(@.price < 10)]"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{ \"books\": [ { \"category\": \"test1\", \"price\": 8.95 }, { \"category\": \"test2\", \"price\": 20 } ] }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_Array_JsonPathMatcher_2()
|
||||
{
|
||||
// Arrange
|
||||
var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..[?(@.Id == 1)]"));
|
||||
|
||||
// Act
|
||||
string jsonString = "{ \"Id\": 1, \"Name\": \"Test\" }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
BodyAsString = jsonString,
|
||||
Encoding = Encoding.UTF8,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
double result = spec.GetMatchingScore(request, requestMatchResult);
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsObject_ExactObjectMatcher_true()
|
||||
{
|
||||
// Assign
|
||||
object body = DateTime.MinValue;
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(body);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = DateTime.MinValue,
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_UsingObject()
|
||||
{
|
||||
// Assign
|
||||
object body = new
|
||||
{
|
||||
Test = "abc"
|
||||
};
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBodyAsJson(body);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = JsonConvert.SerializeObject(body),
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithBodyAsJson_WithIJsonConverter_UsingObject()
|
||||
{
|
||||
// Assign
|
||||
var jsonConverterMock = new Mock<IJsonConverter>();
|
||||
jsonConverterMock.Setup(j => j.Serialize(It.IsAny<object>(), It.IsAny<JsonConverterOptions>())).Returns("test");
|
||||
object body = new
|
||||
{
|
||||
Any = "key"
|
||||
};
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBodyAsJson(body, jsonConverterMock.Object);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "test",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new byte[] { 1 }, BodyType.Bytes)]
|
||||
[InlineData(new byte[] { 48, 49, 50 }, BodyType.Bytes)]
|
||||
[InlineData(new byte[] { 48, 49, 50 }, BodyType.String)]
|
||||
public void Request_WithBodyAsBytes_ExactObjectMatcher_true(byte[] bytes, BodyType detectedBodyType)
|
||||
{
|
||||
// Assign
|
||||
byte[] body = bytes;
|
||||
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(body);
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsBytes = bytes,
|
||||
DetectedBodyType = detectedBodyType
|
||||
};
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, bodyData);
|
||||
|
||||
// Assert
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
requestBuilder.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
public class RequestWithClientIPTests
|
||||
{
|
||||
[Fact]
|
||||
public void Request_WithClientIP_Match_Ok()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithClientIP("127.0.0.2", "1.1.1.1");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.2");
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithClientIP_Match_Fail()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithClientIP("127.0.0.2");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "192.1.1.1");
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithClientIP_WildcardMatcher()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithClientIP(new WildcardMatcher("127.0.0.2"));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.2");
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithClientIP_Func()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithClientIP(c => c.Contains("."));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.2");
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,239 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
public class RequestWithPathTests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_Spaces()
|
||||
{
|
||||
// Assign
|
||||
var spec = Request.Create().WithPath("/path/a b").UsingAnyMethod();
|
||||
|
||||
// when
|
||||
var body = new BodyData();
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/path/a b"), "GET", ClientIp, body);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath_WithHeader_Match()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithHeader("X-toto", "tata");
|
||||
|
||||
// when
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "abc"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPath()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPaths()
|
||||
{
|
||||
var requestBuilder = Request.Create().WithPath("/x1", "/x2");
|
||||
|
||||
var request1 = new RequestMessage(new UrlDetails("http://localhost/x1"), "blabla", ClientIp);
|
||||
var request2 = new RequestMessage(new UrlDetails("http://localhost/x2"), "blabla", ClientIp);
|
||||
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(requestBuilder.GetMatchingScore(request1, requestMatchResult)).IsEqualTo(1.0);
|
||||
Check.That(requestBuilder.GetMatchingScore(request2, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPathFunc()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath(url => url.EndsWith("/foo"));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPathRegexMatcher_HasMatch()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo/bar"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPathRegexMatcher_HasNoMatch()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/bar"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Request_WithPathRegexMatcher_WithPatternAsFile_HasMatch()
|
||||
{
|
||||
// Arrange
|
||||
var pattern = new StringPattern
|
||||
{
|
||||
Pattern = "^/foo",
|
||||
PatternAsFile = "c:\\x.txt"
|
||||
};
|
||||
var spec = Request.Create().WithPath(new RegexMatcher(pattern));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo/bar"), "blabla", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_specify_requests_matching_given_path_and_method_delete()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingDelete();
|
||||
|
||||
// when
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "whatever"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "Delete", ClientIp, body);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_specify_requests_matching_given_path_and_method_get()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingGet();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_specify_requests_matching_given_path_and_method_head()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingHead();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "HEAD", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_specify_requests_matching_given_path_and_method_post()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingPost();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_specify_requests_matching_given_path_and_method_put()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_specify_requests_matching_given_path_and_method_patch()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingPatch();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PATCH", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_exclude_requests_matching_given_path_but_not_http_method()
|
||||
{
|
||||
// given
|
||||
var spec = Request.Create().WithPath("/foo").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "HEAD", ClientIp);
|
||||
|
||||
// then
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -783,6 +783,59 @@ public class ResponseWithTransformerTests
|
||||
response.Message.BodyData.Encoding.Should().Be(enc);
|
||||
}
|
||||
|
||||
#if MIMEKIT
|
||||
[Theory]
|
||||
[InlineData(TransformerType.Handlebars)]
|
||||
// [InlineData(TransformerType.Scriban)]
|
||||
// [InlineData(TransformerType.ScribanDotLiquid)]
|
||||
public async Task Response_ProvideResponse_Transformer_WithBodyAsMimeMessage(TransformerType transformerType)
|
||||
{
|
||||
// Assign
|
||||
var multiPart = @"--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
This is some plain text
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: text/json; charset=utf-8
|
||||
|
||||
{
|
||||
""Key"": ""Value""
|
||||
}
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
|
||||
Content-Type: image/png; name=image.png
|
||||
Content-Disposition: attachment; filename=image.png
|
||||
Content-Transfer-Encoding: base64
|
||||
|
||||
iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjl
|
||||
AAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC
|
||||
|
||||
--=-5XgmpXt0XOfzdtcgNJc2ZQ==--
|
||||
";
|
||||
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = multiPart,
|
||||
DetectedBodyType = BodyType.MultiPart
|
||||
};
|
||||
|
||||
var headers = new Dictionary<string, string[]>
|
||||
{
|
||||
{ "Content-Type", new[] { @"multipart/mixed; boundary=""=-5XgmpXt0XOfzdtcgNJc2ZQ=="""} }
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo_object"), "POST", ClientIp, bodyData, headers);
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{request.BodyAsMimeMessage.BodyParts.[0].ContentType.MimeType}} {{request.BodyAsMimeMessage.BodyParts.[1].ContentType.MimeType}} {{request.BodyAsMimeMessage.BodyParts.[2].FileName}}")
|
||||
.WithTransformer(transformerType);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsString.Should().Be("text/plain text/json image.png");
|
||||
}
|
||||
#endif
|
||||
|
||||
[Theory]
|
||||
[InlineData("/wiremock-data/1", "one")]
|
||||
[InlineData("/wiremock-data/2", "two")]
|
||||
|
||||
@@ -56,6 +56,40 @@ public class MatcherMapperTests
|
||||
models.Should().HaveCount(2);
|
||||
}
|
||||
|
||||
#if MIMEKIT
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MimePartMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var bytes = Convert.FromBase64String("c3RlZg==");
|
||||
var imagePngContentTypeMatcher = new ContentTypeMatcher("image/png");
|
||||
var imagePngContentDispositionMatcher = new ExactMatcher("attachment; filename=\"image.png\"");
|
||||
var imagePngContentTransferEncodingMatcher = new ExactMatcher("base64");
|
||||
var imagePngContentMatcher = new ExactObjectMatcher(bytes);
|
||||
var imagePngMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, imagePngContentTypeMatcher, imagePngContentDispositionMatcher, imagePngContentTransferEncodingMatcher, imagePngContentMatcher);
|
||||
|
||||
// Act
|
||||
var model = _sut.Map(imagePngMatcher)!;
|
||||
|
||||
// Assert
|
||||
model.Name.Should().Be(nameof(MimePartMatcher));
|
||||
model.MatchOperator.Should().BeNull();
|
||||
model.RejectOnMatch.Should().BeNull();
|
||||
|
||||
model.ContentTypeMatcher!.Name.Should().Be(nameof(ContentTypeMatcher));
|
||||
model.ContentTypeMatcher.Pattern.Should().Be("image/png");
|
||||
|
||||
model.ContentDispositionMatcher!.Name.Should().Be(nameof(ExactMatcher));
|
||||
model.ContentDispositionMatcher.Pattern.Should().Be("attachment; filename=\"image.png\"");
|
||||
|
||||
model.ContentTransferEncodingMatcher!.Name.Should().Be(nameof(ExactMatcher));
|
||||
model.ContentTransferEncodingMatcher.Pattern.Should().Be("base64");
|
||||
|
||||
model.ContentMatcher!.Name.Should().Be(nameof(ExactObjectMatcher));
|
||||
model.ContentMatcher.Pattern.Should().Be(bytes);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_IStringMatcher()
|
||||
{
|
||||
@@ -428,4 +462,64 @@ public class MatcherMapperTests
|
||||
matcher.GetPatterns().Should().Contain("p");
|
||||
matcher.IgnoreCase.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MatcherModel_NotNullOrEmptyMatcher()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "NotNullOrEmptyMatcher",
|
||||
RejectOnMatch = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = _sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
matcher.Should().BeAssignableTo<NotNullOrEmptyMatcher>();
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
||||
}
|
||||
|
||||
#if MIMEKIT
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MatcherModel_MimePartMatcher()
|
||||
{
|
||||
// Assign
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "MimePartMatcher",
|
||||
ContentMatcher = new MatcherModel
|
||||
{
|
||||
Name = "ExactMatcher",
|
||||
Pattern = "x"
|
||||
},
|
||||
ContentDispositionMatcher = new MatcherModel
|
||||
{
|
||||
Name = "WildcardMatcher",
|
||||
Pattern = "y"
|
||||
},
|
||||
ContentTransferEncodingMatcher = new MatcherModel
|
||||
{
|
||||
Name = "RegexMatcher",
|
||||
Pattern = "z"
|
||||
},
|
||||
ContentTypeMatcher = new MatcherModel
|
||||
{
|
||||
Name = "ContentTypeMatcher",
|
||||
Pattern = "text/json"
|
||||
}
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (MimePartMatcher)_sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||
matcher.ContentMatcher.Should().BeAssignableTo<ExactMatcher>().Which.GetPatterns().Should().ContainSingle("x");
|
||||
matcher.ContentDispositionMatcher.Should().BeAssignableTo<WildcardMatcher>().Which.GetPatterns().Should().ContainSingle("y");
|
||||
matcher.ContentTransferEncodingMatcher.Should().BeAssignableTo<RegexMatcher>().Which.GetPatterns().Should().ContainSingle("z");
|
||||
matcher.ContentTypeMatcher.Should().BeAssignableTo<ContentTypeMatcher>().Which.GetPatterns().Should().ContainSingle("text/json");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -25,7 +25,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)' != 'netstandard1.3' and '$(TargetFramework)' != 'net451' and '$(TargetFramework)' != 'net452' and '$(TargetFramework)' != 'net46' and '$(TargetFramework)' != 'net461'">
|
||||
<DefineConstants>$(DefineConstants);GRAPHQL</DefineConstants>
|
||||
<DefineConstants>$(DefineConstants);GRAPHQL;MIMEKIT</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -95,12 +95,12 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Verify.Xunit" Version="19.6.0" />
|
||||
<PackageReference Include="MimeKitLite" Version="4.1.0.1" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' != 'net452'">
|
||||
<PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
|
||||
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.4.0" />
|
||||
<!--<PackageReference Include="JsonConverter.NewtonSoft.Json" Version="0.4.0" />-->
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -138,5 +138,4 @@
|
||||
<ItemGroup>
|
||||
<Folder Include="Pact\files\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
75
test/WireMock.Net.Tests/WireMockServerTests.WithMultiPart.cs
Normal file
75
test/WireMock.Net.Tests/WireMockServerTests.WithMultiPart.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
#if MIMEKIT
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
public partial class WireMockServerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithMultiPartBody_Using_MimePartMatchers()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
var textPlainContent = "This is some plain text";
|
||||
var textPlainContentType = "text/plain";
|
||||
var textPlainContentTypeMatcher = new ContentTypeMatcher(textPlainContentType);
|
||||
var textPlainContentMatcher = new ExactMatcher(textPlainContent);
|
||||
var textPlainMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textPlainContentTypeMatcher, null, null, textPlainContentMatcher);
|
||||
|
||||
var textJson = "{ \"Key\" : \"Value\" }";
|
||||
var textJsonContentType = "text/json";
|
||||
var textJsonContentTypeMatcher = new ContentTypeMatcher(textJsonContentType);
|
||||
var textJsonContentMatcher = new JsonMatcher(new { Key = "Value" }, true);
|
||||
var jsonMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textJsonContentTypeMatcher, null, null, textJsonContentMatcher);
|
||||
|
||||
var imagePngBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC");
|
||||
var imagePngContentMatcher = new ExactObjectMatcher(imagePngBytes);
|
||||
var imagePngMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, null, null, null, imagePngContentMatcher);
|
||||
|
||||
var matchers = new IMatcher[]
|
||||
{
|
||||
textPlainMatcher,
|
||||
jsonMatcher,
|
||||
imagePngMatcher
|
||||
};
|
||||
|
||||
server
|
||||
.Given(
|
||||
Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("/multipart")
|
||||
.WithMultiPart(matchers))
|
||||
.RespondWith(Response.Create());
|
||||
|
||||
// Act
|
||||
var formDataContent = new MultipartFormDataContent();
|
||||
formDataContent.Add(new StringContent(textPlainContent, Encoding.UTF8, textPlainContentType), "text");
|
||||
formDataContent.Add(new StringContent(textJson, Encoding.UTF8, textJsonContentType), "json");
|
||||
|
||||
var fileContent = new ByteArrayContent(imagePngBytes);
|
||||
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
|
||||
formDataContent.Add(fileContent, "somefile", "image.png");
|
||||
|
||||
var client = server.CreateClient();
|
||||
|
||||
var response = await client.PostAsync("/multipart", formDataContent);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user