Skip to content

Commit

Permalink
Optimized Uri.withQuery (#637)
Browse files Browse the repository at this point in the history
* Optimized Uri.withQuery

* Improved UriSpec percent encoding tests

* Fix UriSpec test for Scala 2.12
  • Loading branch information
davidik1 authored Dec 16, 2024
1 parent 665cf65 commit 4554337
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ sealed abstract case class Uri(scheme: String, authority: Authority, path: Path,
/**
* Returns a copy of this Uri with the given query.
*/
def withQuery(query: Query): Uri = copy(rawQueryString = if (query.isEmpty) None else Some(query.toString))
def withQuery(query: Query): Uri =
createUnsafe(scheme, authority, path, if (query.isEmpty) None else Some(query.toString), fragment)

/**
* Returns a copy of this Uri with the given query string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,12 @@ class UriSpec extends AnyWordSpec with Matchers {
uri.withRawQueryString("param1=val%22ue1") shouldEqual Uri("http://host/path?param1=val%22ue1#fragment")
uri.withRawQueryString("param1=val\"ue1") shouldEqual Uri("http://host/path?param1=val%22ue1#fragment")

uri.withQuery(Query("param1=val\"ue1")) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment")
uri.withQuery(Query(("param1", "val\"ue1"))) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment")
uri.withQuery(Query(Some("param1=val\"ue1"))) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment")
val query = Query.newBuilder.+=("param1" -> "val\"ue1").result()
uri.withQuery(query) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment")

uri.withFragment("otherFragment") shouldEqual Uri("http://host/path?query#otherFragment")
}

Expand Down

0 comments on commit 4554337

Please sign in to comment.