File tree Expand file tree Collapse file tree 2 files changed +49
-7
lines changed
src/Umbraco.Cms.Api.Delivery/Services
tests/Umbraco.Tests.UnitTests/Umbraco.Cms.Api.Delivery/Services Expand file tree Collapse file tree 2 files changed +49
-7
lines changed Original file line number Diff line number Diff line change @@ -8,11 +8,5 @@ internal abstract class RequestHeaderHandler
88
99 protected RequestHeaderHandler ( IHttpContextAccessor httpContextAccessor ) => _httpContextAccessor = httpContextAccessor ;
1010
11- protected string ? GetHeaderValue ( string headerName )
12- {
13- HttpContext httpContext = _httpContextAccessor . HttpContext ??
14- throw new InvalidOperationException ( "Could not obtain an HTTP context" ) ;
15-
16- return httpContext . Request . Headers [ headerName ] ;
17- }
11+ protected string ? GetHeaderValue ( string headerName ) => _httpContextAccessor . HttpContext ? . Request . Headers [ headerName ] ;
1812}
Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Http ;
2+ using Moq ;
3+ using NUnit . Framework ;
4+ using Umbraco . Cms . Api . Delivery . Services ;
5+
6+ namespace Umbraco . Cms . Tests . UnitTests . Umbraco . Cms . Api . Delivery . Services ;
7+
8+ [ TestFixture ]
9+ public class RequestHeaderHandlerTests
10+ {
11+ private const string HeaderName = "TestHeader" ;
12+ [ Test ]
13+ public void GetHeaderValue_return_null_when_http_context_is_unavailable ( )
14+ {
15+ IHttpContextAccessor httpContextAccessor = Mock . Of < IHttpContextAccessor > ( ) ;
16+
17+ var sut = new TestRequestHeaderHandler ( httpContextAccessor ) ;
18+
19+ Assert . IsNull ( sut . TestGetHeaderValue ( HeaderName ) ) ;
20+ }
21+
22+ [ Test ]
23+ public void GetHeaderValue_return_header_value_when_http_context_is_available ( )
24+ {
25+
26+ const string headerValue = "TestValue" ;
27+
28+ HttpContext httpContext = new DefaultHttpContext ( ) ;
29+ httpContext . Request . Headers [ HeaderName ] = headerValue ;
30+
31+ IHttpContextAccessor httpContextAccessor = Mock . Of < IHttpContextAccessor > ( ) ;
32+ Mock . Get ( httpContextAccessor ) . Setup ( x => x . HttpContext ) . Returns ( httpContext ) ;
33+
34+ var sut = new TestRequestHeaderHandler ( httpContextAccessor ) ;
35+
36+ Assert . AreEqual ( headerValue , sut . TestGetHeaderValue ( HeaderName ) ) ;
37+ }
38+ }
39+
40+
41+ internal class TestRequestHeaderHandler : RequestHeaderHandler
42+ {
43+ public TestRequestHeaderHandler ( IHttpContextAccessor httpContextAccessor ) : base ( httpContextAccessor )
44+ {
45+ }
46+
47+ public string ? TestGetHeaderValue ( string headerName ) => base . GetHeaderValue ( headerName ) ;
48+ }
You can’t perform that action at this time.
0 commit comments