mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-24 18:11:10 +01:00
Add more tests for Proxy with Authorization (#555)
* WireMockServer_Proxy_Should_preserve_Authorization_header_in_proxied_request * !admin * x * .
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using WireMock.Models;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Settings;
|
||||
|
||||
@@ -13,6 +13,7 @@ using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
@@ -75,8 +76,8 @@ namespace WireMock.Net.Tests
|
||||
await new HttpClient(httpClientHandler).SendAsync(requestMessage);
|
||||
|
||||
// Assert
|
||||
Check.That(server.Mappings).HasSize(2);
|
||||
Check.That(server.LogEntries).HasSize(1);
|
||||
server.Mappings.Should().HaveCount(2);
|
||||
server.LogEntries.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -148,10 +149,56 @@ namespace WireMock.Net.Tests
|
||||
|
||||
// check that new proxied mapping is added
|
||||
Check.That(server.Mappings).HasSize(2);
|
||||
}
|
||||
|
||||
//var newMapping = _server.Mappings.First(m => m.Guid != guid);
|
||||
//var matcher = ((Request)newMapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageHeaderMatcher>().FirstOrDefault(m => m.Name == "bbb");
|
||||
//Check.That(matcher).IsNotNull();
|
||||
[Fact]
|
||||
public async Task WireMockServer_Proxy_Should_preserve_Authorization_header_in_proxied_request()
|
||||
{
|
||||
// Assign
|
||||
string path = $"/prx_{Guid.NewGuid()}";
|
||||
var serverForProxyForwarding = WireMockServer.Start();
|
||||
serverForProxyForwarding
|
||||
.Given(Request.Create().WithPath(path))
|
||||
.RespondWith(Response.Create().WithCallback(x => new ResponseMessage
|
||||
{
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = x.Headers["Authorization"].ToString(),
|
||||
DetectedBodyType = Types.BodyType.String
|
||||
}
|
||||
}));
|
||||
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = serverForProxyForwarding.Urls[0],
|
||||
SaveMapping = true,
|
||||
SaveMappingToFile = false
|
||||
}
|
||||
};
|
||||
var server = WireMockServer.Start(settings);
|
||||
|
||||
// Act
|
||||
var requestMessage = new HttpRequestMessage
|
||||
{
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri($"{server.Urls[0]}{path}"),
|
||||
Content = new StringContent("stringContent", Encoding.ASCII)
|
||||
};
|
||||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("BASIC", "test-A");
|
||||
var result = await new HttpClient().SendAsync(requestMessage);
|
||||
|
||||
// Assert
|
||||
(await result.Content.ReadAsStringAsync()).Should().Be("BASIC test-A");
|
||||
|
||||
var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage;
|
||||
var authorizationHeader = receivedRequest.Headers["Authorization"].ToString().Should().Be("BASIC test-A");
|
||||
|
||||
server.Mappings.Should().HaveCount(2);
|
||||
var authorizationRequestMessageHeaderMatcher = ((Request)server.Mappings.Single(m => !m.IsAdminInterface).RequestMatcher)
|
||||
.GetRequestMessageMatcher<RequestMessageHeaderMatcher>(x => x.Matchers.Any(m => m.GetPatterns().Contains("BASIC test-A")));
|
||||
authorizationRequestMessageHeaderMatcher.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user