Skip to content

Commit 57dac0d

Browse files
Allow using a Proxy (#236)
* Allow using a Proxy * Test with proxy server * Rename test: Driver_Uses_Proxy * Better assertion --------- Co-authored-by: Tom Longhurst <130671542+msm-tomlonghurst@users.noreply.github.com>
1 parent c83e907 commit 57dac0d

File tree

9 files changed

+425
-116
lines changed

9 files changed

+425
-116
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ src/packages
3434
*.ide
3535
/.vs/slnx.sqlite
3636
/src/Coypu.Tests/Coypu.Tests.nuget.props
37-
/src/Coypu.Tests
3837
/src/Coypu.NUnit/Coypu.NUnit.nuget.props
39-
/src/Coypu.NUnit
4038
/src/Coypu.Drivers.Tests/Coypu.Drivers.Tests.nuget.targets
4139
/src/Coypu.Drivers.Tests/Coypu.Drivers.Tests.nuget.props
42-
/src/Coypu.Drivers.Tests
4340
/src/Coypu.AcceptanceTests/Coypu.AcceptanceTests.nuget.targets
4441
/src/Coypu.AcceptanceTests/Coypu.AcceptanceTests.nuget.props
4542
/src/Coypu.AcceptanceTests/project.lock.json

src/Coypu.Tests/Coypu.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
<PackageReference Include="NUnit" Version="3.13.2" />
99
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
1010
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
11+
<PackageReference Include="WebDriverManager" Version="2.17.2" />
12+
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
1113
</ItemGroup>
1214
<ItemGroup>
1315
<ProjectReference Include="..\Coypu\Coypu.csproj" />
1416
</ItemGroup>
1517
<ItemGroup>
1618
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
1719
</ItemGroup>
20+
<ItemGroup>
21+
<None Update="proxysettings.json">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
1825
</Project>

src/Coypu.Tests/ProxyTests.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Linq;
3+
using System.Net.Mime;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Coypu.Drivers;
7+
using Coypu.Drivers.Playwright;
8+
using Coypu.Drivers.Selenium;
9+
using Microsoft.AspNetCore.Builder;
10+
using Microsoft.AspNetCore.Hosting.Server;
11+
using Microsoft.AspNetCore.Hosting.Server.Features;
12+
using Microsoft.AspNetCore.Http;
13+
using Microsoft.Extensions.Configuration;
14+
using Microsoft.Extensions.DependencyInjection;
15+
using NUnit.Framework;
16+
using WebDriverManager.DriverConfigs.Impl;
17+
18+
namespace Coypu.Tests;
19+
20+
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
21+
public class ProxyTests
22+
{
23+
private static WebApplication _app;
24+
private static string _proxyServer;
25+
26+
[OneTimeSetUp]
27+
public static void SetUp()
28+
{
29+
var builder = WebApplication.CreateBuilder();
30+
builder.Configuration.AddJsonFile("proxysettings.json");
31+
32+
builder.Services.AddReverseProxy()
33+
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
34+
35+
_app = builder.Build();
36+
37+
_app.UseRouting();
38+
_app.MapReverseProxy();
39+
40+
_app.MapGet("/acceptance-test", context =>
41+
{
42+
const string html = "<!DOCTYPE html><html><head></head><body><h1 id=\"title\">This page has been proxied successfully.</h1></body></html>";
43+
44+
context.Response.ContentType = MediaTypeNames.Text.Html;
45+
context.Response.ContentLength = Encoding.UTF8.GetByteCount(html);
46+
return context.Response.WriteAsync(html);
47+
}
48+
);
49+
50+
_app.RunAsync();
51+
52+
_proxyServer = _app.Services.GetRequiredService<IServer>()
53+
.Features.Get<IServerAddressesFeature>()
54+
.Addresses
55+
.First();
56+
}
57+
58+
[OneTimeTearDown]
59+
public static async Task TearDown()
60+
{
61+
await _app.DisposeAsync();
62+
}
63+
64+
[TestCase(typeof(PlaywrightDriver))]
65+
[TestCase(typeof(SeleniumWebDriver))]
66+
public void Driver_Uses_Proxy(Type driverType)
67+
{
68+
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
69+
var sessionConfiguration = new SessionConfiguration
70+
{
71+
AcceptInsecureCertificates = true,
72+
Proxy = new DriverProxy
73+
{
74+
Server = _proxyServer,
75+
},
76+
Browser = Browser.Chrome,
77+
Driver = driverType,
78+
};
79+
80+
using var browser = new BrowserSession(sessionConfiguration);
81+
82+
// Proxy turns this example.com into localhost, which has an endpoint configured for this URL in the setup
83+
browser.Visit("http://www.example.com/acceptance-test");
84+
85+
// So we then assert we can find the title in the HTML we've served
86+
var title = browser.FindId("title");
87+
88+
Assert.That(title.Exists(), Is.True);
89+
Assert.That(title.Text, Is.EqualTo("This page has been proxied successfully."));
90+
}
91+
}

src/Coypu.Tests/proxysettings.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information"
5+
}
6+
},
7+
"AllowedHosts": "*",
8+
"ReverseProxy": {
9+
"Routes": {
10+
"route1" : {
11+
"ClusterId": "cluster1",
12+
"Match": {
13+
"Path": "{**catch-all}",
14+
"Hosts": ["www.example.com"]
15+
}
16+
},
17+
"route2" : {
18+
"ClusterId": "cluster2",
19+
"Match": {
20+
"Path": "{**catch-all}"
21+
}
22+
}
23+
},
24+
"Clusters": {
25+
"cluster1": {
26+
"Destinations": {
27+
"destination1": {
28+
"Address": "http://localhost:5000/"
29+
}
30+
}
31+
},
32+
"cluster2": {
33+
"Destinations": {
34+
"destination1": {
35+
"Address": "https://*/"
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}

src/Coypu/DriverProxy.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
3+
namespace Coypu
4+
{
5+
/// <summary>
6+
/// Proxy information for a Driver
7+
/// </summary>
8+
public class DriverProxy
9+
{
10+
/// <summary>
11+
/// The Username for the proxy
12+
/// </summary>
13+
public string Username { get; set; }
14+
15+
/// <summary>
16+
/// The Password for the proxy
17+
/// </summary>
18+
public string Password { get; set; }
19+
20+
/// <summary>
21+
/// The Server of the proxy
22+
/// </summary>
23+
public string Server { get; set; }
24+
25+
/// <summary>
26+
/// Use proxy for SSL
27+
/// </summary>
28+
public bool Ssl { get; set; } = true;
29+
30+
/// <summary>
31+
/// Use type of proxy
32+
/// </summary>
33+
public DriverProxyType Type { get; set; } = DriverProxyType.Http;
34+
35+
/// <summary>
36+
/// Domains to bypass
37+
/// </summary>
38+
public IEnumerable<string> BypassAddresses { get; set; }
39+
}
40+
}

src/Coypu/DriverProxyType.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Coypu
2+
{
3+
public enum DriverProxyType
4+
{
5+
Socks,
6+
Http
7+
}
8+
}

0 commit comments

Comments
 (0)