Skip to content

Add support for Content Headers to BasicHtmlWebResponseObject and HtmlWebResponseObject - #4494

Merged
Travis Plunk (TravisEz13) merged 4 commits into
PowerShell:masterfrom
markekraus:GetRawContentHeader
Aug 21, 2017
Merged

Add support for Content Headers to BasicHtmlWebResponseObject and HtmlWebResponseObject#4494
Travis Plunk (TravisEz13) merged 4 commits into
PowerShell:masterfrom
markekraus:GetRawContentHeader

Conversation

@markekraus

@markekraus Mark Kraus (markekraus) commented Aug 4, 2017

Copy link
Copy Markdown
Contributor

Fixes #4467

System.Net.Http.HttpResponseMessage has split the Content headers from the Response headers. This split appears to be intentional and permanent. This split results in the Content-Type and Content-Length headers to be missing from the WebResponseObject.Headers dictionary and from the RawContent property of BasicHtmlWebResponseObject and HtmlWebResponseObject.

This adds the Content headers back to those locations to make it similar to 5.1.

This also adds support to RawContent for multiple response headers with the same field name. System.Net.Http.Headers.HttpContentHeaders and System.Net.Http.Headers.HttpResponseHeaders implement values as arrays.

@msftclas

Mark Kraus (@markekraus),
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by Microsoft. We will now review your pull request.
Thanks,
Microsoft Pull Request Bot

@iSazonov

Copy link
Copy Markdown
Collaborator

Mark Kraus (@markekraus) Could you please add tests?

@markekraus

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Tests Added. I wasn't exactly sure what tests to add. There is already significant validation being done on the Headers and RawContent properties. If I need more than these 2 tests please let me know.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Typo Wen

@markekraus

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) doh... corrected.

@TravisEz13

Copy link
Copy Markdown
Member

I agree with Ilya (@iSazonov) . The tests in the issue look like a good starting point.

@markekraus

Copy link
Copy Markdown
Contributor Author

Travis Plunk (@TravisEz13) Thanks. I added the 2 test that make sense. The other tests from the issue won't ever work unless CoreFX drastically changes their approach in System.Net.Http.HttpResponseMessage.

@markekraus

Copy link
Copy Markdown
Contributor Author

Travis Plunk (@TravisEz13) Steve Lee (@SteveL-MSFT) Rebased PR on master to add test fixes from #4512. The new tests are passing. Let me know if I need to make any other changes.

};

foreach (var entry in response.Headers)
foreach(var headerCollection in headerCollections)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor point, insert a space after 'foreach' here and 'if' below to match the rest of the code.

@markekraus Mark Kraus (markekraus) Aug 17, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

{
headers[entry.Key] = entry.Value;
}
if(BaseResponse.Content != null){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor note: place '{' on a new line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dan Travison (@dantraMSFT) corrected, along with space between if and (.

#endregion charset encoding tests

It "Verifies Invoke-WebRequest includes Content headers in Headers property" {
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/get'"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use HTTPListener here instead of http://httpbin.org/get.
The tests under Context "BasicHtmlWebResponseObject Encoding tests" illustrate using 'test=response&output=$otuput' to pass HTML you want to get in the response. You should consider extending httplistener's 'response' option to allow additional options, such as specifying a content-type header value or other headers to support your testing. let me know if you need any help with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dan Travison (@dantraMSFT) Thanks. I will work on this and submit more and better tests with your suggestions.


#endregion charset encoding tests

It "Verifies Invoke-WebRequest includes Content headers in Headers property" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think these two tests need to be either be extended or additional tests added.
1: I don't believe verifying the presence of the header is sufficient; I believe the test should verify the value(s) as well.
2: You added logic to support headers with multiple values so you need to test that case.

@markekraus

Copy link
Copy Markdown
Contributor Author

Dan Travison (@dantraMSFT) I have added the requested test changes and httplistener now supports a headers option with a JSON object containing response header name/value pairs on the response test.

$statusCode = $queryItems["statuscode"]
$contentType = $queryItems["contenttype"]
$output = $queryItems["output"]
if ($queryItems['headers'])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be helpful to add a comment here that illustrates how the 'headers' value should be formatted; especially since this is the only documentation we have for the listener. :)
At a minimum, show the json structure. Even better would be to show a code sample similar to what you used in your tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dan Travison (@dantraMSFT) added. comments with examples

@TravisEz13

Copy link
Copy Markdown
Member

LGTM

@adityapatwardhan

Copy link
Copy Markdown
Member

Mark Kraus (@markekraus) Can you trigger of feature tests? They seems to have failed last time on Travis CI.

@markekraus

Copy link
Copy Markdown
Contributor Author

Aditya Patwardhan (@adityapatwardhan) feature tag added.

@markekraus

Copy link
Copy Markdown
Contributor Author

Aditya Patwardhan (@adityapatwardhan) all tests passed this time. (last time an unrelated test hung accessing a 3rd party website).

@iSazonov

Copy link
Copy Markdown
Collaborator

LGTM.

Thanks for great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WG-Cmdlets general cmdlet issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-WebRequest removes the Content-Type header from WebResponseObject's

7 participants