Skip to content

Commit

Permalink
Allow using a Proxy (#236)
Browse files Browse the repository at this point in the history
* Allow using a Proxy

* Test with proxy server

* Rename test: Driver_Uses_Proxy

* Better assertion
---------

Co-authored-by: Tom Longhurst <[email protected]>
  • Loading branch information
thomhurst and msm-tomlonghurst authored Apr 8, 2024
1 parent c83e907 commit 57dac0d
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 116 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ src/packages
*.ide
/.vs/slnx.sqlite
/src/Coypu.Tests/Coypu.Tests.nuget.props
/src/Coypu.Tests
/src/Coypu.NUnit/Coypu.NUnit.nuget.props
/src/Coypu.NUnit
/src/Coypu.Drivers.Tests/Coypu.Drivers.Tests.nuget.targets
/src/Coypu.Drivers.Tests/Coypu.Drivers.Tests.nuget.props
/src/Coypu.Drivers.Tests
/src/Coypu.AcceptanceTests/Coypu.AcceptanceTests.nuget.targets
/src/Coypu.AcceptanceTests/Coypu.AcceptanceTests.nuget.props
/src/Coypu.AcceptanceTests/project.lock.json
Expand Down
7 changes: 7 additions & 0 deletions src/Coypu.Tests/Coypu.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
<PackageReference Include="WebDriverManager" Version="2.17.2" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Coypu\Coypu.csproj" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<None Update="proxysettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
91 changes: 91 additions & 0 deletions src/Coypu.Tests/ProxyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Coypu.Drivers;
using Coypu.Drivers.Playwright;
using Coypu.Drivers.Selenium;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using WebDriverManager.DriverConfigs.Impl;

namespace Coypu.Tests;

[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class ProxyTests
{
private static WebApplication _app;
private static string _proxyServer;

[OneTimeSetUp]
public static void SetUp()
{
var builder = WebApplication.CreateBuilder();
builder.Configuration.AddJsonFile("proxysettings.json");

builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

_app = builder.Build();

_app.UseRouting();
_app.MapReverseProxy();

_app.MapGet("/acceptance-test", context =>
{
const string html = "<!DOCTYPE html><html><head></head><body><h1 id=\"title\">This page has been proxied successfully.</h1></body></html>";

context.Response.ContentType = MediaTypeNames.Text.Html;
context.Response.ContentLength = Encoding.UTF8.GetByteCount(html);
return context.Response.WriteAsync(html);
}
);

_app.RunAsync();

_proxyServer = _app.Services.GetRequiredService<IServer>()
.Features.Get<IServerAddressesFeature>()
.Addresses
.First();
}

[OneTimeTearDown]
public static async Task TearDown()
{
await _app.DisposeAsync();
}

[TestCase(typeof(PlaywrightDriver))]
[TestCase(typeof(SeleniumWebDriver))]
public void Driver_Uses_Proxy(Type driverType)
{
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
var sessionConfiguration = new SessionConfiguration
{
AcceptInsecureCertificates = true,
Proxy = new DriverProxy
{
Server = _proxyServer,
},
Browser = Browser.Chrome,
Driver = driverType,
};

using var browser = new BrowserSession(sessionConfiguration);

// Proxy turns this example.com into localhost, which has an endpoint configured for this URL in the setup
browser.Visit("http://www.example.com/acceptance-test");

// So we then assert we can find the title in the HTML we've served
var title = browser.FindId("title");

Assert.That(title.Exists(), Is.True);
Assert.That(title.Text, Is.EqualTo("This page has been proxied successfully."));
}
}
41 changes: 41 additions & 0 deletions src/Coypu.Tests/proxysettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"route1" : {
"ClusterId": "cluster1",
"Match": {
"Path": "{**catch-all}",
"Hosts": ["www.example.com"]
}
},
"route2" : {
"ClusterId": "cluster2",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"cluster1": {
"Destinations": {
"destination1": {
"Address": "http://localhost:5000/"
}
}
},
"cluster2": {
"Destinations": {
"destination1": {
"Address": "https://*/"
}
}
}
}
}
}
40 changes: 40 additions & 0 deletions src/Coypu/DriverProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;

namespace Coypu
{
/// <summary>
/// Proxy information for a Driver
/// </summary>
public class DriverProxy
{
/// <summary>
/// The Username for the proxy
/// </summary>
public string Username { get; set; }

/// <summary>
/// The Password for the proxy
/// </summary>
public string Password { get; set; }

/// <summary>
/// The Server of the proxy
/// </summary>
public string Server { get; set; }

/// <summary>
/// Use proxy for SSL
/// </summary>
public bool Ssl { get; set; } = true;

/// <summary>
/// Use type of proxy
/// </summary>
public DriverProxyType Type { get; set; } = DriverProxyType.Http;

/// <summary>
/// Domains to bypass
/// </summary>
public IEnumerable<string> BypassAddresses { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/Coypu/DriverProxyType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Coypu
{
public enum DriverProxyType
{
Socks,
Http
}
}
Loading

0 comments on commit 57dac0d

Please sign in to comment.