-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make GetHeaderValue support HttpContext unavailable (#16654)
* Make GetHeaderValue tolerant for when the http context is not available. Now it just returns null. * Add unit tests
- Loading branch information
Showing
2 changed files
with
49 additions
and
7 deletions.
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
tests/Umbraco.Tests.UnitTests/Umbraco.Cms.Api.Delivery/Services/RequestHeaderHandlerTests.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,48 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Moq; | ||
using NUnit.Framework; | ||
using Umbraco.Cms.Api.Delivery.Services; | ||
|
||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Cms.Api.Delivery.Services; | ||
|
||
[TestFixture] | ||
public class RequestHeaderHandlerTests | ||
{ | ||
private const string HeaderName = "TestHeader"; | ||
[Test] | ||
public void GetHeaderValue_return_null_when_http_context_is_unavailable() | ||
{ | ||
IHttpContextAccessor httpContextAccessor = Mock.Of<IHttpContextAccessor>(); | ||
|
||
var sut = new TestRequestHeaderHandler(httpContextAccessor); | ||
|
||
Assert.IsNull(sut.TestGetHeaderValue(HeaderName)); | ||
} | ||
|
||
[Test] | ||
public void GetHeaderValue_return_header_value_when_http_context_is_available() | ||
{ | ||
|
||
const string headerValue = "TestValue"; | ||
|
||
HttpContext httpContext = new DefaultHttpContext(); | ||
httpContext.Request.Headers[HeaderName] = headerValue; | ||
|
||
IHttpContextAccessor httpContextAccessor = Mock.Of<IHttpContextAccessor>(); | ||
Mock.Get(httpContextAccessor).Setup(x => x.HttpContext).Returns(httpContext); | ||
|
||
var sut = new TestRequestHeaderHandler(httpContextAccessor); | ||
|
||
Assert.AreEqual(headerValue, sut.TestGetHeaderValue(HeaderName)); | ||
} | ||
} | ||
|
||
|
||
internal class TestRequestHeaderHandler : RequestHeaderHandler | ||
{ | ||
public TestRequestHeaderHandler(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor) | ||
{ | ||
} | ||
|
||
public string? TestGetHeaderValue(string headerName) => base.GetHeaderValue(headerName); | ||
} |