@@ -227,6 +227,50 @@ function ExecuteRequestWithCustomHeaders
227
227
return $result
228
228
}
229
229
230
+ # This function calls either Invoke-WebRequest or Invoke-RestMethod with the given uri
231
+ # using the custom UserAgent and the optional SkipHeaderValidation switch.
232
+ function ExecuteRequestWithCustomUserAgent {
233
+ param (
234
+ [Parameter (Mandatory )]
235
+ [string ]
236
+ $Uri ,
237
+
238
+ [ValidateSet (' Invoke-WebRequest' , ' Invoke-RestMethod' )]
239
+ [string ] $Cmdlet = ' Invoke-WebRequest' ,
240
+
241
+ [Parameter (Mandatory )]
242
+ [ValidateNotNull ()]
243
+ [string ] $UserAgent ,
244
+
245
+ [switch ] $SkipHeaderValidation
246
+ )
247
+ $result = [PSObject ]@ {Output = $null ; Error = $null ; Content = $null }
248
+
249
+ try {
250
+ $Params = @ {
251
+ Uri = $Uri
252
+ TimeoutSec = 5
253
+ UserAgent = $UserAgent
254
+ SkipHeaderValidation = $SkipHeaderValidation.IsPresent
255
+ }
256
+ if ($Cmdlet -eq ' Invoke-WebRequest' ) {
257
+ $result.Output = Invoke-WebRequest @Params
258
+ $result.Content = $result.Output.Content | ConvertFrom-Json
259
+ }
260
+ else {
261
+ $result.Output = Invoke-RestMethod @Params
262
+ # NOTE: $result.Output should already be a PSObject (Invoke-RestMethod converts the returned json automatically)
263
+ # so simply reference $result.Output
264
+ $result.Content = $result.Output
265
+ }
266
+ }
267
+ catch {
268
+ $result.Error = $_
269
+ }
270
+
271
+ return $result
272
+ }
273
+
230
274
<#
231
275
Defines the list of redirect codes to test as well as the
232
276
expected Method when the redirection is handled.
@@ -732,6 +776,31 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
732
776
$response.Content.Headers -contains " If-Match" | Should Be $true
733
777
}
734
778
779
+ It " Verifies Invoke-WebRequest default UserAgent handling with no errors" {
780
+ $UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent ]::InternetExplorer
781
+ $response = ExecuteRequestWithCustomUserAgent - Uri " http://localhost:8081/PowerShell?test=echo" - UserAgent $UserAgent - Cmdlet " Invoke-WebRequest"
782
+
783
+ $response.Error | Should BeNullOrEmpty
784
+ $response.Content.Headers .' User-Agent' | Should Match $UserAgent
785
+ }
786
+
787
+ It " Verifies Invoke-WebRequest default UserAgent handling reports an error is returned for an invalid UserAgent value" {
788
+ $UserAgent = ' Invalid:Agent'
789
+ $response = ExecuteRequestWithCustomUserAgent - Uri " http://localhost:8081/PowerShell?test=echo" - UserAgent $UserAgent - Cmdlet " Invoke-WebRequest"
790
+
791
+ $response.Error | Should Not BeNullOrEmpty
792
+ $response.Error.FullyQualifiedErrorId | Should Be " System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
793
+ $response.Error.Exception.Message | Should Be " The format of value 'Invalid:Agent' is invalid."
794
+ }
795
+
796
+ It " Verifies Invoke-WebRequest UserAgent handling does not report an error when using -SkipHeaderValidation" {
797
+ $UserAgent = ' Invalid:Agent'
798
+ $response = ExecuteRequestWithCustomUserAgent - Uri " http://localhost:8081/PowerShell?test=echo" - UserAgent $UserAgent - SkipHeaderValidation - Cmdlet " Invoke-WebRequest"
799
+
800
+ $response.Error | Should BeNullOrEmpty
801
+ $response.Content.Headers .' User-Agent' | Should Match $UserAgent
802
+ }
803
+
735
804
# endregion SkipHeaderVerification Tests
736
805
737
806
BeforeEach {
@@ -1223,6 +1292,31 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
1223
1292
$response.Content.Headers -contains " If-Match" | Should Be $true
1224
1293
}
1225
1294
1295
+ It " Verifies Invoke-RestMethod default UserAgent handling with no errors" {
1296
+ $UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent ]::InternetExplorer
1297
+ $response = ExecuteRequestWithCustomUserAgent - Uri " http://localhost:8081/PowerShell?test=echo" - UserAgent $UserAgent - Cmdlet " Invoke-RestMethod"
1298
+
1299
+ $response.Error | Should BeNullOrEmpty
1300
+ $response.Content.Headers .' User-Agent' | Should Match $UserAgent
1301
+ }
1302
+
1303
+ It " Verifies Invoke-RestMethod default UserAgent handling reports an error is returned for an invalid UserAgent value" {
1304
+ $UserAgent = ' Invalid:Agent'
1305
+ $response = ExecuteRequestWithCustomUserAgent - Uri " http://localhost:8081/PowerShell?test=echo" - UserAgent $UserAgent - Cmdlet " Invoke-RestMethod"
1306
+
1307
+ $response.Error | Should Not BeNullOrEmpty
1308
+ $response.Error.FullyQualifiedErrorId | Should Be " System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
1309
+ $response.Error.Exception.Message | Should Be " The format of value 'Invalid:Agent' is invalid."
1310
+ }
1311
+
1312
+ It " Verifies Invoke-RestMethod UserAgent handling does not report an error when using -SkipHeaderValidation" {
1313
+ $UserAgent = ' Invalid:Agent'
1314
+ $response = ExecuteRequestWithCustomUserAgent - Uri " http://localhost:8081/PowerShell?test=echo" - UserAgent $UserAgent - SkipHeaderValidation - Cmdlet " Invoke-RestMethod"
1315
+
1316
+ $response.Error | Should BeNullOrEmpty
1317
+ $response.Content.Headers .' User-Agent' | Should Match $UserAgent
1318
+ }
1319
+
1226
1320
# endregion SkipHeaderVerification tests
1227
1321
1228
1322
BeforeEach {
0 commit comments