diff options
author | Piotr Buda <piotr.buda@softwaremill.com> | 2017-07-31 11:59:08 +0200 |
---|---|---|
committer | Piotr Buda <piotr.buda@softwaremill.com> | 2017-08-04 16:37:48 +0200 |
commit | e6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf (patch) | |
tree | c3fda860423c4154bdc73f26f67fe332ce4b1cc6 /tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala | |
parent | 489e591672257d19b3a07198d4d6c9e21be601c7 (diff) | |
download | sttp-e6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf.tar.gz sttp-e6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf.tar.bz2 sttp-e6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf.zip |
#7: Add asFile and asPath responses
Diffstat (limited to 'tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala')
-rw-r--r-- | tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala | 115 |
1 files changed, 112 insertions, 3 deletions
diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index 36c82ef..28e210f 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -1,7 +1,8 @@ package com.softwaremill.sttp -import java.io.ByteArrayInputStream +import java.io.{ByteArrayInputStream, IOException} import java.nio.ByteBuffer +import java.nio.file.Paths import java.time.{ZoneId, ZonedDateTime} import akka.http.scaladsl.coding.{Deflate, Gzip, NoCoding} @@ -14,7 +15,7 @@ import akka.http.scaladsl.server.directives.Credentials import com.softwaremill.sttp.akkahttp.AkkaHttpSttpHandler import com.typesafe.scalalogging.StrictLogging import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures} -import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} +import org.scalatest.{path => _, _} import better.files._ import com.softwaremill.sttp.asynchttpclient.cats.CatsAsyncHttpClientHandler import com.softwaremill.sttp.asynchttpclient.future.FutureAsyncHttpClientHandler @@ -32,14 +33,27 @@ class BasicTests with Matchers with BeforeAndAfterAll with ScalaFutures + with OptionValues with StrictLogging with IntegrationPatience with TestHttpServer - with ForceWrapped { + with ForceWrapped + with BeforeAndAfterEach { + + override def afterEach() { + val file = File(outPath) + if (file.exists) file.delete() + } private def paramsToString(m: Map[String, String]): String = m.toList.sortBy(_._1).map(p => s"${p._1}=${p._2}").mkString(" ") + private val textFile = + new java.io.File("tests/src/test/resources/textfile.txt") + private val binaryFile = + new java.io.File("tests/src/test/resources/binaryfile.jpg") + private val outPath = Paths.get("out") + override val serverRoutes: Route = pathPrefix("echo") { pathPrefix("form_params") { @@ -115,6 +129,12 @@ class BasicTests encodeResponseWith(Gzip, Deflate, NoCoding) { complete("I'm compressed!") } + } ~ pathPrefix("download") { + path("binary") { + getFromFile(binaryFile) + } ~ path("text") { + getFromFile(textFile) + } } override def port = 51823 @@ -402,6 +422,95 @@ class BasicTests resp.body should be(decompressedBody) } } + + def downloadFileTests(): Unit = { + import CustomMatchers._ + + name should "download a binary file using asFile" in { + val file = outPath.resolve("binaryfile.jpg").toFile + val req = + sttp.get(uri"$endpoint/download/binary").response(asFile(file)) + val resp = req.send().force() + + resp.body shouldBe file + file should exist + file should haveSameContentAs(binaryFile) + } + + name should "download a text file using asFile" in { + val file = outPath.resolve("textfile.txt").toFile + val req = + sttp.get(uri"$endpoint/download/text").response(asFile(file)) + val resp = req.send().force() + + resp.body shouldBe file + file should exist + file should haveSameContentAs(textFile) + } + + name should "download a binary file using asPath" in { + val path = outPath.resolve("binaryfile.jpg") + val req = + sttp.get(uri"$endpoint/download/binary").response(asPath(path)) + val resp = req.send().force() + + resp.body shouldBe path + path.toFile should exist + path.toFile should haveSameContentAs(binaryFile) + } + + name should "download a text file using asPath" in { + val path = outPath.resolve("textfile.txt") + val req = + sttp.get(uri"$endpoint/download/text").response(asPath(path)) + val resp = req.send().force() + + resp.body shouldBe path + path.toFile should exist + path.toFile should haveSameContentAs(textFile) + } + + name should "fail at trying to save file to a restricted location" in { + val path = Paths.get("/").resolve("textfile.txt") + val req = + sttp.get(uri"$endpoint/download/text").response(asPath(path)) + val caught = intercept[IOException] { + req.send().force() + } + + caught.getMessage shouldBe "Permission denied" + } + + name should "fail when file exists and overwrite flag is false" in { + val path = outPath.resolve("textfile.txt") + path.toFile.getParentFile.mkdirs() + path.toFile.createNewFile() + val req = + sttp.get(uri"$endpoint/download/text").response(asPath(path)) + + val caught = intercept[IOException] { + req.send().force() + } + + caught.getMessage shouldBe s"File ${path.toFile.getAbsolutePath} exists - overwriting prohibited" + + } + + name should "not fail when file exists and overwrite flag is true" in { + val path = outPath.resolve("textfile.txt") + path.toFile.getParentFile.mkdirs() + path.toFile.createNewFile() + val req = + sttp + .get(uri"$endpoint/download/text") + .response(asPath(path, overwrite = true)) + val resp = req.send().force() + + resp.body shouldBe path + path.toFile should exist + path.toFile should haveSameContentAs(textFile) + } + } } override protected def afterAll(): Unit = { |