From 75a010d974aac96e8c96d34e38590c219cdca900 Mon Sep 17 00:00:00 2001 From: adamw Date: Tue, 29 Aug 2017 12:59:00 +0200 Subject: Multipart support in Akka handler --- .../scala/com/softwaremill/sttp/BasicTests.scala | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'tests/src/test') diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index 9b64271..b2918fa 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -12,6 +12,7 @@ import akka.http.scaladsl.model.headers.CacheDirectives._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.directives.Credentials +import akka.util.ByteString import com.softwaremill.sttp.akkahttp.AkkaHttpSttpHandler import com.typesafe.scalalogging.StrictLogging import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures} @@ -26,6 +27,7 @@ import com.softwaremill.sttp.okhttp.{ OkHttpFutureClientHandler, OkHttpSyncClientHandler } +import scala.concurrent.ExecutionContext.Implicits.global import scala.language.higherKinds @@ -136,6 +138,20 @@ class BasicTests } ~ path("text") { getFromFile(textFile) } + } ~ pathPrefix("multipart") { + entity(as[akka.http.scaladsl.model.Multipart.FormData]) { fd => + complete { + fd.parts + .mapAsync(1) { p => + val fv = p.entity.dataBytes.runFold(ByteString())(_ ++ _) + fv.map(_.utf8String) + .map(v => + p.name + "=" + v + p.filename.fold("")(fn => s" ($fn)")) + } + .runFold(Vector.empty[String])(_ :+ _) + .map(v => v.mkString(", ")) + } + } } override def port = 51823 @@ -184,6 +200,7 @@ class BasicTests authTests() compressionTests() downloadFileTests() +// multipartTests() def parseResponseTests(): Unit = { name should "parse response as string" in { @@ -515,6 +532,23 @@ class BasicTests path.toFile should haveSameContentAs(textFile) } } + + def multipartTests(): Unit = { + val mp = sttp.post(uri"$endpoint/multipart") + + name should "send a multipart message" in { + val req = mp.multipartBody(multipart("p1", "v1"), multipart("p2", "v2")) + val resp = req.send().force() + resp.body should be("p1=v1, p2=v2") + } + + name should "send a multipart message with filenames" in { + val req = mp.multipartBody(multipart("p1", "v1").fileName("f1"), + multipart("p2", "v2").fileName("f2")) + val resp = req.send().force() + resp.body should be("p1=v1 (f1), p2=v2 (f2)") + } + } } override protected def afterAll(): Unit = { -- cgit v1.2.3