Skip to content
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
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 @@ -1575,9 +1575,8 @@ internal void SetRequestContent(HttpRequestMessage request, string content)
ArgumentNullException.ThrowIfNull(content);

Encoding encoding = null;
string contentType = WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType];

if (contentType is not null)
if (WebSession.ContentHeaders.TryGetValue(HttpKnownHeaderNames.ContentType, out string contentType) && contentType is not null)
{
// If Content-Type contains the encoding format (as CharSet), use this encoding format
// to encode the Body of the WebRequest sent to the server. Default Encoding format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$uri = Get-WebListenerUrl -Test $method
$body = GetTestData -contentType $contentType
$command = "Invoke-WebRequest -Uri $uri -Body '$body' -Method $method -ContentType $contentType"
$commandNoContentType = "Invoke-WebRequest -Uri $uri -Body '$body' -Method $method"

It "Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {
It "Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {

$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
Expand All @@ -705,6 +706,29 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
# Validate that the response Content.data field is the same as what we sent.
$jsonContent.data | Should -Be $body
}

It "Invoke-WebRequest -Uri $uri -Method $method -Body [body data]" {

$result = ExecuteWebCommand -command $commandNoContentType
ValidateResponse -response $result

# Validate response content
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.url | Should -Match $uri
if ($method -eq "POST")
{
$jsonContent.headers.'Content-Type' | Should -Match "application/x-www-form-urlencoded"
# Validate that the response Content.form field is the same as what we sent.
[string]$jsonContent.form | Should -Be ([string][PSCustomObject]@{$body.Split("=")[0] = [System.Object[]]})
$jsonContent.data | Should -BeNullOrEmpty
}
else
{
# Validate that the response Content.data field is the same as what we sent.
$jsonContent.data | Should -Be $body
}

}
}
}

Expand Down Expand Up @@ -2739,6 +2763,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$uri = Get-WebListenerUrl -Test $method
$body = GetTestData -contentType $contentType
$command = "Invoke-RestMethod -Uri $uri -Body '$body' -Method $method -ContentType $contentType"
$commandNoContentType = "Invoke-RestMethod -Uri $uri -Body '$body' -Method $method"

It "Invoke-RestMethod -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {

Expand All @@ -2751,6 +2776,27 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
# Validate that the response Content.data field is the same as what we sent.
$result.Output.data | Should -Be $body
}

It "Invoke-RestMethod -Uri $uri -Method $method -Body [body data]" {

$result = ExecuteWebCommand -command $commandNoContentType

# Validate response
$result.Output.url | Should -Match $uri

if ($method -eq "POST")
{
$result.Output.headers.'Content-Type' | Should -Match "application/x-www-form-urlencoded"
# Validate that the response Content.form field is the same as what we sent.
[string]$result.Output.form | Should -Be ([string][PSCustomObject]@{$body.Split("=")[0] = [System.Object[]]})
$result.Output.data | Should -BeNullOrEmpty
}
else
{
# Validate that the response Content.data field is the same as what we sent.
$result.Output.data | Should -Be $body
}
}
}
}

Expand Down