Added unit test for TryGetBasicAuthCredentials extension method and minor code tidy.

This commit is contained in:
Andy Butland
2021-08-06 12:05:08 +02:00
parent 7859798e8b
commit 9732381933
4 changed files with 50 additions and 8 deletions

View File

@@ -0,0 +1,42 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Text;
using Microsoft.AspNetCore.Http;
using NUnit.Framework;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
{
[TestFixture]
public class HttpContextExtensionTests
{
[Test]
public void TryGetBasicAuthCredentials_WithoutHeader_ReturnsFalse()
{
var httpContext = new DefaultHttpContext();
var result = httpContext.TryGetBasicAuthCredentials(out string _, out string _);
Assert.IsFalse(result);
}
[Test]
public void TryGetBasicAuthCredentials_WithHeader_ReturnsTrueWithCredentials()
{
const string Username = "fred";
const string Password = "test";
var httpContext = new DefaultHttpContext();
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{Username}:{Password}"));
httpContext.Request.Headers.Add("Authorization", $"Basic {credentials}");
bool result = httpContext.TryGetBasicAuthCredentials(out string username, out string password);
Assert.IsTrue(result);
Assert.AreEqual(Username, username);
Assert.AreEqual(Password, password);
}
}
}