Skip to content

Commit b2f9f22

Browse files
author
Christopher Anderson
committed
Fix byte array logic, content-type, and add tests
1 parent 4fe222b commit b2f9f22

File tree

4 files changed

+114
-8
lines changed

4 files changed

+114
-8
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Dynamic;
7-
using System.IO;
87
using System.Linq;
98
using System.Threading.Tasks;
109
using Microsoft.AspNetCore.Http;
1110
using Microsoft.AspNetCore.Mvc;
1211
using Microsoft.AspNetCore.Mvc.Formatters;
1312
using Microsoft.AspNetCore.WebUtilities;
1413
using Microsoft.Azure.WebJobs.Script.WebHost.Formatters;
15-
using Microsoft.Net.Http.Headers;
1614
using Newtonsoft.Json;
1715

1816
namespace Microsoft.Azure.WebJobs.Script.Binding
@@ -80,7 +78,7 @@ private async Task WriteResponseBodyAsync(HttpResponse response, object content)
8078

8179
IOutputFormatter selectedFormatter = SelectedFormatter(formatterContext);
8280

83-
if (selectedFormatter != null)
81+
if (selectedFormatter == null)
8482
{
8583
// We were unable to locate a formatter with the provided type, to maintain the original behavior
8684
// we'll convert the object to string and get a formatter
@@ -90,6 +88,8 @@ private async Task WriteResponseBodyAsync(HttpResponse response, object content)
9088
selectedFormatter = SelectedFormatter(formatterContext);
9189
}
9290

91+
formatterContext.ContentType = response.ContentType;
92+
9393
await selectedFormatter.WriteAsync(formatterContext);
9494
}
9595

@@ -101,8 +101,6 @@ private static OutputFormatterWriteContext CreateFormatterContext(HttpResponse r
101101
content?.GetType(),
102102
content);
103103

104-
context.ContentType = response.ContentType;
105-
106104
return context;
107105
}
108106

test/WebJobs.Script.Tests.Shared/HttpTestHelpers.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
76
using System.Text;
87
using Microsoft.AspNetCore.Http;
9-
using Microsoft.AspNetCore.Http.Extensions;
108
using Microsoft.AspNetCore.Http.Features;
11-
using Moq;
9+
using System.Threading.Tasks;
1210

1311
namespace Microsoft.WebJobs.Script.Tests
1412
{

test/WebJobs.Script.Tests.Shared/TestHelpers.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,15 @@ private static async Task<string[]> ReadAllLinesSafeAsync(string logFile)
234234

235235
return logs;
236236
}
237+
238+
239+
public static async Task<string> ReadStreamToEnd(Stream stream)
240+
{
241+
stream.Position = 0;
242+
using (var sr = new StreamReader(stream))
243+
{
244+
return await sr.ReadToEndAsync();
245+
}
246+
}
237247
}
238248
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.Azure.WebJobs.Script.Binding;
11+
using Microsoft.Extensions.Primitives;
12+
using Microsoft.WebJobs.Script.Tests;
13+
using Xunit;
14+
15+
namespace Microsoft.Azure.WebJobs.Script.Tests.Binding.ActionResults
16+
{
17+
public class RawScriptResultTests
18+
{
19+
[Fact]
20+
public void HasExpectedProperties_WithStatusCode()
21+
{
22+
var obj = "hello world";
23+
var result = new RawScriptResult(202, obj) { Headers = new Dictionary<string, object>() };
24+
25+
Assert.Equal(202, result.StatusCode);
26+
Assert.Equal(obj, result.Content);
27+
Assert.Empty(result.Headers);
28+
}
29+
30+
[Fact]
31+
public void HasExpectedProperties_WithoutStatusCode()
32+
{
33+
var obj = "hello world";
34+
var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() };
35+
36+
Assert.Null(result.StatusCode);
37+
Assert.Equal(obj, result.Content);
38+
Assert.Empty(result.Headers);
39+
}
40+
41+
[Fact]
42+
public async Task HandlesStringContent()
43+
{
44+
var obj = "{ \"a\": 1 }";
45+
var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() };
46+
var context = new ActionContext() { HttpContext = new DefaultHttpContext() };
47+
context.HttpContext.Response.Body = new MemoryStream();
48+
await result.ExecuteResultAsync(context);
49+
var body = await TestHelpers.ReadStreamToEnd(context.HttpContext.Response.Body);
50+
Assert.Equal(obj, body);
51+
Assert.Equal(200, context.HttpContext.Response.StatusCode);
52+
}
53+
54+
[Fact]
55+
public async Task HandlesStringContent_WithHeader()
56+
{
57+
var obj = "{ \"a\": 1 }";
58+
var contentHeader = "application/json";
59+
var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() };
60+
var context = new ActionContext() { HttpContext = new DefaultHttpContext() };
61+
context.HttpContext.Response.Headers.Add("Content-Type", contentHeader);
62+
context.HttpContext.Response.Body = new MemoryStream();
63+
await result.ExecuteResultAsync(context);
64+
var body = await TestHelpers.ReadStreamToEnd(context.HttpContext.Response.Body);
65+
StringValues value;
66+
Assert.True(context.HttpContext.Response.Headers.TryGetValue("Content-Type", out value));
67+
Assert.Equal("application/json; charset=utf-8", value);
68+
Assert.Equal(obj, body);
69+
Assert.Equal(200, context.HttpContext.Response.StatusCode);
70+
}
71+
72+
[Fact]
73+
public async Task HandlesByteContent()
74+
{
75+
var content = "{ \"a\": 1 }";
76+
var obj = Encoding.UTF8.GetBytes(content);
77+
var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() };
78+
var context = new ActionContext() { HttpContext = new DefaultHttpContext() };
79+
context.HttpContext.Response.Body = new MemoryStream();
80+
await result.ExecuteResultAsync(context);
81+
var body = await TestHelpers.ReadStreamToEnd(context.HttpContext.Response.Body);
82+
Assert.Equal(content, body);
83+
Assert.Equal(200, context.HttpContext.Response.StatusCode);
84+
}
85+
86+
[Fact]
87+
public async Task HandlesStreamContent()
88+
{
89+
var content = "{ \"a\": 1 }";
90+
var obj = new MemoryStream(Encoding.UTF8.GetBytes(content));
91+
var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() };
92+
var context = new ActionContext() { HttpContext = new DefaultHttpContext() };
93+
context.HttpContext.Response.Body = new MemoryStream();
94+
await result.ExecuteResultAsync(context);
95+
var body = await TestHelpers.ReadStreamToEnd(context.HttpContext.Response.Body);
96+
Assert.Equal(content, body);
97+
Assert.Equal(200, context.HttpContext.Response.StatusCode);
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)