8000 Make UTF-8 Default Encoding for application/json by markekraus · Pull Request #6109 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Make UTF-8 Default Encoding for application/json #6109

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 1 commit into from
Feb 22, 2018
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
< 10000 /details-menu>
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 @@ -166,6 +166,12 @@ protected void InitializeContent()
Encoding encoding = null;
// fill the Content buffer
string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse);

if (String.IsNullOrEmpty(characterSet) && ContentHelper.IsJson(contentType))
{
characterSet = Encoding.UTF8.HeaderName;
}

this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding);
this.Encoding = encoding;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ internal override void ProcessResponse(HttpResponseMessage response)
StreamHelper.TryGetEncoding(charSet, out encoding);
}

if (string.IsNullOrEmpty(charSet) && returnType == RestReturnType.Json)
{
encoding = Encoding.UTF8;
}

object obj = null;
Exception ex = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,22 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
$response.Output.Encoding.EncodingName | Should Be $expectedEncoding.EncodingName
$response.Output | Should BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
}

It "Verifies Invoke-WebRequest defaults to UTF8 on application/json when no charset is present" {
# when contenttype is set, WebListener suppresses charset unless it is included in the query
$query = @{
contenttype = 'application/json'
body = '{"Test": "Test"}'
Copy link
Collaborator
@iSazonov iSazonov Feb 7, 2018

Choose a reason for hiding this comment

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

Should we add multibyte characters to ensure Utf8 encoding works? (`u{...})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need. We are not testing the ability of .NET to properly decode UTF-8. We are testing the ability of the web cmdlets to chose the correct default encoding for application/json. This is an acceptable tests because we already have tests for the iso-8859-1 defaults for other mime type and we have UTF-8 tests which do ensure we can work with UTF-8 characters. Here we only need to make sure the UTF-8 encoding was selected.

}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::UTF8
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing

$response.Error | Should BeNullOrEmpty
$response.Output.Encoding.EncodingName | Should Be $expectedEncoding.EncodingName
$response.Output | Should BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
$response.Output.Content | Should BeExactly $query.body
}
}

#endregion charset encoding tests
Expand Down Expand Up @@ -2303,6 +2319,21 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
$response.Error | Should BeNullOrEmpty
$response.Encoding.EncodingName | Should Be $expectedEncoding.EncodingName
}

It "Verifies Invoke-RestMethod defaults to UTF8 on application/json when no charset is present" {
# when contenttype is set, WebListener suppresses charset unless it is included in the query
$query = @{
contenttype = 'application/json'
body = '{"Test": "Test"}'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [System.Text.Encoding]::UTF8
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing

$response.Error | Should BeNullOrEmpty
$response.Encoding.EncodingName | Should Be $expectedEncoding.EncodingName
$response.output.Test | Should BeExactly 'Test'
}
}

#endregion charset encoding tests
Expand Down
0