Skip to content

Commit 70e78ce

Browse files
committed
tests
1 parent 8788b29 commit 70e78ce

File tree

4 files changed

+146
-105
lines changed

4 files changed

+146
-105
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Import-Module $PSScriptRoot\.. -Force
2+
3+
Describe 'ConvertFrom-UrlQueryString functionality' -Tags Unit {
4+
InModuleScope UrlQueryStringParser {
5+
BeforeAll {
6+
$complexExampleQueryString = "?foo=bar&oogy&array=one&baz=quux&array=two&boogy&array=three&empty=&array=four&last"
7+
}
8+
9+
It 'Parses simple query string correctly' {
10+
$qs = '?a=1&b=two&c=3'
11+
$result = ConvertFrom-UrlQueryString -QueryString $qs
12+
13+
$result.Count | Should -Be 3
14+
$result['a'] | Should -Be '1'
15+
$result['b'] | Should -Be 'two'
16+
$result['c'] | Should -Be '3'
17+
}
18+
19+
It 'Handles URL encoded values and repeated keys' {
20+
$qs = 'name=John%20Doe&age=30&name=Jane'
21+
$result = ConvertFrom-UrlQueryString -QueryString $qs
22+
23+
$result['name'].Count | Should -Be 2
24+
$result['name'] | Should -Contain 'John Doe'
25+
$result['name'] | Should -Contain 'Jane'
26+
$result['age'] | Should -Be '30'
27+
}
28+
29+
It 'Returns zero-count hashtable for empty string' {
30+
(ConvertFrom-UrlQueryString "").Keys.Count | Should -Be 0
31+
}
32+
33+
It 'Converts from pseudo-empty querystring (just "?") to empty dict' {
34+
(ConvertFrom-UrlQueryString "?").Keys.Count | Should -Be 0
35+
}
36+
37+
It 'Converts from from simple single-entry querystring to single-entry dict' {
38+
(ConvertFrom-UrlQueryString "foo=bar")["foo"] | Should -Be "bar"
39+
}
40+
41+
It 'Converts from from simple single-entry querystring with ? prefix to single-entry dict' {
42+
(ConvertFrom-UrlQueryString "?foo=bar")["foo"] | Should -Be "bar"
43+
}
44+
45+
It 'Converts valueless entries with no = to $true' {
46+
(ConvertFrom-UrlQueryString "foo")["foo"] | Should -Be $true
47+
}
48+
49+
It 'Converts valueless entries with = by dropping them' {
50+
(ConvertFrom-UrlQueryString "foo=")["foo"].Length | Should -Be 0
51+
}
52+
53+
It 'Converts a complex example querystring while preserving the order of the keys' {
54+
$result = ConvertFrom-UrlQueryString $complexExampleQueryString
55+
$result.Keys -join "," | Should -Be "foo,oogy,array,baz,boogy,empty,last"
56+
}
57+
58+
It 'Converts repeated keys into arrays while preserving value-order' {
59+
$result = ConvertFrom-UrlQueryString $complexExampleQueryString
60+
$result['array'] -join "," | Should -Be "one,two,three,four"
61+
}
62+
63+
It 'Returns a case-insensitive dict' {
64+
$result = ConvertFrom-UrlQueryString $complexExampleQueryString
65+
{ $result['ArRaY'] } | Should -Not -Throw
66+
}
67+
}
68+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Import-Module $PSScriptRoot\.. -Force
2+
3+
Describe 'ConvertFrom-UrlQueryString functionality' -Tags Unit {
4+
InModuleScope UrlQueryStringParser {
5+
It 'Converts a complex example dict correctly' {
6+
$exampleOrderedDict = [ordered] @{
7+
foo="bar"
8+
oogy=$true
9+
array='one','two','three','four'
10+
baz="quux"
11+
boogy=$true
12+
empty=''
13+
donotshow=$false
14+
last=$true
15+
}
16+
17+
ConvertTo-UrlQueryString $exampleOrderedDict |
18+
Should -Be "?foo=bar&oogy&array=one&array=two&array=three&array=four&baz=quux&boogy&empty=&last"
19+
}
20+
21+
It 'Converts properly with empty ContinuationOfString' {
22+
ConvertTo-UrlQueryString @{foo=$true} -ContinuationOfString '' |
23+
Should -Be '?foo'
24+
}
25+
26+
It 'Converts properly with non-empty ContinuationOfString' {
27+
ConvertTo-UrlQueryString @{foo=$true} -ContinuationOfString '?bar=baz'|
28+
Should -Be '?bar=baz&foo'
29+
}
30+
31+
It 'Converts an empty dict with non-empty -ContinuationOfString to just be -ContinuationOfString.' {
32+
ConvertTo-UrlQueryString @{} -ContinuationOfString '?bar=baz' |
33+
Should -Be "?bar=baz"
34+
}
35+
36+
It 'Converts empty dict to empty UrlQueryString' {
37+
ConvertTo-UrlQueryString @{} |
38+
Should -Be ""
39+
}
40+
41+
It 'Converts dict with $null values to empty UrlQueryString' {
42+
ConvertTo-UrlQueryString @{foo=$null;bar=$null} |
43+
Should -Be ""
44+
}
45+
46+
It 'Skips encoding spaces when -DoMinimalEncode is set' {
47+
ConvertTo-UrlQueryString @{foo='bar baz quux'} -DoMinimalEncode |
48+
Should -Be "?foo=bar baz quux"
49+
}
50+
51+
It 'Encodes $ in keys' {
52+
ConvertTo-UrlQueryString @{'$foo'='bar'} |
53+
Should -Be "?%24foo=bar"
54+
}
55+
56+
It 'Skips encoding $ in keys when -DoMinimalEncode is set.' {
57+
ConvertTo-UrlQueryString @{'$foo'='bar'} -DoMinimalEncode |
58+
Should -Be "?`$foo=bar"
59+
}
60+
61+
It 'Encodes other various characters correctly when -DoMinimalEncode is set.' {
62+
ConvertTo-UrlQueryString ([ordered]@{
63+
allow = 'example equals= colon: at@ slash/ brackets[[] dollar$ comma, semicolon; question? parens()) star* exclaim! space space'
64+
disallow = "example hash# ampersand& percent% plus+ tab`t linebreak`r`n "
65+
}) -DoMinimalEncode |
66+
Should -Be (
67+
'?allow=example equals= colon: at@ slash/ brackets[[] dollar$ comma, semicolon; question? parens()) star* exclaim! space space' +
68+
'&disallow=example hash%23 ampersand%26 percent%25 plus%2B tab%09 linebreak%0D%0A '
69+
)
70+
}
71+
72+
It 'Encodes spaces correctly when -DoMinimalEncode is not set.' {
73+
ConvertTo-UrlQueryString @{foo='bar baz quux'} |
74+
Should -Be "?foo=bar%20baz%20quux"
75+
}
76+
}
77+
}

src/UrlQueryStringParser/UrlQueryStringParser.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function ConvertFrom-UrlQueryString {
9595
$QueryString = $QueryString.Substring(1, $QueryString.Length - 1)
9696
}
9797
$queryEntries = $QueryString -split "&"
98-
foreach($entry in $queryEntries) {
98+
foreach ($entry in $queryEntries) {
9999
if ($entry -like '*=*') {
100100
$equalsCharIndex = $entry.IndexOf("=")
101101
$field = $entry.Substring(0, $equalsCharIndex)

test/Test-UrlQueryStringParser.ps1

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)