URIBuilder handles special characters

This commit is contained in:
Pavel Marek 2024-02-16 14:19:50 +01:00
parent 0e2b1d64f8
commit c5cc2dc448
2 changed files with 17 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package org.enso.downloader.http
import java.net.URI
import java.net.{URI, URLEncoder}
import java.nio.charset.StandardCharsets
/** A simple immutable builder for URIs based on URLs.
*
@ -25,13 +26,15 @@ case class URIBuilder private (uri: URI) {
* The query is appended at the end.
*/
def addQuery(key: String, value: String): URIBuilder = {
val scheme = uri.getScheme
val authority = uri.getAuthority
val path = uri.getPath
val query = if (uri.getQuery == null) "" else uri.getQuery + "&"
val fragment = uri.getFragment
val newQuery = query + key + "=" + value
val newUri = new URI(scheme, authority, path, newQuery, fragment)
val scheme = uri.getScheme
val authority = uri.getAuthority
val path = uri.getPath
val query = if (uri.getQuery == null) "" else uri.getQuery + "&"
val fragment = uri.getFragment
val encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8)
val encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8)
val newQuery = query + encodedKey + "=" + encodedValue
val newUri = new URI(scheme, authority, path, newQuery, fragment)
copy(newUri)
}

View File

@ -16,6 +16,12 @@ class URIBuilderSpec extends AnyWordSpec with Matchers {
val uri = bldr.addQuery("foo", "bar").addQuery("baz", "qux").build()
uri.toString mustEqual "http://google.com?foo=bar&baz=qux"
}
"Handle non-standard symbols in queries" in {
val bldr = URIBuilder.fromUri("http://google.com")
val uri = bldr.addQuery("foo", "bar baz").addQuery("baz", "qux").build()
uri.toString mustEqual "http://google.com?foo=bar+baz&baz=qux"
}
}
}