diff options
author | adamw <adam@warski.org> | 2017-07-08 20:01:53 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-08 20:01:53 +0200 |
commit | f5566b659c4d52de0ff157713fe920671f23a147 (patch) | |
tree | ded526e18d49a0715c3f6efa58204a4c353d30d8 /tests/src/test/scala/com/softwaremill | |
parent | a2f3939a89c8b291c4697e822bb931703b3bd3ba (diff) | |
download | sttp-f5566b659c4d52de0ff157713fe920671f23a147.tar.gz sttp-f5566b659c4d52de0ff157713fe920671f23a147.tar.bz2 sttp-f5566b659c4d52de0ff157713fe920671f23a147.zip |
Setting bodies in the Akka impl
Diffstat (limited to 'tests/src/test/scala/com/softwaremill')
-rw-r--r-- | tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index 0f0f076..e4ee030 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -1,6 +1,8 @@ package com.softwaremill.sttp +import java.io.ByteArrayInputStream import java.net.URI +import java.nio.ByteBuffer import akka.stream.ActorMaterializer import akka.actor.ActorSystem @@ -10,6 +12,7 @@ import com.softwaremill.sttp.akkahttp.AkkaHttpSttpHandler import com.typesafe.scalalogging.StrictLogging import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures} import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} +import better.files._ import scala.concurrent.Future import scala.language.higherKinds @@ -21,13 +24,13 @@ class BasicTests extends FlatSpec with Matchers with BeforeAndAfterAll with Scal path("echo") { get { parameterMap { params => - complete(s"GET /echo ${paramsToString(params)}") + complete(List("GET", "/echo", paramsToString(params)).filter(_.nonEmpty).mkString(" ")) } } ~ post { parameterMap { params => entity(as[String]) { body: String => - complete(s"POST /echo ${paramsToString(params)} $body") + complete(List("POST", "/echo", paramsToString(params), body).filter(_.nonEmpty).mkString(" ")) } } } @@ -69,5 +72,52 @@ class BasicTests extends FlatSpec with Matchers with BeforeAndAfterAll with Scal val fc = forceResponse.force(response).body fc should be ("GET /echo p1=v1 p2=v2") } + + val postEcho = sttp.post(new URI(endpoint + "/echo")) + val testBody = "this is the body" + val testBodyBytes = testBody.getBytes("UTF-8") + val expectedPostEchoResponse = "POST /echo this is the body" + + name should "post a string" in { + val response = postEcho.data(testBody).send(responseAsString) + val fc = forceResponse.force(response).body + fc should be (expectedPostEchoResponse) + } + + name should "post a byte array" in { + val response = postEcho.data(testBodyBytes).send(responseAsString) + val fc = forceResponse.force(response).body + fc should be (expectedPostEchoResponse) + } + + name should "post an input stream" in { + val response = postEcho.data(new ByteArrayInputStream(testBodyBytes)).send(responseAsString) + val fc = forceResponse.force(response).body + fc should be (expectedPostEchoResponse) + } + + name should "post a byte buffer" in { + val response = postEcho.data(ByteBuffer.wrap(testBodyBytes)).send(responseAsString) + val fc = forceResponse.force(response).body + fc should be (expectedPostEchoResponse) + } + + name should "post a file" in { + val f = File.newTemporaryFile().write(testBody) + try { + val response = postEcho.data(f.toJava).send(responseAsString) + val fc = forceResponse.force(response).body + fc should be(expectedPostEchoResponse) + } finally f.delete() + } + + name should "post a path" in { + val f = File.newTemporaryFile().write(testBody) + try { + val response = postEcho.data(f.toJava.toPath).send(responseAsString) + val fc = forceResponse.force(response).body + fc should be(expectedPostEchoResponse) + } finally f.delete() + } } } |