Skip to content

Commit 1e78bfc

Browse files
committed
refactoring HeaderMiddleware tests
1 parent 94f8767 commit 1e78bfc

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

src/WebJobs.Script.WebHost/Middleware/CustomHttpHeadersMiddleware.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ public CustomHttpHeadersMiddleware(IOptions<CustomHttpHeadersOptions> hostOption
2121

2222
public async Task Invoke(HttpContext context, RequestDelegate next)
2323
{
24-
await next(context);
25-
26-
foreach (var header in _hostOptions)
24+
context.Response.OnStarting(() =>
2725
{
28-
context.Response.Headers.TryAdd(header.Key, header.Value);
29-
}
26+
foreach (var header in _hostOptions)
27+
{
28+
context.Response.Headers.TryAdd(header.Key, header.Value);
29+
}
30+
31+
return Task.CompletedTask;
32+
});
33+
34+
await next(context);
3035
}
3136
}
32-
}
37+
}

test/WebJobs.Script.Tests/Middleware/CustomHttpHeadersMiddlewareTests.cs

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Net;
8+
using System.Net.Http;
79
using System.Text;
810
using System.Threading.Tasks;
11+
using Microsoft.AspNetCore.Builder;
12+
using Microsoft.AspNetCore.Hosting;
913
using Microsoft.AspNetCore.Http;
14+
using Microsoft.AspNetCore.Http.Features;
15+
using Microsoft.AspNetCore.TestHost;
16+
using Microsoft.Azure.WebJobs.Script.Middleware;
1017
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
1118
using Microsoft.Azure.WebJobs.Script.WebHost.Middleware;
19+
using Microsoft.Extensions.DependencyInjection;
20+
using Microsoft.Extensions.Hosting;
1221
using Microsoft.Extensions.Options;
1322
using Xunit;
1423

1524
namespace Microsoft.Azure.WebJobs.Script.Tests.Middleware
1625
{
1726
public class CustomHttpHeadersMiddlewareTests
1827
{
28+
private bool _nextInvoked = false;
29+
1930
[Fact]
2031
public async Task Invoke_hasCustomHeaders_AddsResponseHeaders()
2132
{
@@ -26,42 +37,52 @@ public async Task Invoke_hasCustomHeaders_AddsResponseHeaders()
2637
};
2738
var headerOptions = new OptionsWrapper<CustomHttpHeadersOptions>(headers);
2839

29-
bool nextInvoked = false;
30-
RequestDelegate next = (context) =>
40+
HttpClient client = GetTestHttpClient(o =>
3141
{
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);
3646

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'");
4450
}
4551

4652
[Fact]
4753
public async Task Invoke_noCustomHeaders_DoesNotAddResponseHeader()
4854
{
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+
}
5859

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();
6083

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();
6586
}
6687
}
6788
}

0 commit comments

Comments
 (0)