mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-13 05:45:54 +01:00
Add UseDefinedRequestMatchers to ProxyAndRecordSettings (#821)
* . * UseDefinedRequestMatchers * ok * . * ClientIP * t * fix ut * . * cf * cf2
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Serialization;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
public class ProxyMappingConverterTests
|
||||
{
|
||||
private readonly WireMockServerSettings _settings = new();
|
||||
|
||||
private readonly MappingConverter _mappingConverter;
|
||||
|
||||
private readonly ProxyMappingConverter _sut;
|
||||
|
||||
public ProxyMappingConverterTests()
|
||||
{
|
||||
var guidUtilsMock = new Mock<IGuidUtils>();
|
||||
guidUtilsMock.Setup(g => g.NewGuid()).Returns(Guid.Parse("ff55ac0a-fea9-4d7b-be74-5e483a2c1305"));
|
||||
|
||||
_mappingConverter = new MappingConverter(new MatcherMapper(_settings));
|
||||
|
||||
_sut = new ProxyMappingConverter(_settings, guidUtilsMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMapping_UseDefinedRequestMatchers_True()
|
||||
{
|
||||
// Arrange
|
||||
var proxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
UseDefinedRequestMatchers = true
|
||||
};
|
||||
|
||||
var request = Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("x")
|
||||
.WithParam("p1", "p1-v")
|
||||
.WithParam("p2", "p2-v")
|
||||
.WithHeader("Content-Type", new ContentTypeMatcher("text/plain"))
|
||||
.WithCookie("c", "x")
|
||||
.WithBody(new RegexMatcher("<RequestType>Auth</RequestType"));
|
||||
|
||||
var mappingMock = new Mock<IMapping>();
|
||||
mappingMock.SetupGet(m => m.RequestMatcher).Returns(request);
|
||||
mappingMock.SetupGet(m => m.Title).Returns("my title");
|
||||
mappingMock.SetupGet(m => m.Description).Returns("my description");
|
||||
|
||||
var requestMessageMock = new Mock<IRequestMessage>();
|
||||
|
||||
var responseMessage = new ResponseMessage();
|
||||
|
||||
// Act
|
||||
var proxyMapping = _sut.ToMapping(mappingMock.Object, proxyAndRecordSettings, requestMessageMock.Object, responseMessage);
|
||||
|
||||
// Assert
|
||||
var model = _mappingConverter.ToMappingModel(proxyMapping);
|
||||
var json = JsonConvert.SerializeObject(model, JsonSerializationConstants.JsonSerializerSettingsDefault);
|
||||
var expected = File.ReadAllText(Path.Combine("../../../", "Serialization", "files", "proxy.json"));
|
||||
|
||||
json.Should().Be(expected);
|
||||
}
|
||||
}
|
||||
72
test/WireMock.Net.Tests/Serialization/files/proxy.json
Normal file
72
test/WireMock.Net.Tests/Serialization/files/proxy.json
Normal file
@@ -0,0 +1,72 @@
|
||||
{
|
||||
"Guid": "ff55ac0a-fea9-4d7b-be74-5e483a2c1305",
|
||||
"Title": "my title",
|
||||
"Description": "my description",
|
||||
"Priority": -2000000,
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "x",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"POST"
|
||||
],
|
||||
"Headers": [
|
||||
{
|
||||
"Name": "Content-Type",
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "ContentTypeMatcher",
|
||||
"Pattern": "text/plain",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Cookies": [
|
||||
{
|
||||
"Name": "c",
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "x",
|
||||
"IgnoreCase": true
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Params": [
|
||||
{
|
||||
"Name": "p1",
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "ExactMatcher",
|
||||
"Pattern": "p1-v"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"Name": "p2",
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "ExactMatcher",
|
||||
"Pattern": "p2-v"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Body": {
|
||||
"Matcher": {
|
||||
"Name": "RegexMatcher",
|
||||
"Pattern": "<RequestType>Auth</RequestType",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"Response": {}
|
||||
}
|
||||
@@ -85,6 +85,9 @@
|
||||
<None Update="responsebody.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Serialization\files\proxy.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="__admin\mappings.org\*.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
@@ -105,6 +108,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Pact\files\" />
|
||||
<Folder Include="Serialization\files\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,3 +1,6 @@
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NFluent;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
@@ -5,11 +8,9 @@ using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NFluent;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Constants;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
@@ -119,6 +120,61 @@ public class WireMockServerProxyTests
|
||||
server.Mappings.Should().HaveCount(28);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Proxy_With_SaveMappingToFile_Is_True_ShouldSaveMappingToFile()
|
||||
{
|
||||
// Assign
|
||||
string path = $"/prx_{Guid.NewGuid()}";
|
||||
var title = "IndexFile";
|
||||
var description = "IndexFile_Test";
|
||||
var stringBody = "<pretendXml>value</pretendXml>";
|
||||
var serverForProxyForwarding = WireMockServer.Start();
|
||||
var fileSystemHandlerMock = new Mock<IFileSystemHandler>();
|
||||
fileSystemHandlerMock.Setup(f => f.GetMappingFolder()).Returns("m");
|
||||
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = serverForProxyForwarding.Urls[0],
|
||||
SaveMapping = false,
|
||||
SaveMappingToFile = true
|
||||
},
|
||||
FileSystemHandler = fileSystemHandlerMock.Object
|
||||
};
|
||||
|
||||
var server = WireMockServer.Start(settings);
|
||||
server.Given(Request.Create()
|
||||
.WithPath("/*")
|
||||
.WithBody(new RegexMatcher(stringBody)))
|
||||
.WithTitle(title)
|
||||
.WithDescription(description)
|
||||
.AtPriority(WireMockConstants.ProxyPriority)
|
||||
.RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings
|
||||
{
|
||||
Url = serverForProxyForwarding.Urls[0],
|
||||
SaveMapping = false,
|
||||
SaveMappingToFile = true,
|
||||
UseDefinedRequestMatchers = true,
|
||||
}));
|
||||
|
||||
// Act
|
||||
var requestMessage = new HttpRequestMessage
|
||||
{
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri($"{server.Urls[0]}{path}"),
|
||||
Content = new StringContent(stringBody)
|
||||
};
|
||||
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
|
||||
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
server.Mappings.Should().HaveCount(2);
|
||||
|
||||
// Verify
|
||||
fileSystemHandlerMock.Verify(f => f.WriteMappingFile($"m{System.IO.Path.DirectorySeparatorChar}{title}.json", It.IsRegex(stringBody)), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Proxy_With_SaveMapping_Is_False_And_SaveMappingToFile_Is_True_ShouldSaveMappingToFile()
|
||||
{
|
||||
@@ -735,8 +791,6 @@ public class WireMockServerProxyTests
|
||||
content.Should().NotBeEmpty();
|
||||
|
||||
server.LogEntries.Should().HaveCount(1);
|
||||
var status = ((StatusModel)server.LogEntries.First().ResponseMessage.BodyData.BodyAsJson).Status;
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user