diff --git a/akka-docs/rst/java/code/docs/http/javadsl/server/directives/RouteDirectivesExamplesTest.java b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/RouteDirectivesExamplesTest.java new file mode 100644 index 0000000000..7771245b79 --- /dev/null +++ b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/RouteDirectivesExamplesTest.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2016-2016 Lightbend Inc. + */ +package docs.http.javadsl.server.directives; + +import akka.http.javadsl.model.HttpEntities; +import akka.http.javadsl.model.HttpRequest; +import akka.http.javadsl.model.Uri; +import akka.http.javadsl.model.headers.ContentType; +import akka.http.javadsl.model.ContentTypes; +import akka.http.javadsl.model.HttpResponse; +import akka.http.javadsl.model.StatusCodes; +import akka.http.javadsl.server.Rejections; +import akka.http.javadsl.server.Route; +import akka.http.javadsl.testkit.JUnitRouteTest; +import org.junit.Test; + +import java.util.Collections; + +public class RouteDirectivesExamplesTest extends JUnitRouteTest { + + @Test + public void testComplete() { + //#complete + final Route route = route( + path("a", () -> complete(HttpResponse.create().withEntity("foo"))), + path("b", () -> complete(StatusCodes.OK)), + path("c", () -> complete(StatusCodes.CREATED, "bar")), + path("d", () -> complete(StatusCodes.get(201), "bar")), + path("e", () -> + complete(StatusCodes.CREATED, + Collections.singletonList(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8)), + HttpEntities.create("bar"))), + path("f", () -> + complete(StatusCodes.get(201), + Collections.singletonList(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8)), + HttpEntities.create("bar"))), + path("g", () -> complete("baz")) + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/a")) + .assertStatusCode(StatusCodes.OK) + .assertEntity("foo"); + + testRoute(route).run(HttpRequest.GET("/b")) + .assertStatusCode(StatusCodes.OK) + .assertEntity("OK"); + + testRoute(route).run(HttpRequest.GET("/c")) + .assertStatusCode(StatusCodes.CREATED) + .assertEntity("bar"); + + testRoute(route).run(HttpRequest.GET("/d")) + .assertStatusCode(StatusCodes.CREATED) + .assertEntity("bar"); + + testRoute(route).run(HttpRequest.GET("/e")) + .assertStatusCode(StatusCodes.CREATED) + .assertHeaderExists(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8)) + .assertEntity("bar"); + + testRoute(route).run(HttpRequest.GET("/f")) + .assertStatusCode(StatusCodes.CREATED) + .assertHeaderExists(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8)) + .assertEntity("bar"); + + testRoute(route).run(HttpRequest.GET("/g")) + .assertStatusCode(StatusCodes.OK) + .assertEntity("baz"); + //#complete + } + + @Test + public void testReject() { + //#reject + final Route route = route( + path("a", this::reject), // don't handle here, continue on + path("a", () -> complete("foo")), + path("b", () -> reject(Rejections.validationRejection("Restricted!"))) + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/a")) + .assertEntity("foo"); + + runRouteUnSealed(route, HttpRequest.GET("/b")) + .assertRejections(Rejections.validationRejection("Restricted!")); + //#reject + } + + @Test + public void testRedirect() { + //#redirect + final Route route = pathPrefix("foo", () -> + route( + pathSingleSlash(() -> complete("yes")), + pathEnd(() -> redirect(Uri.create("/foo/"), StatusCodes.PERMANENT_REDIRECT)) + ) + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/foo/")) + .assertEntity("yes"); + + testRoute(route).run(HttpRequest.GET("/foo")) + .assertStatusCode(StatusCodes.PERMANENT_REDIRECT) + .assertEntity("The request, and all future requests should be repeated using this URI."); + //#redirect + } + + @Test + public void testFailWith() { + //#failWith + final Route route = path("foo", () -> + failWith(new RuntimeException("Oops.")) + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/foo")) + .assertStatusCode(StatusCodes.INTERNAL_SERVER_ERROR) + .assertEntity("There was an internal server error."); + //#failWith + } +} diff --git a/akka-docs/rst/java/http/routing-dsl/directives/route-directives/complete.rst b/akka-docs/rst/java/http/routing-dsl/directives/route-directives/complete.rst index 0380a70a9b..d5e8f70629 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/route-directives/complete.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/route-directives/complete.rst @@ -17,4 +17,5 @@ Please note that the ``complete`` directive has multiple variants, like Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/RouteDirectivesExamplesTest.java#complete diff --git a/akka-docs/rst/java/http/routing-dsl/directives/route-directives/failWith.rst b/akka-docs/rst/java/http/routing-dsl/directives/route-directives/failWith.rst index 5e5aae085c..66d6e1e8d6 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/route-directives/failWith.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/route-directives/failWith.rst @@ -24,4 +24,5 @@ exception. Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/RouteDirectivesExamplesTest.java#failWith diff --git a/akka-docs/rst/java/http/routing-dsl/directives/route-directives/redirect.rst b/akka-docs/rst/java/http/routing-dsl/directives/route-directives/redirect.rst index bd98427222..07f9db4377 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/route-directives/redirect.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/route-directives/redirect.rst @@ -10,9 +10,8 @@ Completes the request with a redirection response to a given targer URI and of a ``redirect`` is a convenience helper for completing the request with a redirection response. It is equivalent to this snippet relying on the ``complete`` directive: -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. - Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/RouteDirectivesExamplesTest.java#redirect diff --git a/akka-docs/rst/java/http/routing-dsl/directives/route-directives/reject.rst b/akka-docs/rst/java/http/routing-dsl/directives/route-directives/reject.rst index 2a21de1c68..e31ec91878 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/route-directives/reject.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/route-directives/reject.rst @@ -19,4 +19,5 @@ modifier for "filtering out" certain cases. Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/RouteDirectivesExamplesTest.java#reject