Skip to content

Commit

Permalink
Set Playwright HttpCredentials.Origin to scope auth header correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
adiel committed Mar 1, 2024
1 parent 97cbad8 commit 95a48e3
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void CustomBrowser(string browserName)
{
Assert.Inconclusive("This test requires Internet Explorer and will only run on Windows.");
}
var driver = new SeleniumWebDriver(browser, false);
var driver = new SeleniumWebDriver(new SessionConfiguration{Browser = browser, Headless = false});
using (var custom = new BrowserSession(driver))
{
custom.Visit("https://saucelabs.com/test/guinea-pig");
Expand Down
2 changes: 1 addition & 1 deletion src/Coypu.Drivers.Tests/DriverSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static void EnsureDriver()
_driver.Dispose();
}

_driver = (IDriver) Activator.CreateInstance(DriverType, Browser, Headless);
_driver = (IDriver) Activator.CreateInstance(DriverType, new SessionConfiguration{Driver = DriverType, Browser = Browser, Headless = Headless});

_root = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public StubDriverFactory(IDriver driver)
this.driver = driver;
}

public IDriver NewWebDriver(Type driverType, Drivers.Browser browser, bool Headless, string appHost)
public IDriver NewWebDriver(SessionConfiguration sessionConfiguration)
{
return driver;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Coypu/ActivatorDriverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public class ActivatorDriverFactory : DriverFactory
{
public static int OpenDrivers { get; set; }

public IDriver NewWebDriver(Type driverType, Drivers.Browser browser, bool headless, string appHost)
public IDriver NewWebDriver(SessionConfiguration sessionConfiguration)
{
try
{
var driver = (IDriver)Activator.CreateInstance(driverType, browser, headless, appHost);
var driver = (IDriver)Activator.CreateInstance(sessionConfiguration.Driver, sessionConfiguration);
OpenDrivers++;
return driver;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Coypu/BrowserSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal BrowserSession(SessionConfiguration sessionConfiguration,
RestrictedResourceDownloader restrictedResourceDownloader)
: base(sessionConfiguration,
null,
driverFactory.NewWebDriver(sessionConfiguration.Driver, sessionConfiguration.Browser, sessionConfiguration.Headless, sessionConfiguration.AppHost),
driverFactory.NewWebDriver(sessionConfiguration),
timingStrategy,
waiter,
urlBuilder,
Expand Down
2 changes: 1 addition & 1 deletion src/Coypu/DriverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Coypu
{
public interface DriverFactory
{
IDriver NewWebDriver(Type driverType, Drivers.Browser browser, bool headless, string appHost);
IDriver NewWebDriver(SessionConfiguration sessionConfiguration);
}
}
27 changes: 13 additions & 14 deletions src/Coypu/Drivers/Playwright/PlaywrightDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Cookie = System.Net.Cookie;
using Microsoft.Playwright;
using System.Collections.Immutable;
using System.Threading.Tasks;

#pragma warning disable 1591

Expand All @@ -21,36 +20,36 @@ public class PlaywrightDriver : IDriver
private readonly IBrowser _playwrightBrowser;
private IBrowserContext _context;

public PlaywrightDriver(Browser browser, bool headless, string appHost)
public PlaywrightDriver(SessionConfiguration sessionConfiguration)
{
_dialogs = new Dialogs();
_playwright = Microsoft.Playwright.Playwright.CreateAsync().Sync();
_browser = browser;
_headless = headless;
var browserType = PlaywrightBrowserType(browser, _playwright); // TODO: map browser to playwright browser type
_browser = sessionConfiguration.Browser;
_headless = sessionConfiguration.Headless;
var browserType = PlaywrightBrowserType(_browser, _playwright); // TODO: map browser to playwright browser type

_playwrightBrowser = browserType.LaunchAsync(
new BrowserTypeLaunchOptions
{
Headless = headless,
Channel = PlaywrightBrowserChannel(browser),
Headless = _headless,
Channel = PlaywrightBrowserChannel(_browser),
}
).Sync();
NewContext(appHost);
NewContext(sessionConfiguration);
}

private void NewContext(string appHost)
private void NewContext(SessionConfiguration sessionConfiguration)
{
var options = new BrowserNewPageOptions();
if (Uri.IsWellFormedUriString(appHost, UriKind.Absolute))
if (!string.IsNullOrEmpty(sessionConfiguration.AppHost) && !string.IsNullOrEmpty(sessionConfiguration.UserInfo))
{
var userInfo = new Uri(appHost).UserInfo;
if (!string.IsNullOrEmpty(userInfo)) {
var credentials = userInfo.Split(':');
if (!string.IsNullOrEmpty(sessionConfiguration.UserInfo)) {
var credentials = sessionConfiguration.UserInfo.Split(':');
options.HttpCredentials = new HttpCredentials
{
Username = credentials[0],
Password = credentials[1]
Password = credentials[1],
Origin = new FullyQualifiedUrlBuilder().GetFullyQualifiedUrl("", sessionConfiguration).TrimEnd('/')
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Coypu/Drivers/Selenium/DriverFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools.V85.HeadlessExperimental;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
Expand All @@ -12,17 +11,18 @@ namespace Coypu.Drivers.Selenium
{
internal class DriverFactory
{
public IWebDriver NewWebDriver(Browser browser, bool headless)
public IWebDriver NewWebDriver(SessionConfiguration sessionConfiguration)
{
var browser = sessionConfiguration.Browser;
var firefoxOptions = new FirefoxOptions();
var chromeOptions = new ChromeOptions();
EdgeOptions edgeOptions = new EdgeOptions();
if (headless) {
if (sessionConfiguration.Headless) {
firefoxOptions.AddArgument("--headless");
chromeOptions.AddArgument("--headless=new");
edgeOptions.AddArgument("headless");
edgeOptions.AddArgument("disable-gpu");
if (browser == Browser.Safari) {
if (sessionConfiguration.Browser == Browser.Safari) {
throw new NotSupportedException("Safari does not support headless mode");
}
if (browser == Browser.Safari) {
Expand Down
4 changes: 2 additions & 2 deletions src/Coypu/Drivers/Selenium/SeleniumWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class SeleniumWebDriver : IDriver
private IWebDriver _webDriver;
private readonly WindowHandleFinder _windowHandleFinder;

public SeleniumWebDriver(Browser browser, bool headless, string _appHost = null)
: this(new DriverFactory().NewWebDriver(browser, headless), browser) { }
public SeleniumWebDriver(SessionConfiguration sessionConfiguration)
: this(new DriverFactory().NewWebDriver(sessionConfiguration), sessionConfiguration.Browser) { }

protected SeleniumWebDriver(IWebDriver webDriver,
Browser browser)
Expand Down
47 changes: 29 additions & 18 deletions src/Coypu/FullyQualifiedUrlBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
using System;

namespace Coypu
{
internal class FullyQualifiedUrlBuilder : UrlBuilder
{
public string GetFullyQualifiedUrl(string virtualPath, SessionConfiguration sessionConfiguration)
{
var scheme = sessionConfiguration.SSL ? "https" : "http";
var userInfoPart = string.IsNullOrEmpty(sessionConfiguration.UserInfo) ? "" : sessionConfiguration.UserInfo + "@";
var baseUrl = sessionConfiguration.Port == 80
? $"{scheme}://{userInfoPart}{sessionConfiguration.AppHost}"
: $"{scheme}://{userInfoPart}{sessionConfiguration.AppHost}:{sessionConfiguration.Port}";

return new Uri(new Uri(baseUrl), virtualPath).AbsoluteUri;
}
}
}
using System;

namespace Coypu
{
internal class FullyQualifiedUrlBuilder : UrlBuilder
{
public string GetFullyQualifiedUrl(string virtualPath, SessionConfiguration sessionConfiguration)
{
var scheme = sessionConfiguration.SSL ? "https" : "http";
string userInfoPart = GetUserInfoPart(sessionConfiguration);
var baseUrl = sessionConfiguration.Port == 80
? $"{scheme}://{userInfoPart}{sessionConfiguration.AppHost}"
: $"{scheme}://{userInfoPart}{sessionConfiguration.AppHost}:{sessionConfiguration.Port}";

return new Uri(new Uri(baseUrl), virtualPath).AbsoluteUri;
}

private static string GetUserInfoPart(SessionConfiguration sessionConfiguration)
{
// PlaywrightDriver implements basic auth properly via the Authorization header
if (sessionConfiguration.Driver == typeof(Drivers.Playwright.PlaywrightDriver) ||
string.IsNullOrEmpty(sessionConfiguration.UserInfo))
{
return string.Empty;
}
return sessionConfiguration.UserInfo + "@";
}
}
}
14 changes: 7 additions & 7 deletions src/Coypu/UrlBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Coypu
{
public interface UrlBuilder
{
string GetFullyQualifiedUrl(string virtualPath, SessionConfiguration sessionConfiguration);
}
}
namespace Coypu
{
public interface UrlBuilder
{
string GetFullyQualifiedUrl(string virtualPath, SessionConfiguration sessionConfiguration);
}
}

0 comments on commit 95a48e3

Please sign in to comment.