mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 14:40:00 +02: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:
@@ -3,6 +3,7 @@ using System.Net.Http;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using WireMock.Logging;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
@@ -14,9 +15,6 @@ namespace WireMock.Net.Console.Proxy.NETCoreApp2
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
RunTestDifferentPort().Wait(20000); // prints "1"
|
|
||||||
RunTestDifferentPort().Wait(20000); // prints "1"
|
|
||||||
|
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
{
|
{
|
||||||
Urls = new[] { "http://localhost:9091", "https://localhost:9443" },
|
Urls = new[] { "http://localhost:9091", "https://localhost:9443" },
|
||||||
@@ -29,34 +27,18 @@ namespace WireMock.Net.Console.Proxy.NETCoreApp2
|
|||||||
SaveMapping = true,
|
SaveMapping = true,
|
||||||
SaveMappingToFile = false,
|
SaveMappingToFile = false,
|
||||||
ExcludedHeaders = new[] { "dnt", "Content-Length" }
|
ExcludedHeaders = new[] { "dnt", "Content-Length" }
|
||||||
}
|
},
|
||||||
|
Logger= new WireMockConsoleLogger()
|
||||||
});
|
});
|
||||||
|
|
||||||
server.LogEntriesChanged += (sender, eventRecordArgs) =>
|
//server.LogEntriesChanged += (sender, eventRecordArgs) =>
|
||||||
{
|
//{
|
||||||
System.Console.WriteLine(JsonConvert.SerializeObject(eventRecordArgs.NewItems, Formatting.Indented));
|
// System.Console.WriteLine(JsonConvert.SerializeObject(eventRecordArgs.NewItems, Formatting.Indented));
|
||||||
};
|
//};
|
||||||
|
|
||||||
System.Console.WriteLine("Press any key to stop the server");
|
System.Console.WriteLine("Press any key to stop the server");
|
||||||
System.Console.ReadKey();
|
System.Console.ReadKey();
|
||||||
server.Stop();
|
server.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task RunTestDifferentPort()
|
|
||||||
{
|
|
||||||
var server = WireMockServer.Start();
|
|
||||||
|
|
||||||
server.Given(Request.Create().WithPath("/").UsingGet())
|
|
||||||
.RespondWith(Response.Create().WithStatusCode(200).WithBody("Hello"));
|
|
||||||
|
|
||||||
Thread.Sleep(1000);
|
|
||||||
|
|
||||||
var response = await new HttpClient().GetAsync(server.Urls[0]);
|
|
||||||
response.EnsureSuccessStatusCode();
|
|
||||||
|
|
||||||
System.Console.WriteLine("RunTestDifferentPort - server.LogEntries.Count() = " + server.LogEntries.Count());
|
|
||||||
|
|
||||||
server.Stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net5.0</TargetFrameworks>
|
||||||
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
|
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,16 @@ namespace WireMock.RequestBuilders
|
|||||||
return _requestMatchers.OfType<T>().FirstOrDefault();
|
return _requestMatchers.OfType<T>().FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the request message matcher.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">Type of IRequestMatcher</typeparam>
|
||||||
|
/// <returns>A RequestMatcher</returns>
|
||||||
|
public T GetRequestMessageMatcher<T>(Func<T, bool> func) where T : IRequestMatcher
|
||||||
|
{
|
||||||
|
return _requestMatchers.OfType<T>().FirstOrDefault(func);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IClientIPRequestBuilder.WithClientIP(IStringMatcher[])"/>
|
/// <inheritdoc cref="IClientIPRequestBuilder.WithClientIP(IStringMatcher[])"/>
|
||||||
public IRequestBuilder WithClientIP(params IStringMatcher[] matchers)
|
public IRequestBuilder WithClientIP(params IStringMatcher[] matchers)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using FluentAssertions;
|
using System.Threading.Tasks;
|
||||||
using Moq;
|
using FluentAssertions;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using WireMock.Models;
|
using WireMock.Models;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ using WireMock.RequestBuilders;
|
|||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
using WireMock.Util;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
@@ -75,8 +76,8 @@ namespace WireMock.Net.Tests
|
|||||||
await new HttpClient(httpClientHandler).SendAsync(requestMessage);
|
await new HttpClient(httpClientHandler).SendAsync(requestMessage);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(server.Mappings).HasSize(2);
|
server.Mappings.Should().HaveCount(2);
|
||||||
Check.That(server.LogEntries).HasSize(1);
|
server.LogEntries.Should().HaveCount(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -148,10 +149,56 @@ namespace WireMock.Net.Tests
|
|||||||
|
|
||||||
// check that new proxied mapping is added
|
// check that new proxied mapping is added
|
||||||
Check.That(server.Mappings).HasSize(2);
|
Check.That(server.Mappings).HasSize(2);
|
||||||
|
}
|
||||||
|
|
||||||
//var newMapping = _server.Mappings.First(m => m.Guid != guid);
|
[Fact]
|
||||||
//var matcher = ((Request)newMapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageHeaderMatcher>().FirstOrDefault(m => m.Name == "bbb");
|
public async Task WireMockServer_Proxy_Should_preserve_Authorization_header_in_proxied_request()
|
||||||
//Check.That(matcher).IsNotNull();
|
{
|
||||||
|
// 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]
|
[Fact]
|
||||||
|
|||||||
Reference in New Issue
Block a user