diff --git a/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/RangeDirectivesExamplesSpec.scala b/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/RangeDirectivesExamplesSpec.scala index a11f439f9d..9d5a35e15d 100644 --- a/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/RangeDirectivesExamplesSpec.scala +++ b/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/RangeDirectivesExamplesSpec.scala @@ -1,38 +1,59 @@ -///* -// * Copyright (C) 2009-2014 Typesafe Inc. -// */ -// -//package docs.http.scaladsl.server -//package directives -// -//import akka.http.javadsl.model.headers.RangeUnit -//import akka.http.scaladsl.model.Multipart.ByteRanges.BodyPart -//import akka.http.scaladsl.model._ -//import headers._ -// -//class RangeDirectivesExamplesSpec extends RoutingSpec { -// -// "withRangeSupport" in { -// val route = -// withRangeSupport { -// complete("ABCDEFGH") -// } -// -// Get() ~> addHeader(Range(ByteRange(3, 4))) ~> route ~> check { -// headers should contain(`Content-Range`(ContentRange(3, 4, 8))) -// status shouldEqual StatusCodes.PartialContent -// responseAs[String] shouldEqual "DE" -// } -// -// Get() ~> addHeader(Range(ByteRange(0, 1), ByteRange(1, 2), ByteRange(6, 7))) ~> route ~> check { -// headers must not(contain(like[HttpHeader] { case `Content-Range`(_, _) ⇒ ok })) -// responseAs[MultipartByteRanges] must beLike { -// case MultipartByteRanges( -// BodyPart(entity1, `Content-Range`(RangeUnits.Bytes, range1) +: _) +: -// BodyPart(entity2, `Content-Range`(RangeUnits.Bytes, range2) +: _) +: Seq() -// ) ⇒ entity1.asString shouldEqual "ABC" and range1 shouldEqual ContentRange(0, 2, 8) and -// entity2.asString shouldEqual "GH" and range2 shouldEqual ContentRange(6, 7, 8) -// } -// } -// } -//} +/* + * Copyright (C) 2009-2014 Typesafe Inc. + */ + +package docs.http.scaladsl.server +package directives + +import akka.http.scaladsl.model._ +import com.typesafe.config.{ ConfigFactory, Config } +import akka.util.ByteString + +import headers._ +import scala.concurrent.Await +import scala.concurrent.duration._ + +class RangeDirectivesExamplesSpec extends RoutingSpec { + + override def testConfig: Config = + ConfigFactory.parseString("akka.http.routing.range-coalescing-threshold=2").withFallback(super.testConfig) + + "withRangeSupport" in { + val route = + withRangeSupport { + complete("ABCDEFGH") + } + + Get() ~> addHeader(Range(ByteRange(3, 4))) ~> route ~> check { + headers should contain(`Content-Range`(ContentRange(3, 4, 8))) + status shouldEqual StatusCodes.PartialContent + responseAs[String] shouldEqual "DE" + } + + + // we set "akka.http.routing.range-coalescing-threshold = 2" + // above to make sure we get two BodyParts + Get() ~> addHeader(Range(ByteRange(0, 1), ByteRange(1, 2), ByteRange(6, 7))) ~> route ~> check { + headers.collectFirst { case `Content-Range`(_, _) => true } shouldBe None + val responseF = responseAs[Multipart.ByteRanges].parts + .runFold[List[Multipart.ByteRanges.BodyPart]](Nil)((acc, curr) => curr :: acc) + + val response = Await.result(responseF, 3.seconds).reverse + + response should have length 2 + + val part1 = response(0) + part1.contentRange === ContentRange(0, 2, 8) + part1.entity should matchPattern { + case HttpEntity.Strict(_, bytes) if bytes.utf8String == "ABC" => + } + + val part2 = response(1) + part2.contentRange === ContentRange(6, 7, 8) + part2.entity should matchPattern { + case HttpEntity.Strict(_, bytes) if bytes.utf8String == "GH" => + } + } + } + +} diff --git a/akka-docs-dev/rst/scala/http/routing-dsl/directives/range-directives/withRangeSupport.rst b/akka-docs-dev/rst/scala/http/routing-dsl/directives/range-directives/withRangeSupport.rst index 192aef2492..b078d9efd4 100644 --- a/akka-docs-dev/rst/scala/http/routing-dsl/directives/range-directives/withRangeSupport.rst +++ b/akka-docs-dev/rst/scala/http/routing-dsl/directives/range-directives/withRangeSupport.rst @@ -42,4 +42,5 @@ See also: https://tools.ietf.org/html/rfc7233 Example ------- -TODO (../../../../code/docs/http/scaladsl/server/directives/RangeDirectivesExamplesSpec.scala) +.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/RangeDirectivesExamplesSpec.scala + :snippet: withRangeSupport