@@ -35,11 +35,9 @@ Describe "Invoke-FabricRestMethod" -Tag "UnitTests" {
3535
3636 Context " Successful REST call" {
3737 BeforeAll {
38- Mock - CommandName Invoke-WebRequest - MockWith {
38+ Mock - CommandName Invoke-RestMethod - MockWith {
3939 return @ {
40- StatusCode = 200
41- Headers = @ {}
42- Content = ' {"value": [{"id": "test-id", "name": "test-name"}]}'
40+ value = @ (@ { id = ' test-id' ; name = ' test-name' })
4341 }
4442 }
4543 Mock - CommandName Confirm-TokenState - MockWith { return $true }
@@ -72,50 +70,18 @@ Describe "Invoke-FabricRestMethod" -Tag "UnitTests" {
7270
7371 Context " User-Agent header" {
7472 BeforeAll {
75- # Clean up any previous dump files
76- $dumpPath = Join-Path $PSScriptRoot ' ..\..\output\invoke-restmethod-mock-dump.json'
77- $outDir = Split-Path $dumpPath - Parent
78- if (-not (Test-Path $outDir )) { New-Item - ItemType Directory - Path $outDir | Out-Null }
79- Remove-Item - Path $dumpPath - ErrorAction SilentlyContinue
80-
81- # Capture the headers passed to Invoke-RestMethod
82- Set-Variable - Name capturedHeaders - Value $null - Scope Script - Force
83-
8473 Mock - CommandName Get-PSFConfigValue - MockWith {
8574 param ($FullName )
86- $logPath = Join-Path $PSScriptRoot ' ..\..\output\get-psfconfig-dump.json'
87- $entry = @ { Name = $FullName ; Time = (Get-Date ).ToString(' o' ) }
88- Add-Content - Path $logPath - Value ($entry | ConvertTo-Json - Compress)
8975 switch ($FullName ) {
9076 ' FabricTools.UserAgent' { return ' TestUA/1.2' }
91- ' FabricTools.FabricSession.Headers' { return @ { ' User-Agent ' = ' TestUA/1.2 ' } }
77+ ' FabricTools.FabricSession.Headers' { return @ {} }
9278 ' FabricTools.FabricApi.ContentType' { return ' application/json; charset=utf-8' }
9379 Default { return $null }
9480 }
9581 }
9682
9783 Mock - CommandName Invoke-RestMethod - MockWith {
98- $dump = @ { Bound = $PSBoundParameters ; Args = $args } | ConvertTo-Json - Compress
99- $outPath = Join-Path $PSScriptRoot ' ..\..\output\invoke-restmethod-mock-dump.json'
100- $outDir = Split-Path $outPath - Parent
101- if (-not (Test-Path $outDir )) { New-Item - ItemType Directory - Path $outDir | Out-Null }
102- Set-Content - Path $outPath - Value $dump # Use Set-Content (not Add-Content) to write single JSON object
103-
104- if ($PSBoundParameters.ContainsKey (' Headers' )) {
105- Set-Variable - Name capturedHeaders - Value $PSBoundParameters [' Headers' ] - Scope Script - Force
106- } elseif ($args -and $args.Count -ge 2 ) {
107- # Pester may pass named parameters as an args array: ['-Param:', <value>, ...]
108- for ($i = 0 ; $i -lt $args.Count ; $i += 2 ) {
109- $name = $args [$i ].ToString()
110- if ($name -match ' Headers' ) {
111- Set-Variable - Name capturedHeaders - Value $args [$i + 1 ] - Scope Script - Force
112- break
113- }
114- }
115- } else {
116- Set-Variable - Name capturedHeaders - Value $null - Scope Script - Force
117- }
118- return @ { value = @ () ; statusCode = 200 }
84+ return @ { value = @ (); statusCode = 200 }
11985 }
12086
12187 Mock - CommandName Confirm-TokenState - MockWith { return $true }
@@ -125,28 +91,9 @@ Describe "Invoke-FabricRestMethod" -Tag "UnitTests" {
12591 InModuleScope - ModuleName ' FabricTools' {
12692 Invoke-FabricRestMethod - Uri ' https://api.fabric.microsoft.com/v1/test' | Out-Null
12793
128- # Try to capture from script variable first (if mock successfully set it)
129- $captured = $script :capturedHeaders
130-
131- # If not captured via script variable, read from dump file
132- if (-not $captured ) {
133- $dumpPath = Join-Path $PSScriptRoot ' ..\..\output\invoke-restmethod-mock-dump.json'
134- if (Test-Path $dumpPath ) {
135- $dump = Get-Content $dumpPath - Raw | ConvertFrom-Json
136- $args = $dump.Args
137- # Find Headers in the args array
138- for ($i = 0 ; $i -lt $args.Count - 1 ; $i ++ ) {
139- if ($args [$i ] -eq ' -Headers:' ) {
140- $captured = $args [$i + 1 ]
141- break
142- }
143- }
144- }
145- }
146-
147- # Assert the headers were captured and contain User-Agent
148- $captured | Should -Not - BeNullOrEmpty - Because ' Headers should be captured from mock'
149- $captured .' User-Agent' | Should - Be ' TestUA/1.2' - Because ' User-Agent header should be set to configured value'
94+ Should - Invoke - CommandName Invoke-RestMethod - Times 1 - ParameterFilter {
95+ $Headers -ne $null -and $Headers [' User-Agent' ] -eq ' TestUA/1.2'
96+ } - Because ' Invoke-RestMethod should be called with User-Agent header set to configured value'
15097 }
15198 }
15299 }
0 commit comments