Skip to content

Commit

Permalink
fix: Handling non-string parameters by buildUrl (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
RadoslawZambrowski authored Oct 13, 2023
1 parent 14987e0 commit 1ec2091
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/components/CacheFS.brs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ function CacheFS(folder = "kopytkoCache" as String) as Object

' Clears all data from cachefs under the specific directory
' @returns {Boolean} true if all directory data is successfully removed
prototype.clear = sub () as Boolean
prototype.clear = function () as Boolean
if (m._fileSystem.delete(m._PREFIX + m._folder))
return m._createFolder()
end if

return false
end sub
end function

' @private
prototype._createFolder = sub () as Boolean
prototype._createFolder = function () as Boolean
return m._fileSystem.createDirectory(m._PREFIX + m._folder)
end sub
end function

' @private
prototype._hash = function (text as String) as String
Expand Down
14 changes: 5 additions & 9 deletions src/components/_tests/buildUrl.test.brs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ function TestSuite__buildUrl() as Object
ts.addTest("returns the proper URL for multiple params", function (ts as Object) as String
' Given
path = "/example-path"
params = { paramOne: "valueOne", paramTwo: "valueTwo" }
' Because "for each" loop for AAs doesn't guarantee the order, the result may be one of the following
expectedUrls = [
"/example-path?paramone=valueOne&paramtwo=valueTwo",
"/example-path?paramtwo=valueTwo&paramone=valueOne",
]
params = { paramOne: "valueOne", paramTwo: 2, paramThree: false }
expectedUrl = "/example-path?paramone=valueOne&paramthree=false&paramtwo=2"

' When
result = buildUrl(path, params)

' Then
return ts.assertArrayContains(expectedUrls, result)
return ts.assertEqual(result, expectedUrl)
end function)

ts.addTest("returns URL with encoded space sign", function (ts as Object) as String
Expand Down Expand Up @@ -82,10 +78,10 @@ function TestSuite__buildUrl() as Object
return ts.assertEqual(result, expectedUrl)
end function)

ts.addTest("ignores empty string parameter", function (ts as Object) as String
ts.addTest("ignores parameters that can't be converted to non-empty string", function (ts as Object) as String
' Given
path = "/example-path"
params = { parameter: "" }
params = { param1: "", param2: ["p2"], param3: { p3: "p3"}, param4: CreateObject("roSGNode", "Node"), param5: invalid }
expectedUrl = "/example-path"

' When
Expand Down
14 changes: 11 additions & 3 deletions src/components/buildUrl.brs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
' ?buildUrl("http://myurl.com", { queryString: "123" })
' ' prints http://myurl.com?queryString=123
' @param {String} path
' @param {Object} [params=Invalid] - The AA of strings.
' @param {Object} [params=Invalid] - The AA of values convertible to string.
' @returns {String}
function buildUrl(path as String, params = Invalid as Object) as String
' It might be encoded already. To avoid encoding encoded url it needs to be decoded first.
Expand All @@ -14,10 +14,18 @@ function buildUrl(path as String, params = Invalid as Object) as String
end if

paramParts = []
for each paramKey in params
value = params[paramKey]
for each paramKey in params.keys()
rawValue = params[paramKey]
value = Invalid

if (rawValue <> Invalid AND GetInterface(rawValue, "ifToStr") <> Invalid)
value = rawValue.toStr()
end if

if (value <> Invalid AND value <> "")
paramParts.push(paramKey.encodeUriComponent() + "=" + value.encodeUriComponent())
else
?"buildUrl: '";paramKey;"' param is ignored because it can't be converted to string"
end if
end for

Expand Down

0 comments on commit 1ec2091

Please sign in to comment.