diff options
author | adamw <adam@warski.org> | 2017-11-21 17:39:19 +0100 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-11-21 17:42:13 +0100 |
commit | 6b9463012c12bc8c9cd5f3280b9c8aa2ff5bccab (patch) | |
tree | fcf6ca2bc22163222f23dafa55a460dd22a23b1d /core | |
parent | d47c3127e4e191ad3bed0b5366d7abd39973a838 (diff) | |
download | sttp-6b9463012c12bc8c9cd5f3280b9c8aa2ff5bccab.tar.gz sttp-6b9463012c12bc8c9cd5f3280b9c8aa2ff5bccab.tar.bz2 sttp-6b9463012c12bc8c9cd5f3280b9c8aa2ff5bccab.zip |
#48: %-encode plus in queries
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/Uri.scala | 12 | ||||
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/UriTests.scala | 3 |
2 files changed, 11 insertions, 4 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/Uri.scala b/core/src/main/scala/com/softwaremill/sttp/Uri.scala index 932771b..0c7ad08 100644 --- a/core/src/main/scala/com/softwaremill/sttp/Uri.scala +++ b/core/src/main/scala/com/softwaremill/sttp/Uri.scala @@ -135,7 +135,9 @@ case class Uri(scheme: String, e match { case QueryFragmentEncoding.All => URLEncoder.encode(s, "UTF-8") case QueryFragmentEncoding.Standard => - encode(Rfc3986.QueryNoStandardDelims, spaceAsPlus = true)(s) + encode(Rfc3986.QueryNoStandardDelims, + spaceAsPlus = true, + encodePlus = true)(s) case QueryFragmentEncoding.Relaxed => encode(Rfc3986.Query, spaceAsPlus = true)(s) } @@ -161,13 +163,17 @@ case class Uri(scheme: String, /** * @param spaceAsPlus In the query, space is encoded as a `+`. In other * contexts, it should be %-encoded as `%20`. + * @param encodePlus Should `+` (which is the encoded form of space + * in the query) be %-encoded. */ private def encode(allowedCharacters: Set[Char], - spaceAsPlus: Boolean = false)(s: String): String = { + spaceAsPlus: Boolean = false, + encodePlus: Boolean = false)(s: String): String = { val sb = new StringBuilder() // based on https://gist.github.com/teigen/5865923 for (c <- s) { - if (allowedCharacters(c)) sb.append(c) + if (c == '+' && encodePlus) sb.append("%2B") // #48 + else if (allowedCharacters(c)) sb.append(c) else if (c == ' ' && spaceAsPlus) sb.append('+') else { for (b <- c.toString.getBytes("UTF-8")) { diff --git a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala index 194ae20..9f5a0fd 100644 --- a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala @@ -98,7 +98,8 @@ class UriTests extends FunSuite with Matchers { valueEncoding = QueryFragmentEncoding.Relaxed)) -> "k1%26=v1&", List(QF.Plain("ą/ę&+;?", encoding = QueryFragmentEncoding.Relaxed)) -> "%C4%85/%C4%99&+;?", List(QF.KeyValue("k", "v1,v2", valueEncoding = QueryFragmentEncoding.All)) -> "k=v1%2Cv2", - List(QF.KeyValue("k", "v1,v2")) -> "k=v1,v2" + List(QF.KeyValue("k", "v1,v2")) -> "k=v1,v2", + List(QF.KeyValue("k", "+1234")) -> "k=%2B1234" ) for { |