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

Add support for Content Headers to BasicHtmlWebResponseObject and HtmlWebResponseObject #4494

Merged
merged 4 commits into from
Aug 21, 2017

Conversation

markekraus
Copy link
Contributor

@markekraus markekraus commented Aug 4, 2017

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
Copy link

msftclas commented Aug 4, 2017

@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
Collaborator

iSazonov commented Aug 5, 2017

@markekraus Could you please add tests?

@markekraus
Copy link
Contributor Author

@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.

@@ -734,6 +734,24 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {

#endregion SkipHeaderVerification Tests

It "Verifies Invoke-WenRequest includes Content headers in Headers property" {
Copy link
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
Contributor Author

@iSazonov doh... corrected.

@TravisEz13
Copy link
Member

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

@TravisEz13 TravisEz13 added the WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module label Aug 5, 2017
@markekraus
Copy link
Contributor Author

@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
Contributor Author

@TravisEz13 @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
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dantraMSFT corrected.

@@ -36,6 +36,12 @@ public partial class WebResponseObject
{
headers[entry.Key] = entry.Value;
}
if(BaseResponse.Content != null){
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

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

@@ -1070,6 +1070,24 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {

#endregion charset encoding tests

It "Verifies Invoke-WebRequest includes Content headers in Headers property" {
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/get'"
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

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

@@ -1070,6 +1070,24 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {

#endregion charset encoding tests

It "Verifies Invoke-WebRequest includes Content headers in Headers property" {
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 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
Contributor Author

@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.

@@ -133,6 +133,14 @@ Function Start-HTTPListener {
$statusCode = $queryItems["statuscode"]
$contentType = $queryItems["contenttype"]
$output = $queryItems["output"]
if ($queryItems['headers'])
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

@dantraMSFT added. comments with examples

@TravisEz13
Copy link
Member

LGTM

@adityapatwardhan
Copy link
Member

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

@markekraus
Copy link
Contributor Author

@adityapatwardhan feature tag added.

@markekraus
Copy link
Contributor Author

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

@iSazonov
Copy link
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-Utility cmdlets in the Microsoft.PowerShell.Utility module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants