Add UseDefinedRequestMatchers to ProxyAndRecordSettings (#821)

* .

* UseDefinedRequestMatchers

* ok

* .

* ClientIP

* t

* fix ut

* .

* cf

* cf2
This commit is contained in:
Stef Heyenrath
2022-09-30 11:25:11 +02:00
committed by GitHub
parent c0b18631a3
commit f7b04f3234
20 changed files with 486 additions and 131 deletions

View File

@@ -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();
}
}