Skip to content

Commit fcdc92c

Browse files
committed
Unit tests for Get-FabricUserAgent
1 parent 334fa1d commit fcdc92c

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
2+
3+
BeforeAll {
4+
$ModuleName = 'FabricTools'
5+
$PSDefaultParameterValues['Mock:ModuleName'] = $ModuleName
6+
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $ModuleName
7+
$PSDefaultParameterValues['Should:ModuleName'] = $ModuleName
8+
}
9+
10+
Describe "Get-FabricUserAgent" -Tag "UnitTests" {
11+
12+
Context "Return format" {
13+
It 'Should return a non-empty string' {
14+
InModuleScope FabricTools {
15+
$result = Get-FabricUserAgent
16+
$result | Should -Not -BeNullOrEmpty
17+
}
18+
}
19+
20+
It 'Should match the expected pattern: FabricTools/<ver> powershell/<ver> (<os>; <arch>)' {
21+
InModuleScope FabricTools {
22+
$result = Get-FabricUserAgent
23+
$result | Should -Match '^FabricTools/\S+ powershell/\S+ \(.+;\s*.+\)$'
24+
}
25+
}
26+
27+
It 'Should produce a value accepted as a valid HTTP User-Agent header' {
28+
InModuleScope FabricTools {
29+
$result = Get-FabricUserAgent
30+
{
31+
$msg = [System.Net.Http.HttpRequestMessage]::new()
32+
$msg.Headers.Add('User-Agent', $result)
33+
$msg.Dispose()
34+
} | Should -Not -Throw
35+
}
36+
}
37+
}
38+
39+
Context "PowerShell version token" {
40+
It 'Should include the running PowerShell version' {
41+
InModuleScope FabricTools {
42+
$result = Get-FabricUserAgent
43+
$expected = $PSVersionTable.PSVersion.ToString()
44+
$result | Should -Match "powershell/$([regex]::Escape($expected))"
45+
}
46+
}
47+
}
48+
49+
Context "Module version token" {
50+
It 'Should include a non-empty module version (not unknown) when FabricTools is loaded' {
51+
InModuleScope FabricTools {
52+
$result = Get-FabricUserAgent
53+
$result | Should -Not -Match 'FabricTools/unknown'
54+
}
55+
}
56+
57+
It 'Should include a version token in the FabricTools segment' {
58+
InModuleScope FabricTools {
59+
$result = Get-FabricUserAgent
60+
$result | Should -Match '^FabricTools/\S+'
61+
}
62+
}
63+
}
64+
65+
Context "OS and architecture tokens" {
66+
It 'Should include a non-empty OS token inside parentheses' {
67+
InModuleScope FabricTools {
68+
$result = Get-FabricUserAgent
69+
$result | Should -Match '\(.+;'
70+
}
71+
}
72+
73+
It 'Should include a non-empty architecture token inside parentheses' {
74+
InModuleScope FabricTools {
75+
$result = Get-FabricUserAgent
76+
$result | Should -Match ';\s*\S+\)'
77+
}
78+
}
79+
80+
It 'Should collapse multiple spaces in OS description to a single space' {
81+
InModuleScope FabricTools {
82+
$result = Get-FabricUserAgent
83+
$result | Should -Not -Match '\s{2,}'
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)