3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Linq ;
6
7
using System . Net ;
8
+ using System . Net . Http ;
7
9
using System . Text ;
8
10
using System . Threading . Tasks ;
11
+ using Microsoft . AspNetCore . Builder ;
12
+ using Microsoft . AspNetCore . Hosting ;
9
13
using Microsoft . AspNetCore . Http ;
14
+ using Microsoft . AspNetCore . Http . Features ;
15
+ using Microsoft . AspNetCore . TestHost ;
16
+ using Microsoft . Azure . WebJobs . Script . Middleware ;
10
17
using Microsoft . Azure . WebJobs . Script . WebHost . Configuration ;
11
18
using Microsoft . Azure . WebJobs . Script . WebHost . Middleware ;
19
+ using Microsoft . Extensions . DependencyInjection ;
20
+ using Microsoft . Extensions . Hosting ;
12
21
using Microsoft . Extensions . Options ;
13
22
using Xunit ;
14
23
15
24
namespace Microsoft . Azure . WebJobs . Script . Tests . Middleware
16
25
{
17
26
public class CustomHttpHeadersMiddlewareTests
18
27
{
28
+ private bool _nextInvoked = false ;
29
+
19
30
[ Fact ]
20
31
public async Task Invoke_hasCustomHeaders_AddsResponseHeaders ( )
21
32
{
@@ -26,42 +37,52 @@ public async Task Invoke_hasCustomHeaders_AddsResponseHeaders()
26
37
} ;
27
38
var headerOptions = new OptionsWrapper < CustomHttpHeadersOptions > ( headers ) ;
28
39
29
- bool nextInvoked = false ;
30
- RequestDelegate next = ( context ) =>
40
+ HttpClient client = GetTestHttpClient ( o =>
31
41
{
32
- nextInvoked = true ;
33
- context . Response . StatusCode = ( int ) HttpStatusCode . Accepted ;
34
- return Task . CompletedTask ;
35
- } ;
42
+ o . Add ( "X-Content-Type-Options" , "nosniff" ) ;
43
+ o . Add ( "Feature-Policy" , "camera 'none'; geolocation 'none'" ) ;
44
+ } ) ;
45
+ HttpResponseMessage response = await client . GetAsync ( string . Empty ) ;
36
46
37
- var middleware = new CustomHttpHeadersMiddleware ( headerOptions ) ;
38
-
39
- var httpContext = new DefaultHttpContext ( ) ;
40
- await middleware . Invoke ( httpContext , next ) ;
41
- Assert . True ( nextInvoked ) ;
42
- Assert . Equal ( httpContext . Response . Headers [ "X-Content-Type-Options" ] . ToString ( ) , "nosniff" ) ;
43
- Assert . Equal ( httpContext . Response . Headers [ "Feature-Policy" ] . ToString ( ) , "camera 'none'; geolocation 'none'" ) ;
47
+ Assert . True ( _nextInvoked ) ;
48
+ Assert . Equal ( response . Headers . GetValues ( "X-Content-Type-Options" ) . Single ( ) , "nosniff" ) ;
49
+ Assert . Equal ( response . Headers . GetValues ( "Feature-Policy" ) . Single ( ) , "camera 'none'; geolocation 'none'" ) ;
44
50
}
45
51
46
52
[ Fact ]
47
53
public async Task Invoke_noCustomHeaders_DoesNotAddResponseHeader ( )
48
54
{
49
- var headerOptions = new OptionsWrapper < CustomHttpHeadersOptions > ( new CustomHttpHeadersOptions ( ) ) ;
50
-
51
- bool nextInvoked = false ;
52
- RequestDelegate next = ( context ) =>
53
- {
54
- nextInvoked = true ;
55
- context . Response . StatusCode = ( int ) HttpStatusCode . Accepted ;
56
- return Task . CompletedTask ;
57
- } ;
55
+ HttpResponseMessage response = await GetTestHttpClient ( ) . GetAsync ( string . Empty ) ;
56
+ Assert . True ( _nextInvoked ) ;
57
+ Assert . Equal ( response . Headers . Count ( ) , 0 ) ;
58
+ }
58
59
59
- var middleware = new CustomHttpHeadersMiddleware ( headerOptions ) ;
60
+ private HttpClient GetTestHttpClient ( Action < CustomHttpHeadersOptions > configureOptions = null )
61
+ {
62
+ // The custom middleware relies on the host starting the request (thus invoking OnStarting),
63
+ // so we need to create a test host to flow through the entire pipeline.
64
+ var host = new WebHostBuilder ( )
65
+ . UseTestServer ( )
66
+ . ConfigureServices ( s =>
67
+ {
68
+ s . AddSingleton < IJobHostMiddlewarePipeline , DefaultMiddlewarePipeline > ( ) ;
69
+ s . AddSingleton < IJobHostHttpMiddleware , CustomHttpHeadersMiddleware > ( ) ;
70
+ s . AddOptions < CustomHttpHeadersOptions > ( ) . Configure ( o => configureOptions ? . Invoke ( o ) ) ;
71
+ } )
72
+ . Configure ( app =>
73
+ {
74
+ app . UseMiddleware < JobHostPipelineMiddleware > ( ) ;
75
+ app . Use ( ( context , next ) =>
76
+ {
77
+ _nextInvoked = true ;
78
+ context . Response . StatusCode = ( int ) HttpStatusCode . Accepted ;
79
+ return Task . CompletedTask ;
80
+ } ) ;
81
+ } )
82
+ . Build ( ) ;
60
83
61
- var httpContext = new DefaultHttpContext ( ) ;
62
- await middleware . Invoke ( httpContext , next ) ;
63
- Assert . True ( nextInvoked ) ;
64
- Assert . Equal ( httpContext . Response . Headers . Count , 0 ) ;
84
+ host . Start ( ) ;
85
+ return host . GetTestClient ( ) ;
65
86
}
66
87
}
67
88
}
0 commit comments