Skip to content

Commit

Permalink
Translate basic auth supplied in url to auth header (playwright only)
Browse files Browse the repository at this point in the history
  • Loading branch information
adiel committed Feb 27, 2024
1 parent 4f77292 commit c6628f3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/Coypu.AcceptanceTests/BasicAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using Coypu.AcceptanceTests.Sites;
using Coypu.Drivers.Playwright;
using Coypu.NUnit.Matchers;
using NUnit.Framework;

namespace Coypu.AcceptanceTests
{
[TestFixture]
public class BasicAuth
{
private SelfHostedSite site;
private BrowserSession browser;

[SetUp]
public void SetUp()
{
site = new SelfHostedSite();
}

[TearDown]
public void TearDown()
{
browser.Dispose();
site.Dispose();
}

[Test]
public void It_passes_through_basic_auth_from_url_as_auth_header()
{
var configuration = new SessionConfiguration
{
Timeout = TimeSpan.FromMilliseconds(1000),
Port = site.BaseUri.Port,
AppHost = "http://username:passw0rd@localhost",
Driver = typeof(PlaywrightDriver), // Selenium can't do this
Headless = false,
Browser = Drivers.Browser.Chromium
};

browser = new BrowserSession(configuration);
browser.Visit("/headers");
Assert.That(browser, Shows.Content("Authorization: " + GetBasicAuthHeader("username", "passw0rd")));

browser.Visit("http://un2:pw2@localhost:" + site.BaseUri.Port + "/headers");
Assert.That(browser, Shows.Content("Authorization: " + GetBasicAuthHeader("un2", "pw2")));

browser.Visit("http://localhost:" + site.BaseUri.Port + "/headers");
Assert.That(browser, Shows.Content("Authorization:"));
Assert.That(browser, Shows.No.Content("Authorization: Basic"));
}

private string GetBasicAuthHeader(string username, string password)
{
var auth = $"{username}:{password}";
var bytes = Encoding.UTF8.GetBytes(auth);
return "Basic " + Convert.ToBase64String(bytes);
}
}
}
5 changes: 5 additions & 0 deletions src/Coypu.AcceptanceTests/sites/SelfHostedSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public SelfHostedSite()
return Results.Text("bdd");
return Results.NoContent();
});
_app.MapGet("/headers",
(HttpRequest req) =>
{
return Results.Text(req.Headers.Select(h => $"{h.Key}: {h.Value}").Aggregate((a, b) => $"{a}\n{b}"));
});

_app.StartAsync().Wait();
}
Expand Down
14 changes: 13 additions & 1 deletion src/Coypu/Drivers/Playwright/PlaywrightDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text.RegularExpressions;
using Cookie = System.Net.Cookie;
using Microsoft.Playwright;
using OpenQA.Selenium.DevTools.V85.Network;
using System.Text;

#pragma warning disable 1591

Expand Down Expand Up @@ -200,6 +200,18 @@ public void CancelPrompt(string text, DriverScope scope, Action trigger)
public void Visit(string url,
Scope scope)
{
string userInfo = new Uri(url).UserInfo;
if (string.IsNullOrEmpty(userInfo))
{
_context.SetExtraHTTPHeadersAsync(new Dictionary<string, string> {
{ "Authorization", string.Empty }
}).Sync();
} else {
var base64EncodedAuthenticationString = Convert.ToBase64String(Encoding.ASCII.GetBytes(userInfo));
_context.SetExtraHTTPHeadersAsync(new Dictionary<string, string> {
{ "Authorization", "Basic " + base64EncodedAuthenticationString }
}).Sync();
}
IResponse response = PlaywrightPage(scope).GotoAsync(url).Sync();
if (response != null && response.Status != 200)
{
Expand Down

0 comments on commit c6628f3

Please sign in to comment.