8000 Add explicit ContentType detection to Invoke-RestMethod by dantraMSFT · Pull Request #4692 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Add explicit ContentType detection to Invoke-RestMethod #4692

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

Merged
merged 4 commits into from
Aug 31, 2017
Merged

Add explicit ContentType detection to Invoke-RestMethod #4692

merged 4 commits into from
Aug 31, 2017

Conversation

dantraMSFT
Copy link
Contributor
@dantraMSFT dantraMSFT commented Aug 28, 2017

fix #3267

This change removes the call to ContentHelper.GetEncoding since it's logic for returning a default encoding when a ContentType header is not found disables subsequent checks for meta charset definitions.

All variations of the Invoke-WebRequest tests have been mirrored for Invoke-RestMethod. Verbose output is used to verify the encoding since Invoke-RestMethod returns the json content directly instead of a response object. (See ExecuteRestMethod)

Add explicit ContentType detection to Invoke-RestMethod

[switch] $UseBasicParsing
)
$result = [PSObject]@{Output = $null; Error = $null; Encoding = $null; Content = $null}
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think that '[PSObject]' should probably be '[PSCustomObject]' - the [PSObject] doesn't actually convert the type away from a hashtable. So, [PSObject] can be removed, or changed to [PSCustomObject]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

}
if ($result.Encoding -eq $null)
{
throw "Encoding not found in verbose output. Lines: $($result).Verbose.Count Content:$($result.Verbose)"
Copy link
Collaborator

Choose a reason for hiding this comment

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

$($result).Verbose.Count should be $($result.Verbose.Count)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll fix 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.

Fixed

{
$result.Error = $_ | select-object * | Out-String
}
finally
Copy link
Collaborator

Choose a reason for hiding this comment

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

since the $TESTDRIVE is removed by pester, you only need set the verbosePreference back.

Copy link
Contributor

Choose a reason for hiding this comment

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

The test drive isn't cleared until the end of the context block. line 322 uses a test for the file's existence. With the current configuration, subsequent calls to ExecuteRestMethod could result in false positives for the test on line 322 if the file is not removed at the end at the end of the function. This cleanup action would need to remain or the filename generated on each call to ExecuteRestMethod would need to be unique.

Copy link
Contributor Author
@dantraMSFT dantraMSFT Aug 29, 2017

Choose a reason for hiding this comment

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

As @markekraus indicates, this is by design and simpler than generating file names.
it also occurs to me that I shouldn't be relying on test/context lifetime for this function; I don't know if it will be called once or multiple times.

I'll be leaving the code as is.

}

It "Verifies Invoke-WebRequest detects charset meta value when newlines are encountered in the element." {
$output = @'
Copy link
Collaborator

Choose a reason for hiding this comment

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

just a question, would:

"<html>`n<head>`n<meta`n   charset=""Unicode""  `n>`n</head>`n</html>`n"

be the same?

Copy link
Contributor Author
@dantraMSFT dantraMSFT Aug 29, 2017

Choose a reason for hiding this comment

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

Yes, but I find the HERE string much easier to read and it avoids me making silly escaping errors.
I'll be leaving it as is.

$VerbosePreference = $verbosePreferenceSave
if (Test-Path -Path $verboseFile)
{
Remove-Item -Path $verboseFile -ErrorAction Ignore
Copy link
Collaborator

Choose a reason for hiding this comment

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

Usualy we use SilentlyContinue not Ignore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, I'll change it.

Copy link
Collaborator
8000

Choose a reason for hiding this comment

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

Closed.

@@ -296,8 +296,71 @@ function ExecuteWebRequest
return $result
}

[string] $verboseEncodingPrefix = 'Content encoding: '
Copy link
Contributor

Choose a reason for hiding this comment

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

Move $verboseEncodingPrefix into the function body so it does not become orphaned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think I want a locally scoped variable that has to be initialized each call.

Copy link
Contributor

Choose a reason for hiding this comment

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

fair enough

@daxian-dbw
Copy link
Member

The WebCmdlets.Tests.ps1 has conflicts due to the merge of #4622 🤕

Copy link
Contributor
@markekraus markekraus left a comment

Choose a reason for hiding this comment

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

Artifacts from merge should be removed.

return $result
}

function GetSelfSignedCert {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is no longer needed it was removed in #4622

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it looks like Dongbo removed it when he merged.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes. the commit came in after I hit submit. both of these comments in this review are fixed now

@@ -831,6 +983,31 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {

#endregion SkipHeaderVerification Tests

#region Certificate Authentication Tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Everything in this region was removed in #4622

@daxian-dbw
Copy link
Member

@markekraus thanks! @dantraMSFT can you go through the file WebCmdlets.Tests.ps1 and see if things are good after resolving conflicts?

@dantraMSFT
Copy link
Contributor Author
dantraMSFT commented Aug 31, 2017

@daxian-dbw the merge looks fine and all tests pass on my systems.

@daxian-dbw daxian-dbw merged commit 96a90f8 into PowerShell:master Aug 31, 2017
@dantraMSFT dantraMSFT deleted the dantra/issue3267 branch August 31, 2017 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants
0