-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fixes #2084
- Loading branch information
Showing
7 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...s/src/test/scala/akka/http/scaladsl/server/DiscardEntityDefaultExceptionHandlerSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.http.scaladsl.server | ||
|
||
import akka.http.scaladsl.model.ContentTypes.`text/plain(UTF-8)` | ||
import akka.http.scaladsl.model.HttpEntity | ||
import akka.http.scaladsl.model.StatusCodes.InternalServerError | ||
import akka.stream.scaladsl.Source | ||
import akka.util.ByteString | ||
import org.scalatest.concurrent.Eventually._ | ||
import org.scalatest.concurrent.ScalaFutures | ||
|
||
import scala.concurrent.Future | ||
|
||
class DiscardEntityDefaultExceptionHandlerSpec extends RoutingSpec with ScalaFutures { | ||
|
||
private val route = concat( | ||
path("crash") { | ||
throw new RuntimeException("BOOM!") | ||
}, path("crashAfterConsuming") { | ||
extractRequestEntity { entity ⇒ | ||
val future: Future[String] = entity.dataBytes.runFold(ByteString.empty)(_ ++ _).map(_ ⇒ throw new RuntimeException("KABOOM!")) | ||
complete(future) | ||
} | ||
} | ||
) | ||
|
||
trait Fixture { | ||
@volatile | ||
var streamConsumed = false | ||
val thousandElements: Stream[ByteString] = Stream.continually(ByteString("foo")).take(999).append { | ||
streamConsumed = true | ||
Seq(ByteString("end")) | ||
} | ||
|
||
} | ||
|
||
"Default ExceptionHandler" should { | ||
"rejectEntity by default" in new Fixture { | ||
streamConsumed shouldBe false | ||
Get("/crash", HttpEntity(`text/plain(UTF-8)`, Source[ByteString](thousandElements))) ~> Route.seal(route) ~> check { | ||
status shouldBe InternalServerError | ||
eventually { // Stream will be eventually consumed, once all the stream bytes are successfully discarded | ||
streamConsumed shouldBe true | ||
} | ||
} | ||
} | ||
|
||
"rejectEntity by default even if consumed already" in new Fixture { | ||
streamConsumed shouldBe false | ||
Get("/crashAfterConsuming", HttpEntity(`text/plain(UTF-8)`, Source[ByteString](thousandElements))) ~> Route.seal(route) ~> check { | ||
// Stream should be consumed immediately after the request finishes | ||
streamConsumed shouldBe true | ||
status shouldBe InternalServerError | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters