Skip to content

Commit

Permalink
Adds rawBody support
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Anderson authored and fabiocav committed Nov 29, 2017
1 parent 58b0073 commit a97d137
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 20 deletions.
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;
}
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-beta4" />
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" />
<PackageReference Include="Moq" Version="4.7.145" />
<PackageReference Include="xunit" Version="2.3.1" />
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

0 comments on commit a97d137

Please sign in to comment.