Skip to content

Commit a97d137

Browse files
Christopher Andersonfabiocav
authored andcommitted
Adds rawBody support
1 parent 58b0073 commit a97d137

File tree

7 files changed

+43
-20
lines changed

7 files changed

+43
-20
lines changed

src/WebJobs.Script.Grpc/Proto/FunctionRpc.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,5 @@ message RpcHttp {
252252
string status_code = 12;
253253
map<string,string> query = 15;
254254
bool is_raw = 16;
255+
TypedData rawBody = 17;
255256
}

src/WebJobs.Script/Rpc/MessageExtensions/RpcMessageConversionExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public static TypedData ToRpc(this object value)
7272
};
7373
typedData.Http = http;
7474

75+
http.RawBody = null;
76+
7577
foreach (var pair in request.Query)
7678
{
7779
http.Query.Add(pair.Key, pair.Value.ToString());
@@ -109,6 +111,7 @@ public static TypedData ToRpc(this object value)
109111
var bytes = new byte[length];
110112
request.Body.Read(bytes, 0, length);
111113
body = bytes;
114+
rawBody = Encoding.UTF8.GetString(bytes);
112115
break;
113116

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

121124
http.Body = body.ToRpc();
125+
http.RawBody = rawBody.ToRpc();
122126
}
123127
}
124128
else

src/WebJobs.Script/WebJobs.Script.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageReference Include="Microsoft.Azure.Functions.JavaWorker" Version="1.1.0-beta2-10009">
2323
<PrivateAssets>None</PrivateAssets>
2424
</PackageReference>
25-
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10022">
25+
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10026">
2626
<PrivateAssets>None</PrivateAssets>
2727
</PackageReference>
2828
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta4" />

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ protected async Task<string> CreateTest<Req>(Req content, string contentType, bo
346346
Assert.NotNull(objResult);
347347
Assert.Equal(contentType, objResult.ContentTypes[0]);
348348
Assert.Equal(200, objResult.StatusCode);
349+
if(content is byte[])
350+
{
351+
Assert.Equal(System.Text.Encoding.UTF8.GetString(content as byte[]), objResult.Value);
352+
}
353+
else
354+
{
355+
Assert.Equal(content.ToString(), objResult.Value);
356+
}
349357
return objResult.Value.ToString();
350358
}
351359
}

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Diagnostics;
76
using System.IO;
87
using System.Linq;
9-
using System.Text.RegularExpressions;
108
using System.Threading.Tasks;
11-
using System.Web.Http;
129
using Microsoft.Azure.WebJobs.Host;
1310
using Microsoft.WindowsAzure.Storage.Blob;
1411
using Microsoft.WindowsAzure.Storage.Queue;
@@ -19,6 +16,7 @@
1916
using Microsoft.AspNetCore.Http;
2017
using Microsoft.AspNetCore.Mvc;
2118
using Microsoft.Azure.WebJobs.Script.Binding;
19+
using System.Text;
2220

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

351-
#if HTTP_TESTS
349+
352350
[Fact]
353351
public async Task HttpTrigger_Post_ByteArray()
354352
{
355353
TestHelpers.ClearFunctionLogs("HttpTriggerByteArray");
356354

355+
IHeaderDictionary headers = new HeaderDictionary();
356+
headers.Add("Content-Type", "application/octet-stream");
357+
357358
byte[] inputBytes = new byte[] { 1, 2, 3, 4, 5 };
359+
var content = inputBytes;
358360

359-
HttpRequestMessage request = new HttpRequestMessage
360-
{
361-
RequestUri = new Uri(string.Format("http://localhost/api/httptriggerbytearray")),
362-
Method = HttpMethod.Post,
363-
Content = new ByteArrayContent(inputBytes)
364-
};
365-
request.SetConfiguration(new HttpConfiguration());
366-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
361+
362+
HttpRequest request = HttpTestHelpers.CreateHttpRequest("POST", "http://localhost/api/httptriggerbytearray", headers, content);
367363

368364
Dictionary<string, object> arguments = new Dictionary<string, object>
369365
{
370366
{ "req", request }
371367
};
372368
await Fixture.Host.CallAsync("HttpTriggerByteArray", arguments);
373369

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

377-
JObject testResult = await GetFunctionTestResult("HttpTriggerByteArray");
378-
Assert.True((bool)testResult["isBuffer"]);
379-
Assert.Equal(5, (int)testResult["length"]);
372+
ObjectResult objectResult = result as ObjectResult;
373+
Assert.NotNull(objectResult);
374+
Assert.Equal(200, objectResult.StatusCode);
375+
376+
Newtonsoft.Json.Linq.JObject body = (Newtonsoft.Json.Linq.JObject) objectResult.Value;
377+
Assert.True((bool) body["isBuffer"]);
378+
Assert.Equal(5, body["length"]);
379+
380+
var rawBody = Encoding.UTF8.GetBytes((string) body["rawBody"]);
381+
Assert.Equal(inputBytes, rawBody);
380382
}
381383

384+
#if HTTP_TESTS
382385
[Fact]
383386
public async Task HttpTriggerToBlob()
384387
{

test/WebJobs.Script.Tests.Integration/TestScripts/Node/HttpTriggerByteArray/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
context.res = {
1010
status: 200,
11-
body: "Success!"
11+
body: {
12+
isBuffer: Buffer.isBuffer(body),
13+
length: body.length,
14+
rawBody: context.req.rawBody
15+
}
1216
};
1317

1418
context.done();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<NoWarn>NU1701</NoWarn>
2323
</PackageReference>
2424
<PackageReference Include="Microsoft.Azure.Functions.JavaWorker" Version="1.1.0-beta2-10009" />
25-
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10022" />
25+
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10026" />
2626
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
2727
<PackageReference Include="Moq" Version="4.7.145" />
2828
<PackageReference Include="xunit" Version="2.3.1" />
@@ -55,6 +55,9 @@
5555
</ItemGroup>
5656

5757
<ItemGroup>
58+
<None Update="appsettings.json">
59+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
60+
</None>
5861
<None Update="xunit.runner.json">
5962
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6063
</None>

0 commit comments

Comments
 (0)