Skip to content

Commit abef3ea

Browse files
committed
add test script, fix empty hash returning ?
1 parent 744d85c commit abef3ea

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed
110 Bytes
Binary file not shown.

src/UrlQueryStringParser/UrlQueryStringParser.psm1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ function ConvertTo-UrlQueryString {
1313
# $true will be stored as valueless entries in the query-string.
1414
[Parameter(ValueFromPipeline)]
1515
[Collections.IDictionary] $Members,
16+
17+
# A previous QueryString that this is continuing. If it's non-empty, then the beginning separator will be
18+
# '&' instead of '?'
19+
[Parameter()]
20+
[string] $ContinuationOfString,
1621

1722
# Leave the space-characters as space characters, which some browsers support.
1823
[switch] $SkipEncodeSpaces
1924
)
2025
process {
21-
[string] $result = ""
22-
if ($Members.Keys) {
23-
$result += "?"
26+
[string] $result = "" + $ContinuationOfString
27+
if ($Members.Keys -and $Members.Keys.Count) {
28+
$result += if (-not $ContinuationOfString) {"?"}
2429
}
2530
foreach($key in $Members.Keys) {
2631
$key = [uri]::EscapeDataString($key.ToString())

test/Test-UrlQueryStringParser.ps1

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<#
2+
.SYNOPSIS
3+
Tests for UrlQueryStringParser
4+
#>
5+
[CmdletBinding()]
6+
param()
7+
8+
if ((ConvertTo-UrlQueryString @{"foo"=$true} -ContinuationOfString '') -ne '?foo') {
9+
throw "Continuation of empty failed."
10+
}
11+
12+
if ((ConvertTo-UrlQueryString @{"foo"=$true} -ContinuationOfString '?bar=baz') -ne '?bar=baz&foo') {
13+
throw "Continuation of non-empty failed."
14+
}
15+
16+
if ((ConvertTo-UrlQueryString @{}) -ne "") {
17+
throw "Empty conversion to UrlQueryString failed."
18+
}
19+
20+
if ((ConvertTo-UrlQueryString @{} -ContinuationOfString '?bar=baz') -ne "?bar=baz") {
21+
throw "Continuation with empty failed."
22+
}
23+
24+
if ((ConvertTo-UrlQueryString @{"foo"='bar baz quux'} -SkipEncodeSpaces) -ne "?foo=bar baz quux") {
25+
throw "Skip encoding spaces failed."
26+
}
27+
28+
if ((ConvertTo-UrlQueryString @{"foo"='bar baz quux'}) -ne "?foo=bar%20baz%20quux") {
29+
throw "Encoding spaces failed."
30+
}
31+
32+
$exampleOrderedDict = [ordered] @{
33+
foo="bar"
34+
oogy=$true
35+
array='one','two','three','four'
36+
baz="quux"
37+
boogy=$true
38+
empty=''
39+
donotshow=$false
40+
last=$true
41+
}
42+
43+
if ((ConvertTo-UrlQueryString $exampleOrderedDict) -ne "?foo=bar&oogy&array=one&array=two&array=three&array=four&baz=quux&boogy&empty=&last") {
44+
throw "Deluxe test failed."
45+
}
46+
47+
if ((ConvertFrom-UrlQueryString "").Keys.Count -gt 0) {
48+
throw "Conversion from empty querystring failed."
49+
}
50+
51+
if ((ConvertFrom-UrlQueryString "?").Keys.Count -gt 0) {
52+
throw "Conversion from pseudo-empty querystring failed."
53+
}
54+
55+
if ((ConvertFrom-UrlQueryString "foo=bar")["foo"] -ne "bar") {
56+
throw "Conversion from simple single-entry foo=bar failed."
57+
}
58+
59+
if ((ConvertFrom-UrlQueryString "?foo=bar")["foo"] -ne "bar") {
60+
throw "Conversion from simple single-entry foo=bar with '?' prefix failed."
61+
}
62+
63+
if ((ConvertFrom-UrlQueryString "foo")["foo"] -ne $true) {
64+
throw 'Valueless entries as $true failed.'
65+
}
66+
67+
if ((ConvertFrom-UrlQueryString "foo=")["foo"].Length -ne 0) {
68+
throw 'Convert from empty-string-value querystring failed.'
69+
}
70+
71+
$exampleQueryString = "?foo=bar&oogy&array=one&baz=quux&array=two&boogy&array=three&empty=&array=four&last"
72+
$result = ConvertFrom-UrlQueryString $exampleQueryString
73+
if (($result.Keys -join ",") -ne "foo,oogy,array,baz,boogy,empty,last") {
74+
throw "Order of keys not preserved converting to ordered dictionary."
75+
}
76+
if (($result['array'] -join ",") -ne "one,two,three,four") {
77+
throw "Order of array members not preserved converting to ordered dictionary."
78+
}
79+
if (-not $result['ArRaY']) {
80+
throw "Case sensitivity!"
81+
}

0 commit comments

Comments
 (0)