+htp #18929 add withSizeLimit directive (#20760)

withSizeLimit and withoutSizeLimit directives added
This commit is contained in:
Michal Sitko 2016-06-28 12:14:44 +02:00 committed by Konrad Malawski
parent e00a86271a
commit 72f8544efd
15 changed files with 487 additions and 1 deletions

View file

@ -22,6 +22,7 @@ class MiscDirectivesExamplesSpec extends RoutingSpec {
responseAs[String] shouldEqual "Client's ip is 192.168.3.12"
}
}
"rejectEmptyResponse-example" in {
val route = rejectEmptyResponse {
path("even" / IntNumber) { i =>
@ -42,6 +43,7 @@ class MiscDirectivesExamplesSpec extends RoutingSpec {
responseAs[String] shouldEqual "Number 28 is even."
}
}
"requestEntityEmptyPresent-example" in {
val route =
requestEntityEmpty {
@ -59,6 +61,7 @@ class MiscDirectivesExamplesSpec extends RoutingSpec {
responseAs[String] shouldEqual "request entity empty"
}
}
"selectPreferredLanguage-example" in {
val request = Get() ~> `Accept-Language`(
Language("en-US"),
@ -78,6 +81,7 @@ class MiscDirectivesExamplesSpec extends RoutingSpec {
}
} ~> check { responseAs[String] shouldEqual "de-DE" }
}
"validate-example" in {
val route =
extractUri { uri =>
@ -94,4 +98,84 @@ class MiscDirectivesExamplesSpec extends RoutingSpec {
rejection shouldEqual ValidationRejection("Path too long: '/abcdefghijkl'", None)
}
}
"withSizeLimit-example" in {
val route = withSizeLimit(500) {
entity(as[String]) { _
complete(HttpResponse())
}
}
// tests:
def entityOfSize(size: Int) =
HttpEntity(ContentTypes.`text/plain(UTF-8)`, "0" * size)
Post("/abc", entityOfSize(500)) ~> route ~> check {
status shouldEqual StatusCodes.OK
}
Post("/abc", entityOfSize(501)) ~> Route.seal(route) ~> check {
status shouldEqual StatusCodes.BadRequest
}
}
"withSizeLimit-execution-moment-example" in {
val route = withSizeLimit(500) {
complete(HttpResponse())
}
// tests:
def entityOfSize(size: Int) =
HttpEntity(ContentTypes.`text/plain(UTF-8)`, "0" * size)
Post("/abc", entityOfSize(500)) ~> route ~> check {
status shouldEqual StatusCodes.OK
}
Post("/abc", entityOfSize(501)) ~> route ~> check {
status shouldEqual StatusCodes.OK
}
}
"withSizeLimit-nested-example" in {
val route =
withSizeLimit(500) {
withSizeLimit(800) {
entity(as[String]) { _
complete(HttpResponse())
}
}
}
// tests:
def entityOfSize(size: Int) =
HttpEntity(ContentTypes.`text/plain(UTF-8)`, "0" * size)
Post("/abc", entityOfSize(800)) ~> route ~> check {
status shouldEqual StatusCodes.OK
}
Post("/abc", entityOfSize(801)) ~> Route.seal(route) ~> check {
status shouldEqual StatusCodes.BadRequest
}
}
"withoutSizeLimit-example" in {
val route =
withoutSizeLimit {
entity(as[String]) { _
complete(HttpResponse())
}
}
// tests:
def entityOfSize(size: Int) =
HttpEntity(ContentTypes.`text/plain(UTF-8)`, "0" * size)
// will work even if you have configured akka.http.parsing.max-content-length = 500
Post("/abc", entityOfSize(501)) ~> route ~> check {
status shouldEqual StatusCodes.OK
}
}
}