| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1241,26 +1241,29 @@ internal virtual void FillRequestStream(HttpRequestMessage request) | |||
| 1241 | 1241 | } | |
| 1242 | 1242 | ||
| 1243 | 1243 | // Add the content headers | |
| 1244 | - if (request.Content != null) | ||
| 1244 | + if (request.Content == null) | ||
| 1245 | + { | ||
| 1246 | + request.Content = new StringContent(string.Empty); | ||
| 1247 | + request.Content.Headers.Clear(); | ||
| 1248 | + } | ||
| 1249 | + | ||
| 1250 | + foreach (var entry in WebSession.ContentHeaders) | ||
| 1245 | 1251 | { | |
| 1246 | - foreach (var entry in WebSession.ContentHeaders) | ||
| 1252 | + if (SkipHeaderValidation) | ||
| 1247 | 1253 | { | |
| 1248 | - if (SkipHeaderValidation) | ||
| 1254 | + request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value); | ||
| 1255 | + } | ||
| 1256 | + else | ||
| 1257 | + { | ||
| 1258 | + try | ||
| 1249 | 1259 | { | |
| 1250 | - request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value); | ||
| 1260 | + request.Content.Headers.Add(entry.Key, entry.Value); | ||
| 1251 | 1261 | } | |
| 1252 | - else | ||
| 1262 | + catch (FormatException ex) | ||
| 1253 | 1263 | { | |
| 1254 | - try | ||
| 1255 | - { | ||
| 1256 | - request.Content.Headers.Add(entry.Key, entry.Value); | ||
| 1257 | - } | ||
| 1258 | - catch (FormatException ex) | ||
| 1259 | - { | ||
| 1260 | - var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex); | ||
| 1261 | - ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType); | ||
| 1262 | - ThrowTerminatingError(er); | ||
| 1263 | - } | ||
| 1264 | + var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex); | ||
| 1265 | + ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType); | ||
| 1266 | + ThrowTerminatingError(er); | ||
| 1264 | 1267 | } | |
| 1265 | 1268 | } | |
| 1266 | 1269 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -993,6 +993,28 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { | |||
| 993 | 993 | $result.data | Should -Match 'bar' | |
| 994 | 994 | $result.headers.'Content-Type' | Should -BeExactly $contentType | |
| 995 | 995 | } | |
| 996 | + | ||
| 997 | + It "Verifies Invoke-WebRequest applies -ContentType when no -Body is present" { | ||
| 998 | + $contentType = 'application/json' | ||
| 999 | + $uri = Get-WebListenerUrl -Test 'Get' | ||
| 1000 | + | ||
| 1001 | + $response = Invoke-WebRequest -Uri $uri -Method 'GET' -ContentType $contentType | ||
| 1002 | + $result = $response.Content | ConvertFrom-Json | ||
| 1003 | + | ||
| 1004 | + $result.data | Should -BeNullOrEmpty | ||
| 1005 | + $result.headers.'Content-Type' | Should -BeExactly $contentType | ||
| 1006 | + } | ||
| 1007 | + | ||
| 1008 | + It "Verifies Invoke-WebRequest applies an invalid -ContentType when no -Body is present and -SkipHeaderValidation is present" { | ||
| 1009 | + $contentType = 'foo' | ||
| 1010 | + $uri = Get-WebListenerUrl -Test 'Get' | ||
| 1011 | + | ||
| 1012 | + $response = Invoke-WebRequest -Uri $uri -Method 'GET' -ContentType $contentType -SkipHeaderValidation | ||
| 1013 | + $result = $response.Content | ConvertFrom-Json | ||
| 1014 | + | ||
| 1015 | + $result.data | Should -BeNullOrEmpty | ||
| 1016 | + $result.headers.'Content-Type' | Should -BeExactly $contentType | ||
| 1017 | + } | ||
| 996 | 1018 | } | |
| 997 | 1019 | ||
| 998 | 1020 | #region charset encoding tests | |
@@ -2388,6 +2410,26 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { | |||
| 2388 | 2410 | $result.data | Should -Match 'bar' | |
| 2389 | 2411 | $result.headers.'Content-Type' | Should -BeExactly $contentType | |
| 2390 | 2412 | } | |
| 2413 | + | ||
| 2414 | + It "Verifies Invoke-RestMethod applies -ContentType when no -Body is present" { | ||
| 2415 | + $contentType = 'application/json' | ||
| 2416 | + $uri = Get-WebListenerUrl -Test 'Get' | ||
| 2417 | + | ||
| 2418 | + $result = Invoke-RestMethod -Uri $uri -Method 'GET' -ContentType $contentType | ||
| 2419 | + | ||
| 2420 | + $result.data | Should -BeNullOrEmpty | ||
| 2421 | + $result.headers.'Content-Type' | Should -BeExactly $contentType | ||
| 2422 | + } | ||
| 2423 | + | ||
| 2424 | + It "Verifies Invoke-RestMethod applies an invalid -ContentType when no -Body is present and -SkipHeaderValidation is present" { | ||
| 2425 | + $contentType = 'foo' | ||
| 2426 | + $uri = Get-WebListenerUrl -Test 'Get' | ||
| 2427 | + | ||
| 2428 | + $result = Invoke-RestMethod -Uri $uri -Method 'GET' -ContentType $contentType -SkipHeaderValidation | ||
| 2429 | + | ||
| 2430 | + $result.data | Should -BeNullOrEmpty | ||
| 2431 | + $result.headers.'Content-Type' | Should -BeExactly $contentType | ||
| 2432 | + } | ||
| 2391 | 2433 | } | |
| 2392 | 2434 | ||
| 2393 | 2435 | Context "HTTPS Tests" { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments