Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds rawBody support to Node.js #2064

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/WebJobs.Script.Grpc/Proto/FunctionRpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,5 @@ message RpcHttp {
string status_code = 12;
map<string,string> query = 15;
bool is_raw = 16;
TypedData rawBody = 17;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this will always be a string, so you can concretely type it as a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it's not always a string, sometimes it's None. I could have created a new type which only has those types, but I opted to reuse the existing TypedData object since it wasn't very opinionated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct - in those cases, would not setting rawBody be sufficient? Protobuf doesn't require all properties to be set in a message, so I think that all client libs will handle the None case. I'd suggest that this change is scoped only to node, which definitely handles nullables. To enable this behavior for node & other workers in the future we could pass the body bytes directly to the client, where they can determine rawbody

That said, I'm on vacation and TypedData works. Just my 2¢ :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I don't set it, it's null. null !== undefined which is a breaking behavior. protobuf for Node.js doesn't seem to have an option for "null" - looks like a fun thread: protocolbuffers/protobuf#1606

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static TypedData ToRpc(this object value)
};
typedData.Http = http;

http.RawBody = null;

foreach (var pair in request.Query)
{
http.Query.Add(pair.Key, pair.Value.ToString());
Expand Down Expand Up @@ -109,6 +111,7 @@ public static TypedData ToRpc(this object value)
var bytes = new byte[length];
request.Body.Read(bytes, 0, length);
body = bytes;
rawBody = Encoding.UTF8.GetString(bytes);
break;

default:
Expand All @@ -119,6 +122,7 @@ public static TypedData ToRpc(this object value)
request.Body.Position = 0;

http.Body = body.ToRpc();
http.RawBody = rawBody.ToRpc();
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/WebJobs.Script/WebJobs.Script.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageReference Include="Microsoft.Azure.Functions.JavaWorker" Version="1.1.0-beta2-10009">
<PrivateAssets>None</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10022">
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10026">
<PrivateAssets>None</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3" />
Expand Down
8 changes: 8 additions & 0 deletions test/WebJobs.Script.Tests.Integration/NodeContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ protected async Task<string> CreateTest<Req>(Req content, string contentType, bo
Assert.NotNull(objResult);
Assert.Equal(contentType, objResult.ContentTypes[0]);
Assert.Equal(200, objResult.StatusCode);
if(content is byte[])
{
Assert.Equal(System.Text.Encoding.UTF8.GetString(content as byte[]), objResult.Value);
}
else
{
Assert.Equal(content.ToString(), objResult.Value);
}
return objResult.Value.ToString();
}
}
Expand Down
37 changes: 20 additions & 17 deletions test/WebJobs.Script.Tests.Integration/NodeEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Queue;
Expand All @@ -19,6 +16,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Script.Binding;
using System.Text;

namespace Microsoft.Azure.WebJobs.Script.Tests
{
Expand Down Expand Up @@ -348,37 +346,42 @@ public async Task HttpTrigger_Get(string functionName)
Assert.Equal(customHeader, reqHeaders["custom-1"]);
}

#if HTTP_TESTS

[Fact]
public async Task HttpTrigger_Post_ByteArray()
{
TestHelpers.ClearFunctionLogs("HttpTriggerByteArray");

IHeaderDictionary headers = new HeaderDictionary();
headers.Add("Content-Type", "application/octet-stream");

byte[] inputBytes = new byte[] { 1, 2, 3, 4, 5 };
var content = inputBytes;

HttpRequestMessage request = new HttpRequestMessage
{
RequestUri = new Uri(string.Format("http://localhost/api/httptriggerbytearray")),
Method = HttpMethod.Post,
Content = new ByteArrayContent(inputBytes)
};
request.SetConfiguration(new HttpConfiguration());
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

HttpRequest request = HttpTestHelpers.CreateHttpRequest("POST", "http://localhost/api/httptriggerbytearray", headers, content);

Dictionary<string, object> arguments = new Dictionary<string, object>
{
{ "req", request }
};
await Fixture.Host.CallAsync("HttpTriggerByteArray", arguments);

HttpResponseMessage response = (HttpResponseMessage)request.Properties[ScriptConstants.AzureFunctionsHttpResponseKey];
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var result = (IActionResult)request.HttpContext.Items[ScriptConstants.AzureFunctionsHttpResponseKey];

JObject testResult = await GetFunctionTestResult("HttpTriggerByteArray");
Assert.True((bool)testResult["isBuffer"]);
Assert.Equal(5, (int)testResult["length"]);
ObjectResult objectResult = result as ObjectResult;
Assert.NotNull(objectResult);
Assert.Equal(200, objectResult.StatusCode);

Newtonsoft.Json.Linq.JObject body = (Newtonsoft.Json.Linq.JObject) objectResult.Value;
Assert.True((bool) body["isBuffer"]);
Assert.Equal(5, body["length"]);

var rawBody = Encoding.UTF8.GetBytes((string) body["rawBody"]);
Assert.Equal(inputBytes, rawBody);
}

#if HTTP_TESTS
[Fact]
public async Task HttpTriggerToBlob()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

context.res = {
status: 200,
body: "Success!"
body: {
isBuffer: Buffer.isBuffer(body),
length: body.length,
rawBody: context.req.rawBody
}
};

context.done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="Microsoft.Azure.Functions.JavaWorker" Version="1.1.0-beta2-10009" />
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10022" />
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10026" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170923-02" />
<PackageReference Include="Moq" Version="4.7.99" />
<PackageReference Include="xunit" Version="2.3.0" />
Expand Down Expand Up @@ -55,6 +55,9 @@
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down