Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lila/src/main/scala/StringOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,25 @@ object StringOps:
def addQueryParams(url: String, params: Map[String, String]): String =
if params.isEmpty then url
else
val queryString = params // we could encode the key, and we should, but is it really necessary?
.map { (key, value) => s"$key=${urlencode(value)}" }
val (baseUrl, existingParams) = url.split("\\?", 2) match
case Array(base, query) =>
val parsed = query
.split("&")
.map:
_.split("=", 2) match
case Array(k, v) => k -> Some(v)
case Array(k) => k -> None
.toMap
(base, parsed)
case Array(base) => (base, Map.empty[String, Option[String]])

val mergedParams = existingParams ++ params.map { (k, v) => k -> Some(urlencode(v)) }

val queryString = mergedParams // we could encode the key, and we should, but is it really necessary?
.map: (key, value) =>
value.fold(key)(v => s"$key=$v")
.mkString("&")
s"$url${if url.contains("?") then "&" else "?"}$queryString"
s"$baseUrl?$queryString"

def removeGarbageChars(str: String) = removeChars(str, isGarbageChar)

Expand Down
55 changes: 55 additions & 0 deletions lila/src/test/scala/StringOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,58 @@ class StringTest extends munit.FunSuite:
line"""),
"multi\nline"
)

test("addQueryParam"):
assertEquals(
addQueryParam("https://example.com/path", "key", "value"),
"https://example.com/path?key=value"
)
assertEquals(
addQueryParam("https://example.com/path?key1", "key2", "value"),
"https://example.com/path?key1&key2=value"
)

test("addQueryParams"):
assertEquals(
addQueryParams("https://example.com/path", Map.empty),
"https://example.com/path"
)
assertEquals(
addQueryParams("https://example.com/path", Map("key1" -> "value1", "key2" -> "value2")),
"https://example.com/path?key1=value1&key2=value2"
)
assertEquals(
addQueryParams("https://example.com/path?key1=value1", Map("key2" -> "value2")),
"https://example.com/path?key1=value1&key2=value2"
)
assertEquals(
addQueryParams("https://example.com/path?key1=value1&key2=value2", Map("key3" -> "value3")),
"https://example.com/path?key1=value1&key2=value2&key3=value3"
)
assertEquals(
addQueryParams(
"https://example.com/path?encoded1=test%40example.com",
Map("encoded2" -> "multiple words")
),
"https://example.com/path?encoded1=test%40example.com&encoded2=multiple+words"
)

test("addQueryParams - replace existing param of same key"):
assertEquals(
addQueryParams("https://example.com/path?key=value1", Map("key" -> "value2")),
"https://example.com/path?key=value2"
)
assertEquals(
addQueryParams("https://example.com/path?key", Map("key" -> "value2")),
"https://example.com/path?key=value2"
)

test("addQueryParams - parse malformed params"):
assertEquals(
addQueryParams("https://example.com/path?&&&", Map("key1" -> "value1")),
"https://example.com/path?key1=value1"
)
assertEquals(
addQueryParams("https://example.com/path?=value1&key2=&=&&key3=value3", Map("key1" -> "value1")),
"https://example.com/path?&key2=&key3=value3&key1=value1"
)