From 985e3b1c2f5110642286676e47e617db70b328ce Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Tue, 10 Nov 2015 13:51:13 +0100 Subject: [PATCH] =str #18902 in tests (due to grouped+Sink.head) empty response stream would throw --- .../testkit/RouteTestResultComponent.scala | 12 ++++++-- .../server/StreamingResponseSpecs.scala | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 akka-http-tests/src/test/scala/akka/http/scaladsl/server/StreamingResponseSpecs.scala diff --git a/akka-http-testkit/src/main/scala/akka/http/scaladsl/testkit/RouteTestResultComponent.scala b/akka-http-testkit/src/main/scala/akka/http/scaladsl/testkit/RouteTestResultComponent.scala index 783476203e..609e6f8236 100644 --- a/akka-http-testkit/src/main/scala/akka/http/scaladsl/testkit/RouteTestResultComponent.scala +++ b/akka-http-testkit/src/main/scala/akka/http/scaladsl/testkit/RouteTestResultComponent.scala @@ -5,9 +5,11 @@ package akka.http.scaladsl.testkit import java.util.concurrent.CountDownLatch +import akka.dispatch.ExecutionContexts + import scala.collection.immutable import scala.concurrent.duration._ -import scala.concurrent.ExecutionContext +import scala.concurrent.{ Await, ExecutionContext } import akka.stream.Materializer import akka.stream.scaladsl._ import akka.http.scaladsl.model.HttpEntity.ChunkStreamPart @@ -95,7 +97,11 @@ trait RouteTestResultComponent { private def failNeitherCompletedNorRejected(): Nothing = failTest("Request was neither completed nor rejected within " + timeout) - private def awaitAllElements[T](data: Source[T, _]): immutable.Seq[T] = - data.grouped(100000).runWith(Sink.head).awaitResult(timeout) + private def awaitAllElements[T](data: Source[T, _]): immutable.Seq[T] = { + data.grouped(100000).runWith(Sink.head).recover({ + case e: NoSuchElementException ⇒ Nil + })(ExecutionContexts.sameThreadExecutionContext) + .awaitResult(timeout) + } } } \ No newline at end of file diff --git a/akka-http-tests/src/test/scala/akka/http/scaladsl/server/StreamingResponseSpecs.scala b/akka-http-tests/src/test/scala/akka/http/scaladsl/server/StreamingResponseSpecs.scala new file mode 100644 index 0000000000..ca8ce7d645 --- /dev/null +++ b/akka-http-tests/src/test/scala/akka/http/scaladsl/server/StreamingResponseSpecs.scala @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2009-2015 Typesafe Inc. + */ + +package akka.http.scaladsl.server + +import akka.http.scaladsl.model.{ ContentTypes, HttpEntity, HttpResponse, StatusCodes } +import akka.stream.scaladsl.Source +import akka.util.ByteString + +class StreamingResponseSpecs extends RoutingSpec { + + "streaming ByteString responses" should { + "should render empty string if stream was empty" in { + import StatusCodes._ + + val src = Source.empty[ByteString] + val entity = HttpEntity.Chunked.fromData(ContentTypes.`application/json`, src) + val response = HttpResponse(status = StatusCodes.OK, entity = entity) + val route = complete(response) + + Get() ~> route ~> check { + status should ===(OK) + responseAs[String] shouldEqual "" + } + } + + } +} \ No newline at end of file