Skip to content

Commit 334fa1d

Browse files
committed
Update module version and enhance User-Agent handling
Set the module version to 0.0.1 and refactor the User-Agent header construction in the Invoke-FabricRestMethod function.
1 parent 95ff614 commit 334fa1d

4 files changed

Lines changed: 20 additions & 73 deletions

File tree

source/FabricTools.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'FabricTools.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.31.0'
15+
ModuleVersion = '0.0.1'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

source/Private/Get-FabricUserAgent.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ function Get-FabricUserAgent {
5252
$archToken = $env:PROCESSOR_ARCHITECTURE -or 'unknown'
5353
}
5454

55-
return "powershell/$psVersion,FabricTools/$moduleVersion,($osToken; $archToken)"
55+
return "FabricTools/$moduleVersion powershell/$psVersion ($osToken; $archToken)"
5656
}

source/Public/Invoke-FabricRestMethod.ps1

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ Function Invoke-FabricRestMethod {
152152
$contentType = Get-PSFConfigValue -FullName 'FabricTools.FabricApi.ContentType'
153153
$userAgent = Get-PSFConfigValue -FullName 'FabricTools.UserAgent' -ErrorAction SilentlyContinue
154154

155+
# Build a fresh headers hashtable by copying session headers and ensuring User-Agent is included.
156+
$requestHeaders = @{}
157+
if ($sessionHeaders -and $sessionHeaders -is [hashtable]) {
158+
foreach ($key in $sessionHeaders.Keys) {
159+
$requestHeaders[$key] = $sessionHeaders[$key]
160+
}
161+
}
162+
if ($userAgent) {
163+
$requestHeaders['User-Agent'] = $userAgent
164+
}
165+
155166
$continuationToken = $null
156167
$repeat = $false
157168

@@ -163,17 +174,6 @@ Function Invoke-FabricRestMethod {
163174
}
164175
Write-Message -Message "API Endpoint: $Method $apiEndpointUrl" -Level Verbose
165176

166-
# Build a fresh headers hashtable by copying session headers and ensuring User-Agent is included.
167-
$requestHeaders = @{}
168-
if ($sessionHeaders -and $sessionHeaders -is [hashtable]) {
169-
foreach ($key in $sessionHeaders.Keys) {
170-
$requestHeaders[$key] = $sessionHeaders[$key]
171-
}
172-
}
173-
if ($userAgent) {
174-
$requestHeaders['User-Agent'] = $userAgent
175-
}
176-
177177
$request = @{
178178
Headers = $requestHeaders
179179
Uri = $Uri

tests/Unit/Invoke-FabricRestMethod.Tests.ps1

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)