+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"
}
//#
}
}

View file

@ -0,0 +1,30 @@
.. _-extractStrictEntity-:
extractStrictEntity
===================
Signature
---------
.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala
:snippet: extractStrictEntity
Description
-----------
Extracts the strict http entity as ``HttpEntity.Strict`` from the :class:`RequestContext`.
A timeout parameter is given and if the stream isn't completed after the timeout, the directive will be failed.
.. warning::
The directive will read the request entity into memory within the size limit(8M by default) and effectively disable streaming.
The size limit can be configured globally with ``akka.http.parsing.max-content-length`` or
overridden by wrapping with :ref:`-withSizeLimit-` or :ref:`-withoutSizeLimit-` directive.
Example
-------
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala
:snippet: extractStrictEntity-example

View file

@ -20,6 +20,7 @@ a single value or a tuple of values.
* :ref:`-extractDataBytes-`
* :ref:`-extractExecutionContext-`
* :ref:`-extractMaterializer-`
* :ref:`-extractStrictEntity-`
* :ref:`-extractLog-`
* :ref:`-extractRequest-`
* :ref:`-extractRequestContext-`
@ -45,6 +46,7 @@ Transforming the Request(Context)
* :ref:`-withMaterializer-`
* :ref:`-withLog-`
* :ref:`-withSettings-`
* :ref:`-toStrictEntity-`
.. _Response Transforming Directives:
@ -98,6 +100,7 @@ Alphabetically
extractExecutionContext
extractDataBytes
extractMaterializer
extractStrictEntity
extractLog
extractRequest
extractRequestContext
@ -124,6 +127,7 @@ Alphabetically
recoverRejections
recoverRejectionsWith
textract
toStrictEntity
tprovide
withExecutionContext
withMaterializer

View file

@ -0,0 +1,30 @@
.. _-toStrictEntity-:
toStrictEntity
==============
Signature
---------
.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala
:snippet: toStrictEntity
Description
-----------
Transforms the request entity to strict entity before it is handled by the inner route.
A timeout parameter is given and if the stream isn't completed after the timeout, the directive will be failed.
.. warning::
The directive will read the request entity into memory within the size limit(8M by default) and effectively disable streaming.
The size limit can be configured globally with ``akka.http.parsing.max-content-length`` or
overridden by wrapping with :ref:`-withSizeLimit-` or :ref:`-withoutSizeLimit-` directive.
Example
-------
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala
:snippet: toStrictEntity-example