Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ public static IDictionary<string, string> GetQueryParameterDictionary(this HttpR

public static IDictionary<string, string> GetRawHeaders(this HttpRequestMessage request)
{
Dictionary<string, string> headers = new Dictionary<string, string>();

var allHeadersRaw = request.Headers.ToString();
var headers = new Dictionary<string, string>();
Copy link
Member

Choose a reason for hiding this comment

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

We have logic here https://github.com/Azure/azure-webjobs-sdk-script/blob/658d4d0c9dce9af940bd1a4fd8d5fb0f2fb85e90/src/WebJobs.Script/Binding/HttpBinding.cs#L290 that goes the other way - takes top level headers and applies them to a response. So what you're doing makes this symmetric for request content.

var allHeadersRaw = request.Headers.ToString() + Environment.NewLine + request.Content?.Headers?.ToString();
var rawHeaderLines = allHeadersRaw.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

foreach (var header in rawHeaderLines)
Expand All @@ -82,7 +81,7 @@ public static IDictionary<string, string> GetRawHeaders(this HttpRequestMessage
string value = header.Substring(idx + 1).Trim();
headers.Add(name, value);
}

return headers;
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/WebJobs.Script.Tests.Integration/NodeEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ public async Task HttpTrigger_Get(string functionName)
request.Headers.Add("test-header", "Test Request Header");
string userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36";
request.Headers.Add("user-agent", userAgent);
string accept = "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8";
request.Headers.Add("accept", accept);
string customHeader = "foo,bar,baz";
request.Headers.Add("custom-1", customHeader);

Expand Down Expand Up @@ -412,6 +414,7 @@ public async Task HttpTrigger_Get(string functionName)
JObject reqHeaders = (JObject)resultObject["reqHeaders"];
Assert.Equal("Test Request Header", reqHeaders["test-header"]);
Assert.Equal(userAgent, reqHeaders["user-agent"]);
Assert.Equal(accept, reqHeaders["accept"]);
Assert.Equal(customHeader, reqHeaders["custom-1"]);
}

Expand Down Expand Up @@ -736,6 +739,7 @@ public async Task HttpTrigger_Post_PlainText()
Content = new StringContent(testData)
};
request.SetConfiguration(new HttpConfiguration());
request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
Copy link
Member

Choose a reason for hiding this comment

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

Can you add like 2 more content headers and verify them below to test the multiplicity? I.e. symmetric with https://github.com/Azure/azure-webjobs-sdk-script/blob/658d4d0c9dce9af940bd1a4fd8d5fb0f2fb85e90/src/WebJobs.Script/Binding/HttpBinding.cs#L290


Dictionary<string, object> arguments = new Dictionary<string, object>
{
Expand All @@ -752,6 +756,7 @@ public async Task HttpTrigger_Post_PlainText()
Assert.Equal(testData, (string)resultObject["reqBody"]);
Assert.Equal("string", (string)resultObject["reqRawBodyType"]);
Assert.Equal(testData, (string)resultObject["reqRawBody"]);
Assert.Equal("text/plain", resultObject["reqHeaders"]["content-type"]);
Copy link
Member

Choose a reason for hiding this comment

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

Do we have other tests passing multi-value headers, headers with quality, etc.? Would be good to make sure those are handled as expected.

Copy link
Contributor Author

@mamaso mamaso Mar 23, 2017

Choose a reason for hiding this comment

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

Another great comment, all of our multiple-value tests use User-Agent, which as mentioned in the other comment is its own special snowflake as it uses " " separator. I'll add some tests with the accept header which uses standard comma separators.

}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public async Task HttpTrigger_Post_PlainText()
string result = await response.Content.ReadAsStringAsync();
JObject resultObject = JObject.Parse(result);
Assert.Equal(testData, (string)resultObject["result"]["message"]["value"]);
Assert.Equal("text/plain; charset=utf-8", (string)resultObject["result"]["content-type"]);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ else
$body = (New-Object PSObject |
Add-Member -PassThru NoteProperty message $message |
Add-Member -PassThru NoteProperty {user-agent} ${req_headers_user-agent} |
Add-Member -PassThru NoteProperty {custom-1} ${req_headers_custom-1}
Add-Member -PassThru NoteProperty {custom-1} ${req_headers_custom-1} |
Add-Member -PassThru NoteProperty {content-type} ${req_headers_content-type}
)

$responseContent = @{result=$body; headers=@{"TEST-HEADER"="Test Response Header"}} | ConvertTo-Json -Compress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Xunit;

namespace Microsoft.Azure.WebJobs.Script.Tests
Expand Down Expand Up @@ -79,17 +80,34 @@ public void GetRawHeaders_ReturnsExpectedHeaders()

// Multiple headers
string userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36";
string accept = "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8";
string testHeader2 = "foo,bar,baz";
string testHeader3 = "foo bar baz";
request.Headers.Add("User-Agent", userAgent);
request.Headers.Add("Accept", accept);
request.Headers.Add("Header2", testHeader2);
request.Headers.Add("Header3", testHeader3);
request.Headers.Add("Empty", string.Empty);
var str = request.Headers.ToString();
headers = request.GetRawHeaders();
Assert.Equal(4, headers.Count);
Assert.Equal(6, headers.Count);
Assert.Equal(userAgent, headers["User-Agent"]);
Assert.Equal(accept, headers["Accept"]);
Assert.Equal(testHeader1, headers["Header1"]);
Assert.Equal(testHeader2, headers["Header2"]);
Assert.Equal(testHeader3, headers["Header3"]);
Assert.Equal(string.Empty, headers["Empty"]);

// Content headers
request.Content = new StringContent("test");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
request.Content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse("form-data; name=\"fieldName\"; filename=\"filename.jpg\"");
request.Content.Headers.ContentRange = ContentRangeHeaderValue.Parse("bytes 200-1000/67589");
headers = request.GetRawHeaders();
Assert.Equal(9, headers.Count);
Assert.Equal("text/html", headers["Content-Type"]);
Assert.Equal("form-data; name=\"fieldName\"; filename=\"filename.jpg\"", headers["Content-Disposition"]);
Assert.Equal("bytes 200-1000/67589", headers["Content-Range"]);
}
}
}