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+ }
0 commit comments