From 0863a2af75583b976aee3171585685b2524d0874 Mon Sep 17 00:00:00 2001 From: adamw Date: Sat, 8 Jul 2017 21:27:52 +0200 Subject: Simple URI tests --- README.md | 8 ++++-- .../com/softwaremill/sttp/UriInterpolator.scala | 32 +++++++++++----------- .../main/scala/com/softwaremill/sttp/package.scala | 4 +++ .../softwaremill/sttp/UriInterpolatorTests.scala | 24 ++++++++++++++++ 4 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 tests/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala diff --git a/README.md b/README.md index 9a98f87..1081357 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,15 @@ The HTTP client for Scala that you always wanted ```scala +val user = "adamw" val state = "closed" -val request = sttp.get(uri"https://api.github.com/repos/adamw/elasticmq/issues?state=$state") +val sort: Option[String] = None +val request = sttp.get(uri"https://api.github.com/repos/$user/elasticmq/issues?state=$state&sort=$sort") val response = request.send(responseAsString("utf-8")) println(response.header("Content-Length")) // has type Option[String] -println(response.body) // has type String +println(response.body) // has type String as specified when sending the request ``` ## Goals of the project @@ -37,7 +39,7 @@ import com.softwaremill.sttp._ To send requests, you will also need a backend. A default, synchronous backend based on Java's `HttpURLConnection` is provided out-of-the box. An implicit value needs to be in scope to invoke `send()` (however it's possible to -create requests without any implicit backend in scope): +create request descriptions without any implicit backend in scope): ```scala implicit val handler = HttpConnectionSttpHandler diff --git a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala index abb2327..e329630 100644 --- a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala +++ b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala @@ -1,6 +1,8 @@ package com.softwaremill.sttp -// from https://gist.github.com/teigen/5865923 +import java.net.URI + +// based on https://gist.github.com/teigen/5865923 object UriInterpolator { private val unreserved = { @@ -9,24 +11,22 @@ object UriInterpolator { alphanum ++ mark } - implicit class UriContext(val sc:StringContext) extends AnyVal { - def uri(args:String*) = { - val strings = sc.parts.iterator - val expressions = args.iterator - val sb = new StringBuffer(strings.next()) + def interpolate(sc: StringContext, args: String*): URI = { + val strings = sc.parts.iterator + val expressions = args.iterator + val sb = new StringBuffer(strings.next()) - while(strings.hasNext){ - for(c <- expressions.next()){ - if(unreserved(c)) - sb.append(c) - else for(b <- c.toString.getBytes("UTF-8")){ - sb.append("%") - sb.append("%02X".format(b)) - } + while(strings.hasNext){ + for(c <- expressions.next()){ + if(unreserved(c)) + sb.append(c) + else for(b <- c.toString.getBytes("UTF-8")){ + sb.append("%") + sb.append("%02X".format(b)) } - sb.append(strings.next()) } - sb.toString + sb.append(strings.next()) } + new URI(sb.toString) } } diff --git a/core/src/main/scala/com/softwaremill/sttp/package.scala b/core/src/main/scala/com/softwaremill/sttp/package.scala index 52d06f0..3321aa7 100644 --- a/core/src/main/scala/com/softwaremill/sttp/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/package.scala @@ -178,4 +178,8 @@ package object sttp { private val MultipartFormDataContentType = "multipart/form-data" private def contentTypeWithEncoding(ct: String, enc: String) = s"$ct; charset=$enc" + + implicit class UriContext(val sc: StringContext) extends AnyVal { + def uri(args:String*): URI = UriInterpolator.interpolate(sc, args: _*) + } } diff --git a/tests/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala b/tests/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala new file mode 100644 index 0000000..57bb6af --- /dev/null +++ b/tests/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala @@ -0,0 +1,24 @@ +package com.softwaremill.sttp + +import java.net.URI + +import org.scalatest.{FlatSpec, Matchers} + +class UriInterpolatorTests extends FlatSpec with Matchers { + val v1 = "y" + val v2 = "a c" + val v2encoded = "a%20c" + + val testData: List[(URI, String)] = List( + (uri"http://example.com", "http://example.com"), + (uri"http://example.com?x=y", "http://example.com?x=y"), + (uri"http://example.com?x=$v1", s"http://example.com?x=$v1"), + (uri"http://example.com?x=$v2", s"http://example.com?x=$v2encoded") + ) + + for (((interpolated, expected), i) <- testData.zipWithIndex) { + it should s"interpolate to $expected ($i)" in { + interpolated should be (new URI(expected)) + } + } +} -- cgit v1.2.3