Skip to content
Open
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 @@ -2107,6 +2107,7 @@ 304 or (>= 400 and <= 599) => true,
WebRequestMethod.Put => HttpMethod.Put,
WebRequestMethod.Options => HttpMethod.Options,
WebRequestMethod.Trace => HttpMethod.Trace,
WebRequestMethod.Query => HttpMethod.Query,
_ => new HttpMethod(method.ToString().ToUpperInvariant())
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@ public enum WebRequestMethod
/// PATCH method.
/// </summary>
Patch,

/// <summary>
/// QUERY method.
/// </summary>
Query,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,47 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
($result.Output.Content | ConvertFrom-Json).query | Should -Be "?testparam=testvalue"
}

It "Validate Invoke-WebRequest -Method Query is used and body is sent as request content" {
$uri = Get-WebListenerUrl -Test 'Query'
$command = "Invoke-WebRequest -Uri '$uri' -Method Query -Body @{'testparam'='testvalue'} -ContentType 'application/x-www-form-urlencoded'"
$result = ExecuteWebCommand -command $command
$result.Error | Should -BeNullOrEmpty
$jsonResult = $result.Output.Content | ConvertFrom-Json
$jsonResult.method | Should -Be "QUERY"
$jsonResult.form.testparam | Should -Be "testvalue"
# A hashtable body must be sent as the request content, not converted to URI query parameters
$jsonResult.args.testparam | Should -BeNullOrEmpty
}

It "Validate Invoke-WebRequest -Method Query with a string body" {
$uri = Get-WebListenerUrl -Test 'Query'
$command = "Invoke-WebRequest -Uri '$uri' -Method Query -Body '{`"testparam`":`"testvalue`"}' -ContentType 'application/json'"
$result = ExecuteWebCommand -command $command
$result.Error | Should -BeNullOrEmpty
$jsonResult = $result.Output.Content | ConvertFrom-Json
$jsonResult.method | Should -Be "QUERY"
$jsonResult.headers.'Content-Type' | Should -Match "application/json"
$jsonResult.data | Should -Be '{"testparam":"testvalue"}'
}

It "Validate Invoke-WebRequest -CustomMethod QUERY behaves like -Method Query" {
$uri = Get-WebListenerUrl -Test 'Query'
$command = "Invoke-WebRequest -Uri '$uri' -CustomMethod QUERY -Body @{'testparam'='testvalue'} -ContentType 'application/x-www-form-urlencoded'"
$result = ExecuteWebCommand -command $command
$result.Error | Should -BeNullOrEmpty
$jsonResult = $result.Output.Content | ConvertFrom-Json
$jsonResult.method | Should -Be "QUERY"
$jsonResult.form.testparam | Should -Be "testvalue"
}

It 'Validate Invoke-WebRequest empty body -Method Query' {
$uri = Get-WebListenerUrl -Test 'Query'
$command = "Invoke-WebRequest -Uri '$uri' -Method Query"
$result = ExecuteWebCommand -command $command
$result.Error | Should -BeNullOrEmpty
($result.Output.Content | ConvertFrom-Json).method | Should -Be "QUERY"
}

It "Validate Invoke-WebRequest returns HTTP errors in exception" {
$query = @{
body = "I am a teapot!!!"
Expand Down Expand Up @@ -2994,6 +3035,36 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Output.Query | Should -Be "?testparam=testvalue"
}

It "Validate Invoke-RestMethod -Method Query is used and body is sent as request content" {
$uri = Get-WebListenerUrl -Test 'Query'
$command = "Invoke-RestMethod -Uri '$uri' -Method Query -Body @{'testparam'='testvalue'} -ContentType 'application/x-www-form-urlencoded'"
$result = ExecuteWebCommand -command $command
$result.Error | Should -BeNullOrEmpty
$result.Output.method | Should -Be "QUERY"
$result.Output.form.testparam | Should -Be "testvalue"
# A hashtable body must be sent as the request content, not converted to URI query parameters
$result.Output.args.testparam | Should -BeNullOrEmpty
}

It "Validate Invoke-RestMethod -Method Query with a string body" {
$uri = Get-WebListenerUrl -Test 'Query'
$command = "Invoke-RestMethod -Uri '$uri' -Method Query -Body '{`"testparam`":`"testvalue`"}' -ContentType 'application/json'"
$result = ExecuteWebCommand -command $command
$result.Error | Should -BeNullOrEmpty
$result.Output.method | Should -Be "QUERY"
$result.Output.Headers.'Content-Type' | Should -Match "application/json"
$result.Output.data | Should -Be '{"testparam":"testvalue"}'
}

It "Validate Invoke-RestMethod -CustomMethod QUERY behaves like -Method Query" {
$uri = Get-WebListenerUrl -Test 'Query'
$command = "Invoke-RestMethod -Uri '$uri' -CustomMethod QUERY -Body @{'testparam'='testvalue'} -ContentType 'application/x-www-form-urlencoded'"
$result = ExecuteWebCommand -command $command
$result.Error | Should -BeNullOrEmpty
$result.Output.method | Should -Be "QUERY"
$result.Output.form.testparam | Should -Be "testvalue"
}

It "Validate Invoke-RestMethod returns HTTP errors in exception" {
$query = @{
body = "I am a teapot!!!"
Expand Down
5 changes: 5 additions & 0 deletions test/tools/WebListener/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
template: "Delete",
defaults: new { controller = "Get", action = "Index" },
constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint("DELETE") }));
routes.MapRoute(
name: "query",
template: "Query",
defaults: new { controller = "Get", action = "Index" },
constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint("QUERY") }));
routes.MapRoute(
name: "retry",
template: "Retry/{sessionId?}/{failureCode?}/{failureCount?}",
Expand Down