=doc another batch of documentation (example) fixes

This commit is contained in:
Johannes Rudolph 2015-05-21 17:34:46 +02:00
parent fd85cac20f
commit 4b256f7c1a
41 changed files with 213 additions and 215 deletions

View file

@ -148,7 +148,7 @@ class BasicDirectivesExamplesSpec extends RoutingSpec {
responseAs[String] shouldEqual "POST"
}
}
"0mapRouteResponse" in {
"0mapRouteResult" in {
val rejectAll = // not particularly useful directive
mapRouteResult {
case _ => Rejected(List(AuthorizationFailedRejection))
@ -192,4 +192,103 @@ class BasicDirectivesExamplesSpec extends RoutingSpec {
responseAs[String] shouldEqual "prefix:test"
}
}
"cancelRejections-filter-example" in {
def isMethodRejection: Rejection => Boolean = {
case MethodRejection(_) => true
case _ => false
}
val route =
cancelRejections(isMethodRejection) {
post {
complete("Result")
}
}
Get("/") ~> route ~> check {
rejections shouldEqual Nil
handled shouldEqual false
}
}
"cancelRejection-example" in {
val route =
cancelRejection(MethodRejection(HttpMethods.POST)) {
post {
complete("Result")
}
}
Get("/") ~> route ~> check {
rejections shouldEqual Nil
handled shouldEqual false
}
}
"extractRequest-example" in {
val route =
extractRequest { request =>
complete(s"Request method is ${request.method.name} and content-type is ${request.entity.contentType}")
}
Post("/", "text") ~> route ~> check {
responseAs[String] shouldEqual "Request method is POST and content-type is text/plain; charset=UTF-8"
}
Get("/") ~> route ~> check {
responseAs[String] shouldEqual "Request method is GET and content-type is none/none"
}
}
"extractUri-example" in {
val route =
extractUri { uri =>
complete(s"Full URI: $uri")
}
Get("/") ~> route ~> check {
// tests are executed with the host assumed to be "example.com"
responseAs[String] shouldEqual "Full URI: http://example.com/"
}
Get("/test") ~> route ~> check {
responseAs[String] shouldEqual "Full URI: http://example.com/test"
}
}
"mapUnmatchedPath-example" in {
def ignore456(path: Uri.Path) = path match {
case s @ Uri.Path.Segment(head, tail) if head.startsWith("456") =>
val newHead = head.drop(3)
if (newHead.isEmpty) tail
else s.copy(head = head.drop(3))
case _ => path
}
val ignoring456 = mapUnmatchedPath(ignore456)
val route =
pathPrefix("123") {
ignoring456 {
path("abc") {
complete(s"Content")
}
}
}
Get("/123/abc") ~> route ~> check {
responseAs[String] shouldEqual "Content"
}
Get("/123456/abc") ~> route ~> check {
responseAs[String] shouldEqual "Content"
}
}
"extractUnmatchedPath-example" in {
val route =
pathPrefix("abc") {
extractUnmatchedPath { remaining =>
complete(s"Unmatched: '$remaining'")
}
}
Get("/abc") ~> route ~> check {
responseAs[String] shouldEqual "Unmatched: ''"
}
Get("/abc/456") ~> route ~> check {
responseAs[String] shouldEqual "Unmatched: '/456'"
}
}
}

View file

@ -31,7 +31,7 @@ class FutureDirectivesExamplesSpec extends RoutingSpec {
}))
implicit val responseTimeout = Timeout(2, TimeUnit.SECONDS)
"example-1" in {
"onComplete" in {
def divide(a: Int, b: Int): Future[Int] = Future {
a / b
}
@ -54,7 +54,7 @@ class FutureDirectivesExamplesSpec extends RoutingSpec {
}
}
"example-2" in {
"onSuccess" in {
val route =
path("success") {
onSuccess(Future { "Ok" }) { extraction =>
@ -77,7 +77,7 @@ class FutureDirectivesExamplesSpec extends RoutingSpec {
}
}
"example-3" in {
"completeOrRecoverWith" in {
val route =
path("success") {
completeOrRecoverWith(Future { "Ok" }) { extraction =>

View file

@ -11,7 +11,7 @@ import StatusCodes._
class HostDirectivesExamplesSpec extends RoutingSpec {
"extract-hostname" in {
"extractHost" in {
val route =
extractHost { hn =>
complete(s"Hostname: $hn")

View file

@ -4,12 +4,13 @@
package docs.http.scaladsl.server
package directives
/*
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.model._
import spray.json.DefaultJsonProtocol
import headers._
import StatusCodes._
import MediaTypes.`application/json`
//# person-case-class
case class Person(name: String, favoriteNumber: Int)
@ -37,7 +38,7 @@ class MarshallingDirectivesExamplesSpec extends RoutingSpec {
}
}
"example-produce-with-json" in {
"example-completeWith-with-json" in {
import PersonJsonSupport._
val findPerson = (f: Person => Unit) => {
@ -49,13 +50,13 @@ class MarshallingDirectivesExamplesSpec extends RoutingSpec {
}
val route = get {
produce(instanceOf[Person]) { completionFunction => ctx => findPerson(completionFunction) }
completeWith(instanceOf[Person]) { completionFunction => findPerson(completionFunction) }
}
Get("/") ~> route ~> check {
mediaType shouldEqual `application/json`
responseAs[String] must contain(""""name": "Jane"""")
responseAs[String] must contain(""""favoriteNumber": 42""")
responseAs[String] should include(""""name": "Jane"""")
responseAs[String] should include(""""favoriteNumber": 42""")
}
}
@ -77,9 +78,8 @@ class MarshallingDirectivesExamplesSpec extends RoutingSpec {
Post("/", HttpEntity(`application/json`, """{ "name": "Jane", "favoriteNumber" : 42 }""")) ~>
route ~> check {
mediaType shouldEqual `application/json`
responseAs[String] must contain(""""name": "Jane"""")
responseAs[String] must contain(""""favoriteNumber": 42""")
responseAs[String] should include(""""name": "Jane"""")
responseAs[String] should include(""""favoriteNumber": 42""")
}
}
}
*/

View file

@ -4,80 +4,22 @@
package docs.http.scaladsl.server
package directives
/*
import akka.http.scaladsl.model._
import akka.http.scaladsl.server._
import headers._
class MiscDirectivesExamplesSpec extends RoutingSpec {
"cancelAllRejections-example" in {
def isMethodRejection: Rejection => Boolean = {
case MethodRejection(_) => true
case _ => false
}
val route =
cancelAllRejections(isMethodRejection) {
post {
complete("Result")
}
}
Get("/") ~> route ~> check {
rejections shouldEqual Nil
handled shouldEqual false
}
}
"cancelRejection-example" in {
val route =
cancelRejection(MethodRejection(HttpMethods.POST)) {
post {
complete("Result")
}
}
Get("/") ~> route ~> check {
rejections shouldEqual Nil
handled shouldEqual false
}
}
"clientIP-example" in {
val route = clientIP { ip =>
"extractClientIP-example" in {
val route = extractClientIP { ip =>
complete("Client's ip is " + ip.toOption.map(_.getHostAddress).getOrElse("unknown"))
}
Get("/").withHeaders(`Remote-Address`("192.168.3.12")) ~> route ~> check {
Get("/").withHeaders(`Remote-Address`(RemoteAddress("192.168.3.12"))) ~> route ~> check {
responseAs[String] shouldEqual "Client's ip is 192.168.3.12"
}
}
"jsonpWithParameter-example" in {
case class Test(abc: Int)
object TestProtocol {
import spray.json.DefaultJsonProtocol._
implicit val testFormat = jsonFormat(Test, "abc")
}
val route =
jsonpWithParameter("jsonp") {
import TestProtocol._
import spray.httpx.SprayJsonSupport._
complete(Test(456))
}
Get("/?jsonp=result") ~> route ~> check {
responseAs[String] shouldEqual
"""result({
| "abc": 456
|})""".stripMarginWithNewline("\n")
contentType shouldEqual MediaTypes.`application/javascript`.withCharset(HttpCharsets.`UTF-8`)
}
Get("/") ~> route ~> check {
responseAs[String] shouldEqual
"""{
| "abc": 456
|}""".stripMarginWithNewline("\n")
contentType shouldEqual ContentTypes.`application/json`
}
}
"rejectEmptyResponse-example" in {
val route = rejectEmptyResponse {
path("even" / IntNumber) { i =>
@ -113,77 +55,9 @@ class MiscDirectivesExamplesSpec extends RoutingSpec {
responseAs[String] shouldEqual "request entity empty"
}
}
"requestInstance-example" in {
val route =
requestInstance { request =>
complete(s"Request method is ${request.method} and length is ${request.entity.data.length}")
}
Post("/", "text") ~> route ~> check {
responseAs[String] shouldEqual "Request method is POST and length is 4"
}
Get("/") ~> route ~> check {
responseAs[String] shouldEqual "Request method is GET and length is 0"
}
}
"requestUri-example" in {
val route =
requestUri { uri =>
complete(s"Full URI: $uri")
}
Get("/") ~> route ~> check {
// tests are executed with the host assumed to be "example.com"
responseAs[String] shouldEqual "Full URI: http://example.com/"
}
Get("/test") ~> route ~> check {
responseAs[String] shouldEqual "Full URI: http://example.com/test"
}
}
"rewriteUnmatchedPath-example" in {
def ignore456(path: Uri.Path) = path match {
case s @ Uri.Path.Segment(head, tail) if head.startsWith("456") =>
val newHead = head.drop(3)
if (newHead.isEmpty) tail
else s.copy(head = head.drop(3))
case _ => path
}
val ignoring456 = rewriteUnmatchedPath(ignore456)
val route =
pathPrefix("123") {
ignoring456 {
path("abc") {
complete(s"Content")
}
}
}
Get("/123/abc") ~> route ~> check {
responseAs[String] shouldEqual "Content"
}
Get("/123456/abc") ~> route ~> check {
responseAs[String] shouldEqual "Content"
}
}
"unmatchedPath-example" in {
val route =
pathPrefix("abc") {
unmatchedPath { remaining =>
complete(s"Unmatched: '$remaining'")
}
}
Get("/abc") ~> route ~> check {
responseAs[String] shouldEqual "Unmatched: ''"
}
Get("/abc/456") ~> route ~> check {
responseAs[String] shouldEqual "Unmatched: '/456'"
}
}
"validate-example" in {
val route =
requestUri { uri =>
extractUri { uri =>
validate(uri.path.toString.size < 5, s"Path too long: '${uri.path.toString}'") {
complete(s"Full URI: $uri")
}
@ -197,4 +71,3 @@ class MiscDirectivesExamplesSpec extends RoutingSpec {
}
}
}
*/