-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Basic Authentication extension. Fixes #31
- Added basic authentication header extension
- Loading branch information
1 parent
478452a
commit 1554060
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
VaraniumSharp.Tests/Extensions/HttpClientExtensionsTests.cs
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,33 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using VaraniumSharp.Extensions; | ||
|
||
namespace VaraniumSharp.Tests.Extensions | ||
{ | ||
public class HttpClientExtensionsTests | ||
{ | ||
#region Public Methods | ||
|
||
[Test] | ||
public void SettingAuthenticationHeaderWorks() | ||
{ | ||
// arrange | ||
const string username = "testuser"; | ||
const string password = "testpass"; | ||
var authValue = $"Basic {Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"))}"; | ||
var sut = new HttpClient(); | ||
|
||
// act | ||
sut.SetBasicAuthHeader(username, password); | ||
|
||
// assert | ||
sut.DefaultRequestHeaders.GetValues("Authorization") | ||
.Should().Contain(authValue); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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,31 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
|
||
namespace VaraniumSharp.Extensions | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="HttpClient"/> | ||
/// </summary> | ||
public static class HttpClientExtensions | ||
{ | ||
#region Public Methods | ||
|
||
/// <summary> | ||
/// Set Basic Authentication header | ||
/// </summary> | ||
/// <param name="client">HttpClient for which the header should be set</param> | ||
/// <param name="username">Username for authentication</param> | ||
/// <param name="password">Password for authentication</param> | ||
/// <returns>HttpClient</returns> | ||
public static HttpClient SetBasicAuthHeader(this HttpClient client, string username, string password) | ||
{ | ||
var credentials = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); | ||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); | ||
return client; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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