This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: examples/spray-can/simple-http-server/src/main/scala/spray/examples/FileUploadHandler.scala spray-httpx/src/main/scala/spray/httpx/unmarshalling/MultipartUnmarshallers.scala spray-httpx/src/test/scala/spray/httpx/unmarshalling/FormDataUnmarshallersSpec.scala
- Loading branch information
Showing
350 changed files
with
4,586 additions
and
2,258 deletions.
There are no files selected for viewing
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
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
130 changes: 130 additions & 0 deletions
130
docs/documentation/spray-routing/code/docs/directives/FutureDirectivesExamplesSpec.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,130 @@ | ||
package docs.directives | ||
|
||
import spray.http.StatusCodes._ | ||
import scala.concurrent.Future | ||
import scala.util.{Try, Success, Failure} | ||
import spray.util.LoggingContext | ||
import spray.routing.ExceptionHandler | ||
import akka.actor.{Actor, Props} | ||
import akka.pattern.ask | ||
import akka.util.Timeout | ||
import java.util.concurrent.TimeUnit | ||
|
||
class FutureDirectivesExamplesSpec extends DirectivesSpec { | ||
object TestException extends Throwable | ||
|
||
implicit def myExceptionHandler(implicit log: LoggingContext) = | ||
ExceptionHandler { | ||
case TestException => ctx => | ||
ctx.complete(InternalServerError, "Unsuccessful future!") | ||
} | ||
|
||
def handleResponse(response: Try[String]) = response match { | ||
case Success(value) => complete(value) | ||
case Failure(ex) => failWith(ex) | ||
} | ||
|
||
val resourceActor = system.actorOf(Props(new Actor { | ||
def receive = { case _ => sender ! "resource" } | ||
})) | ||
implicit val responseTimeout = Timeout(2, TimeUnit.SECONDS) | ||
|
||
"single-execution" in { | ||
val route = | ||
onSuccess((resourceActor ? "GetResource").mapTo[String]) { resource => | ||
// inner routes will always use the resource computed at creation time. | ||
complete(resource) | ||
} | ||
|
||
Get("/") ~> route ~> check { | ||
status === OK | ||
entityAs[String] === "resource" | ||
} | ||
} | ||
|
||
"per-request-execution" in { | ||
val route = | ||
dynamic { | ||
onSuccess((resourceActor ? "GetResource").mapTo[String]) { resources => | ||
// inner routes will get a fresh resource on every execution. | ||
complete(resources) | ||
} | ||
} | ||
|
||
Get("/") ~> route ~> check { | ||
status === OK | ||
entityAs[String] === "resource" | ||
} | ||
} | ||
|
||
"example-1" in { | ||
val route = | ||
path("success") { | ||
onComplete(Future { "Ok" }) { | ||
handleResponse | ||
} | ||
} ~ | ||
path("failure") { | ||
onComplete(Future.failed[String](TestException)) { | ||
handleResponse | ||
} | ||
} | ||
|
||
Get("/success") ~> route ~> check { | ||
entityAs[String] === "Ok" | ||
} | ||
|
||
Get("/failure") ~> sealRoute(route) ~> check { | ||
status === InternalServerError | ||
entityAs[String] === "Unsuccessful future!" | ||
} | ||
} | ||
|
||
"example-2" in { | ||
val route = | ||
path("success") { | ||
onSuccess(Future { "Ok" }) { extraction => | ||
complete(extraction) | ||
} | ||
} ~ | ||
path("failure") { | ||
onSuccess(Future.failed[String](TestException)) { extraction => | ||
complete(extraction) | ||
} | ||
} | ||
|
||
|
||
Get("/success") ~> route ~> check { | ||
entityAs[String] === "Ok" | ||
} | ||
|
||
Get("/failure") ~> sealRoute(route) ~> check { | ||
status === InternalServerError | ||
entityAs[String] === "Unsuccessful future!" | ||
} | ||
} | ||
|
||
"example-3" in { | ||
val route = | ||
path("success") { | ||
onFailure(Future { "Ok" }) { extraction => | ||
failWith(extraction) // not executed. | ||
} | ||
} ~ | ||
path("failure") { | ||
onFailure(Future.failed[String](TestException)) { extraction => | ||
failWith(extraction) | ||
} | ||
} | ||
|
||
|
||
Get("/success") ~> route ~> check { | ||
entityAs[String] === "Ok" | ||
} | ||
|
||
Get("/failure") ~> sealRoute(route) ~> check { | ||
status === InternalServerError | ||
entityAs[String] === "Unsuccessful future!" | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
.../documentation/spray-routing/code/docs/directives/MarshallingDirectivesExamplesSpec.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,80 @@ | ||
package docs.directives | ||
|
||
import spray.http._ | ||
import spray.httpx.SprayJsonSupport | ||
import spray.httpx.unmarshalling.Unmarshaller | ||
import spray.json.DefaultJsonProtocol | ||
import HttpHeaders._ | ||
import MediaTypes._ | ||
|
||
//# person-case-class | ||
case class Person(name: String, favoriteNumber: Int) | ||
|
||
//# person-json-support | ||
object PersonJsonSupport extends DefaultJsonProtocol with SprayJsonSupport { | ||
implicit val PortofolioFormats = jsonFormat2(Person) | ||
} | ||
//# | ||
|
||
class MarshallingDirectivesExamplesSpec extends DirectivesSpec { | ||
|
||
"example-entity-with-json" in { | ||
import PersonJsonSupport._ | ||
|
||
val route = post { | ||
entity(as[Person]) { person => | ||
complete(s"Person: ${person.name} - favorite number: ${person.favoriteNumber}") | ||
} | ||
} | ||
|
||
Post("/", HttpEntity(`application/json`, """{ "name": "Jane", "favoriteNumber" : 42 }""" )) ~> | ||
route ~> check { | ||
entityAs[String] === "Person: Jane - favorite number: 42" | ||
} | ||
} | ||
|
||
"example-produce-with-json" in { | ||
import PersonJsonSupport._ | ||
|
||
val findPerson = (f: Person => Unit) => { | ||
|
||
//... some processing logic... | ||
|
||
//complete the request | ||
f(Person("Jane", 42)) | ||
} | ||
|
||
val route = get { | ||
produce(instanceOf[Person]) { completionFunction => ctx => findPerson(completionFunction) } | ||
} | ||
|
||
Get("/") ~> route ~> check { | ||
mediaType === `application/json` | ||
entityAs[String] must contain(""""name": "Jane"""") | ||
entityAs[String] must contain(""""favoriteNumber": 42""") | ||
} | ||
} | ||
|
||
"example-handleWith-with-json" in { | ||
import PersonJsonSupport._ | ||
|
||
val updatePerson = (person: Person) => { | ||
|
||
//... some processing logic... | ||
|
||
//return the person | ||
person | ||
} | ||
|
||
val route = post { | ||
handleWith(updatePerson) | ||
} | ||
|
||
Post("/", HttpEntity(`application/json`, """{ "name": "Jane", "favoriteNumber" : 42 }""" )) ~> | ||
route ~> check { | ||
mediaType === `application/json` | ||
entityAs[String] must contain(""""name": "Jane"""") | ||
entityAs[String] must contain(""""favoriteNumber": 42""") | ||
} | ||
} | ||
} |
Oops, something went wrong.