Skip to content

Commit 0900746

Browse files
author
Christopher Anderson
committed
Set content-type properly and don't send body until after we send status code
1 parent d6aa46a commit 0900746

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/WebJobs.Script/Binding/Http/RawScriptResult.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.Mvc.Formatters;
1313
using Microsoft.AspNetCore.WebUtilities;
1414
using Microsoft.Azure.WebJobs.Script.WebHost.Formatters;
15+
using Microsoft.Net.Http.Headers;
1516
using Newtonsoft.Json;
1617

1718
namespace Microsoft.Azure.WebJobs.Script.Binding
@@ -45,16 +46,27 @@ public async Task ExecuteResultAsync(ActionContext context)
4546
{
4647
foreach (var header in Headers)
4748
{
48-
response.Headers.Add(header.Key, header.Value?.ToString() ?? string.Empty);
49+
if (header.Key.Equals("content-type", StringComparison.OrdinalIgnoreCase))
50+
{
51+
if (header.Value == null)
52+
{
53+
throw new InvalidOperationException("content-type header cannot be null");
54+
}
55+
response.ContentType = header.Value.ToString();
56+
}
57+
else
58+
{
59+
response.Headers.Add(header.Key, header.Value?.ToString() ?? string.Empty);
60+
}
4961
}
5062
}
5163

52-
await WriteResponseBodyAsync(response, Content);
53-
5464
if (StatusCode != null)
5565
{
5666
response.StatusCode = StatusCode.Value;
5767
}
68+
69+
await WriteResponseBodyAsync(response, Content);
5870
}
5971

6072
private async Task WriteResponseBodyAsync(HttpResponse response, object content)
@@ -83,11 +95,15 @@ private async Task WriteResponseBodyAsync(HttpResponse response, object content)
8395

8496
private static OutputFormatterWriteContext CreateFormatterContext(HttpResponse response, object content)
8597
{
86-
return new OutputFormatterWriteContext(
98+
var context = new OutputFormatterWriteContext(
8799
response.HttpContext,
88100
(s, e) => new HttpResponseStreamWriter(s, e),
89101
content?.GetType(),
90102
content);
103+
104+
context.ContentType = response.ContentType;
105+
106+
return context;
91107
}
92108

93109
private static IOutputFormatter SelectedFormatter(OutputFormatterWriteContext formatterContext)

test/WebJobs.Script.Tests.Integration/NodeContentTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Web.Http.Results;
2323
using Moq;
2424
using Microsoft.Azure.WebJobs.Script.Binding;
25+
using Microsoft.Azure.WebJobs.Host;
2526

2627
namespace Microsoft.Azure.WebJobs.Script.Tests
2728
{
@@ -58,6 +59,15 @@ public async Task StringTextPlainRaw()
5859
Assert.Equal(str, content);
5960
}
6061

62+
[Fact]
63+
public async Task BadContentType_ThrowsExpectedException()
64+
{
65+
await Assert.ThrowsAsync<FunctionInvocationException>(async () =>
66+
{
67+
var content = await Response("asdf", null);
68+
});
69+
}
70+
6171
[Fact]
6272
public async Task ByteArrayTextPlainResponse()
6373
{

0 commit comments

Comments
 (0)