+htp #20881 add toStrictEntity and extractStrictEntity directive (#20953)

This commit is contained in:
Hawstein 2016-07-22 17:33:30 +08:00 committed by Konrad Malawski
parent e0d73187bd
commit 6fb2d176a1
10 changed files with 279 additions and 1 deletions

View file

@ -826,5 +826,40 @@ class BasicDirectivesExamplesSpec extends RoutingSpec {
}
//#
}
"extractStrictEntity-example" in {
//#extractStrictEntity-example
import scala.concurrent.duration._
val route = extractStrictEntity(3.seconds) { entity =>
complete(entity.data.utf8String)
}
// tests:
val dataBytes = Source.fromIterator(() Iterator.range(1, 10).map(x ByteString(x.toString)))
Post("/", HttpEntity(ContentTypes.`text/plain(UTF-8)`, data = dataBytes)) ~> route ~> check {
responseAs[String] shouldEqual "123456789"
}
//#
}
"toStrictEntity-example" in {
//#toStrictEntity-example
import scala.concurrent.duration._
val route = toStrictEntity(3.seconds) {
extractRequest { req =>
req.entity match {
case strict: HttpEntity.Strict =>
complete(s"Request entity is strict, data=${strict.data.utf8String}")
case _ =>
complete("Ooops, request entity is not strict!")
}
}
}
// tests:
val dataBytes = Source.fromIterator(() Iterator.range(1, 10).map(x ByteString(x.toString)))
Post("/", HttpEntity(ContentTypes.`text/plain(UTF-8)`, data = dataBytes)) ~> route ~> check {
responseAs[String] shouldEqual "Request entity is strict, data=123456789"
}
//#
}
}