Skip to content

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
louisfischer committed Nov 15, 2021
2 parents 33ed62b + 8c460b4 commit 6f722ac
Show file tree
Hide file tree
Showing 7 changed files with 703 additions and 118 deletions.
2 changes: 1 addition & 1 deletion nuspec/nuget/Cake.Http.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<description>HttpClient addin for cake build.</description>
<summary>HttpClient addin for cake build.</summary>
<projectUrl>https://github.com/cake-contrib/Cake.Http</projectUrl>
<iconUrl>https://cdn.jsdelivr.net/gh/cake-contrib/graphics/png/cake-contrib-medium.png</iconUrl>
<iconUrl>https://cdn.jsdelivr.net/gh/cake-contrib/graphics/png/addin/cake-contrib-addin-medium.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<copyright>Copyright (c) Cake Contributions Organization 2017</copyright>
<license type="expression">MIT</license>
Expand Down
68 changes: 0 additions & 68 deletions src/Cake.Http.Tests/Unit/HttpSettingsExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,52 +281,6 @@ public void EnsureSuccessCodeMethod_Extension_Should_Set_EnsureSuccesscCode_Prop

public sealed class TheUseBasicAuthorizationMethod
{
[Fact]
[Trait(Traits.TestCategory, TestCategories.Unit)]
public void UseBasicAuthorizationMethod_Extension_Should_Throw_On_Null_Or_Empty_UserName_Parameter()
{
//Given
HttpSettings settings = new HttpSettings();
string userName = null;
string password = "tiger";

//When
userName = null;
var nullRecord = Record.Exception(() => HttpSettingsExtensions.UseBasicAuthorization(settings, userName, password));
userName = string.Empty;
var emptyRecord = Record.Exception(() => HttpSettingsExtensions.UseBasicAuthorization(settings, userName, password));
userName = " ";
var spaceRecord = Record.Exception(() => HttpSettingsExtensions.UseBasicAuthorization(settings, userName, password));

//Then
CakeAssert.IsArgumentNullException(nullRecord, nameof(userName));
CakeAssert.IsArgumentNullException(emptyRecord, nameof(userName));
CakeAssert.IsArgumentNullException(spaceRecord, nameof(userName));
}

[Fact]
[Trait(Traits.TestCategory, TestCategories.Unit)]
public void UseBasicAuthorizationMethod_Extension_Should_Throw_On_Null_Or_Empty_Password_Parameter()
{
//Given
HttpSettings settings = new HttpSettings();
string userName = "scott";
string password = null;

//When
password = null;
var nullRecord = Record.Exception(() => HttpSettingsExtensions.UseBasicAuthorization(settings, userName, password));
password = string.Empty;
var emptyRecord = Record.Exception(() => HttpSettingsExtensions.UseBasicAuthorization(settings, userName, password));
password = " ";
var spaceRecord = Record.Exception(() => HttpSettingsExtensions.UseBasicAuthorization(settings, userName, password));

//Then
CakeAssert.IsArgumentNullException(nullRecord, nameof(password));
CakeAssert.IsArgumentNullException(emptyRecord, nameof(password));
CakeAssert.IsArgumentNullException(spaceRecord, nameof(password));
}

[Fact]
[Trait(Traits.TestCategory, TestCategories.Unit)]
public void UseBasicAuthorizationMethod_Extension_Should_Add_Basic_Authorization_Header()
Expand All @@ -350,28 +304,6 @@ public void UseBasicAuthorizationMethod_Extension_Should_Add_Basic_Authorization

public sealed class TheUseBearerAuthorizationMethod
{
[Fact]
[Trait(Traits.TestCategory, TestCategories.Unit)]
public void UseBearerAuthorizationMethod_Extension_Should_Throw_On_Null_Or_Empty_Token_Parameter()
{
//Given
HttpSettings settings = new HttpSettings();
string token = null;

//When
token = null;
var nullRecord = Record.Exception(() => HttpSettingsExtensions.UseBearerAuthorization(settings, token));
token = string.Empty;
var emptyRecord = Record.Exception(() => HttpSettingsExtensions.UseBearerAuthorization(settings, token));
token = " ";
var spaceRecord = Record.Exception(() => HttpSettingsExtensions.UseBearerAuthorization(settings, token));

//Then
CakeAssert.IsArgumentNullException(nullRecord, nameof(token));
CakeAssert.IsArgumentNullException(emptyRecord, nameof(token));
CakeAssert.IsArgumentNullException(spaceRecord, nameof(token));
}

[Fact]
[Trait(Traits.TestCategory, TestCategories.Unit)]
public void UseBearerAuthorizationMethod_Extension_Should_Add_Bearer_Authorization_Header()
Expand Down
2 changes: 1 addition & 1 deletion src/Cake.Http/Cake.Http.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
Expand Down
40 changes: 20 additions & 20 deletions src/Cake.Http/CakeHttpClientHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Cake.Core;
using Cake.Core;
using System;
using System.Linq;
using System.Net.Http;
Expand All @@ -18,16 +18,16 @@ public class CakeHttpClientHandler : WebRequestHandler
#else
public class CakeHttpClientHandler : HttpClientHandler
#endif
{
private readonly HttpSettings _Settings;
private readonly ICakeContext _Context;
{
private readonly HttpSettings _Settings;
private readonly ICakeContext _Context;

/// <summary>
/// Custom HTTP Handler to delegate the processing of HTTP requests and extending it.
/// </summary>
/// <param name="context">Cake Context the request is geting </param>
/// <param name="settings">HttpSettings to apply to the inner handler</param>
public CakeHttpClientHandler(ICakeContext context, HttpSettings settings)
/// <summary>
/// Custom HTTP Handler to delegate the processing of HTTP requests and extending it.
/// </summary>
/// <param name="context">Cake Context the request is geting </param>
/// <param name="settings">HttpSettings to apply to the inner handler</param>
public CakeHttpClientHandler(ICakeContext context, HttpSettings settings)
{
_Context = context ?? throw new ArgumentNullException(nameof(context));
_Settings = settings ?? throw new ArgumentException(nameof(settings));
Expand Down Expand Up @@ -56,7 +56,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

//Logs the Request to the Cake Context Logger
byte[] requestMessage = null;
if (request?.Content != null)
if (request?.Content != null && _Settings.LogRequestResponseOutput)
requestMessage = await request.Content.ReadAsByteArrayAsync();
else
requestMessage = new byte[] { };
Expand All @@ -68,10 +68,10 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

//Logs the Response to the Cake Context Logger
byte[] responseMessage = null;
if (response.IsSuccessStatusCode && response.Content != null)
if (response.IsSuccessStatusCode && response.Content != null && _Settings.LogRequestResponseOutput)
responseMessage = await response.Content.ReadAsByteArrayAsync();

else if (response.Content != null)
else if (response.Content != null && _Settings.LogRequestResponseOutput)
{
var tempMessage = await response.Content.ReadAsStringAsync();
responseMessage = Encoding.UTF8.GetBytes($"{response.ReasonPhrase} ({ (int)response.StatusCode })\r\n{ string.Concat(Enumerable.Repeat("=", 70))}\r\n{ tempMessage ?? string.Empty }");
Expand Down Expand Up @@ -100,7 +100,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
}

/// <summary>
/// Appends headers to HttpRequestMessage before sending the
/// Appends headers to HttpRequestMessage before sending the
/// </summary>
/// <param name="request"></param>
private void AppendHeaders(HttpRequestMessage request)
Expand All @@ -111,17 +111,17 @@ private void AppendHeaders(HttpRequestMessage request)
{
//Append Non-Content Header Request
foreach (var header in _Settings.Headers.Where(kvp => !(kvp.Key.IndexOf("content", StringComparison.OrdinalIgnoreCase) >= 0)))
request.Headers.Add(header.Key, header.Value);
//Append Content Headers to Request Body
if(request.Content != null)
request.Headers.Add(header.Key, header.Value);

//Append Content Headers to Request Body
if (request.Content != null)
{
foreach (var header in _Settings.Headers.Where(kvp => kvp.Key.IndexOf("content", StringComparison.OrdinalIgnoreCase) >= 0))
request.Content.Headers.Add(header.Key, header.Value);
}
}

if(_Settings.Cookies?.Count > 0)
if (_Settings.Cookies?.Count > 0)
request.Headers.Add("Cookie", string.Join<string>(";", _Settings.Cookies.Select(kvp => $"{kvp.Key}={kvp.Value}")));
}

Expand All @@ -147,4 +147,4 @@ private enum HttpEventType
Response
}
}
}
}
Loading

0 comments on commit 6f722ac

Please sign in to comment.