-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
c83e907
commit 57dac0d
Showing
9 changed files
with
425 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://*/" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Coypu | ||
{ | ||
public enum DriverProxyType | ||
{ | ||
Socks, | ||
Http | ||
} | ||
} |
Oops, something went wrong.