=doc,htp #20466 Include Respond Directives Examples for Java. (#20595)

* Include Respond Directives Examples for Java.

* Include license on RespondWithDirectivesExamplesTest.
This commit is contained in:
Felipe Fernández 2016-05-24 09:54:44 +01:00 committed by Konrad Malawski
parent 06c7467a0e
commit d9c7f9b7f0
6 changed files with 141 additions and 6 deletions

View file

@ -0,0 +1,135 @@
/*
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
*/
package docs.http.javadsl.server.directives;
import java.util.List;
import akka.http.javadsl.model.HttpHeader;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.headers.HttpOrigin;
import akka.http.javadsl.model.headers.Origin;
import akka.http.javadsl.model.headers.RawHeader;
import akka.http.javadsl.server.Route;
import akka.http.javadsl.testkit.JUnitRouteTest;
import com.google.common.collect.Lists;
import org.junit.Test;
public class RespondWithDirectivesExamplesTest extends JUnitRouteTest {
@Test
public void testRespondWithHeader() {
//#respondWithHeader
final Route route = path("foo", () ->
respondWithHeader(RawHeader.create("Funky-Muppet", "gonzo"), () ->
complete("beep")));
testRoute(route).run(HttpRequest.GET("/foo"))
.assertHeaderExists("Funky-Muppet", "gonzo")
.assertEntity("beep");
//#respondWithHeader
}
@Test
public void testRespondWithDefaultHeader() {
//#respondWithDefaultHeader
//custom headers
final RawHeader blippy = RawHeader.create("X-Fish-Name", "Blippy");
final RawHeader elTonno = RawHeader.create("X-Fish-Name", "El Tonno");
// format: OFF
// by default always include the Blippy header,
// unless a more specific X-Fish-Name is given by the inner route
final Route route =
respondWithDefaultHeader(blippy, () -> // blippy
respondWithHeader(elTonno, () -> // / el tonno
path("el-tonno", () -> // | /
complete("¡Ay blippy!") // | |- el tonno
).orElse( // | |
path("los-tonnos", () -> // | |
complete("¡Ay ay blippy!") // | |- el tonno
) // | |
) // | |
).orElse( // | x
complete("Blip!") // |- blippy
) // x
);
//format: ON
testRoute(route).run(HttpRequest.GET("/"))
.assertHeaderExists("X-Fish-Name", "Blippy")
.assertEntity("Blip!");
testRoute(route).run(HttpRequest.GET("/el-tonno"))
.assertHeaderExists("X-Fish-Name", "El Tonno")
.assertEntity("¡Ay blippy!");
testRoute(route).run(HttpRequest.GET("/los-tonnos"))
.assertHeaderExists("X-Fish-Name", "El Tonno")
.assertEntity("¡Ay ay blippy!");
//#respondWithDefaultHeader
}
@Test
public void respondWithHeaders() {
//#respondWithHeaders
final HttpHeader gonzo = RawHeader.create("Funky-Muppet", "gonzo");
final HttpHeader akka = Origin.create(HttpOrigin.parse("http://akka.io"));
final Route route = path("foo", () ->
respondWithHeaders(Lists.newArrayList(gonzo, akka), () ->
complete("beep")
)
);
testRoute(route).run(HttpRequest.GET("/foo"))
.assertHeaderExists("Funky-Muppet", "gonzo")
.assertHeaderExists("Origin", "http://akka.io")
.assertEntity("beep");
//#respondWithHeaders
}
@Test
public void testRespondWithDefaultHeaders() {
//#respondWithDefaultHeaders
//custom headers
final RawHeader blippy = RawHeader.create("X-Fish-Name", "Blippy");
final HttpHeader akka = Origin.create(HttpOrigin.parse("http://akka.io"));
final List<HttpHeader> defaultHeaders = Lists.newArrayList(blippy, akka);
final RawHeader elTonno = RawHeader.create("X-Fish-Name", "El Tonno");
// format: OFF
// by default always include the Blippy and Akka headers,
// unless a more specific X-Fish-Name is given by the inner route
final Route route =
respondWithDefaultHeaders(defaultHeaders, () -> // blippy and akka
respondWithHeader(elTonno, () -> // / el tonno
path("el-tonno", () -> // | /
complete("¡Ay blippy!") // | |- el tonno
).orElse( // | |
path("los-tonnos", () -> // | |
complete("¡Ay ay blippy!") // | |- el tonno
) // | |
) // | |
).orElse( // | x
complete("Blip!") // |- blippy and akka
) // x
);
//format: ON
testRoute(route).run(HttpRequest.GET("/"))
.assertHeaderExists("X-Fish-Name", "Blippy")
.assertHeaderExists("Origin", "http://akka.io")
.assertEntity("Blip!");
testRoute(route).run(HttpRequest.GET("/el-tonno"))
.assertHeaderExists("X-Fish-Name", "El Tonno")
.assertEntity("¡Ay blippy!");
testRoute(route).run(HttpRequest.GET("/los-tonnos"))
.assertHeaderExists("X-Fish-Name", "El Tonno")
.assertEntity("¡Ay ay blippy!");
//#respondWithDefaultHeaders
}
}

View file

@ -18,4 +18,4 @@ See also :ref:`-respondWithDefaultHeaders-java-` if you'd like to add more than
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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/RespondWithDirectivesExamplesTest.java#respondWithDefaultHeader

View file

@ -30,6 +30,6 @@ is shown in the example below, however it allows including multiple default head
The semantics remain the same however, as explained by the following 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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/RespondWithDirectivesExamplesTest.java#respondWithDefaultHeaders
See the :ref:`-respondWithDefaultHeader-java-` directive for an example with only one header.

View file

@ -14,4 +14,4 @@ See also :ref:`-respondWithHeaders-java-` if you'd like to add more than one hea
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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/RespondWithDirectivesExamplesTest.java#respondWithHeader

View file

@ -15,4 +15,4 @@ See also :ref:`-respondWithHeader-java-` if you'd like to add just a single head
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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/RespondWithDirectivesExamplesTest.java#respondWithHeaders

View file

@ -5,7 +5,7 @@ package docs.http.scaladsl
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.server.Directives
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.{ Matchers, WordSpec }
class SprayJsonCompactMarshalSpec extends WordSpec with Matchers {
@ -22,7 +22,7 @@ class SprayJsonCompactMarshalSpec extends WordSpec with Matchers {
}
// use it wherever json (un)marshalling is needed
class MyJsonService extends Directives with CompactJsonFormatSupport{
class MyJsonService extends Directives with CompactJsonFormatSupport {
// format: OFF
val route =