Skip to content

Commit 897e095

Browse files
Matt Masonmathewc
authored andcommitted
Handle json arrays for script languages
1 parent b4f6c80 commit 897e095

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

src/WebJobs.Script/Binding/HttpBinding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ internal static HttpResponseMessage CreateResponse(HttpRequestMessage request, o
5858
{
5959
try
6060
{
61-
// attempt to read the content as json
62-
content = JObject.Parse(stringContent);
61+
// attempt to read the content as JObject/JArray
62+
content = JsonConvert.DeserializeObject(stringContent);
6363
}
6464
catch (JsonException)
6565
{

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Collections.Generic;
6+
using System.Net;
7+
using System.Net.Http;
48
using System.Threading.Tasks;
9+
using System.Web.Http;
510
using Microsoft.WindowsAzure.Storage.Blob;
11+
using Newtonsoft.Json;
12+
using Newtonsoft.Json.Linq;
613
using Xunit;
714

815
namespace Microsoft.Azure.WebJobs.Script.Tests
@@ -39,6 +46,35 @@ public async Task QueueTriggerToBlob()
3946
await QueueTriggerToBlobTest();
4047
}
4148

49+
[Fact]
50+
public async Task HttpTrigger_Get_Array()
51+
{
52+
HttpRequestMessage request = new HttpRequestMessage
53+
{
54+
RequestUri = new Uri("http://localhost/api/httptrigger"),
55+
Method = HttpMethod.Get
56+
};
57+
request.SetConfiguration(new HttpConfiguration());
58+
59+
Dictionary<string, object> arguments = new Dictionary<string, object>
60+
{
61+
{ "req", request }
62+
};
63+
await Fixture.Host.CallAsync("HttpTrigger", arguments);
64+
65+
HttpResponseMessage response = (HttpResponseMessage)request.Properties[ScriptConstants.AzureFunctionsHttpResponseKey];
66+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
67+
var contentType = response.Content.Headers.ContentType;
68+
Assert.Equal("application/json", contentType.MediaType);
69+
Assert.Equal("utf-8", contentType.CharSet);
70+
71+
ObjectContent objectContent = response.Content as ObjectContent;
72+
Assert.NotNull(objectContent);
73+
Assert.Equal(typeof(JArray), objectContent.ObjectType);
74+
JArray content = await response.Content.ReadAsAsync<JArray>();
75+
Assert.Equal("[{\"a\":\"b\"}]", content.ToString(Formatting.None));
76+
}
77+
4278
public class TestFixture : EndToEndTestFixture
4379
{
4480
public TestFixture() : base(@"TestScripts\Php", "php")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "httpTrigger",
5+
"direction": "in",
6+
"name": "req",
7+
"methods": [ "get" ]
8+
},
9+
{
10+
"type": "http",
11+
"name": "res",
12+
"direction": "out"
13+
}
14+
]
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
$json = '[{"a":"b"}]';
3+
$res = getenv('res');
4+
file_put_contents($res, $json);
5+
?>

test/WebJobs.Script.Tests.Integration/WebJobs.Script.Tests.Integration.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,12 @@
945945
<None Include="TestScripts\Php\host.json">
946946
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
947947
</None>
948+
<None Include="TestScripts\Php\HttpTrigger\function.json">
949+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
950+
</None>
951+
<Content Include="TestScripts\Php\HttpTrigger\run.php">
952+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
953+
</Content>
948954
<None Include="TestScripts\Php\ManualTrigger\function.json">
949955
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
950956
</None>

0 commit comments

Comments
 (0)