From 4d5a3c515d3c4b0aabc94447df59aeadb8470c9e Mon Sep 17 00:00:00 2001 From: Mark Kraus Date: Tue, 6 Feb 2018 04:52:53 -0600 Subject: [PATCH] [feature] Make UTF-8 Default Encoding for application/json --- .../BasicHtmlWebResponseObject.Common.cs | 6 ++++ .../Common/InvokeRestMethodCommand.Common.cs | 5 +++ .../WebCmdlets.Tests.ps1 | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index c2899459a40..d6e56dc062b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -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; } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 7c6a149cc7f..613ef69ca34 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -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; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index b8441a87128..089424ea371 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -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"}' + } + $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 @@ -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