+doc,htp example snippet for akka http java dsl: CookieDirectives (#20585)(#20466)

This commit is contained in:
Hawstein 2016-05-24 17:22:24 +08:00 committed by Konrad Malawski
parent dfc7943e94
commit 77741acfca
5 changed files with 110 additions and 4 deletions

View file

@ -0,0 +1,102 @@
/*
* Copyright (C) 2016-2016 Lightbend Inc. <http://www.lightbend.com>
*/
package docs.http.javadsl.server.directives;
import akka.http.javadsl.model.HttpHeader;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.headers.Cookie;
import akka.http.javadsl.model.headers.HttpCookie;
import akka.http.javadsl.model.headers.SetCookie;
import akka.http.javadsl.server.Rejections;
import akka.http.javadsl.server.Route;
import akka.http.javadsl.testkit.JUnitRouteTest;
import akka.http.scaladsl.model.DateTime;
import org.junit.Test;
import java.util.Optional;
import java.util.OptionalLong;
public class CookieDirectivesExamplesTest extends JUnitRouteTest {
@Test
public void testCookie() {
//#cookie
final Route route = cookie("userName", nameCookie ->
complete("The logged in user is '" + nameCookie.value() + "'")
);
// tests:
testRoute(route).run(HttpRequest.GET("/").addHeader(Cookie.create("userName", "paul")))
.assertEntity("The logged in user is 'paul'");
// missing cookie
runRouteUnSealed(route, HttpRequest.GET("/"))
.assertRejections(Rejections.missingCookie("userName"));
testRoute(route).run(HttpRequest.GET("/"))
.assertEntity("Request is missing required cookie 'userName'");
//#cookie
}
@Test
public void testOptionalCookie() {
//#optionalCookie
final Route route = optionalCookie("userName", optNameCookie -> {
if (optNameCookie.isPresent()) {
return complete("The logged in user is '" + optNameCookie.get().value() + "'");
} else {
return complete("No user logged in");
}
}
);
// tests:
testRoute(route).run(HttpRequest.GET("/").addHeader(Cookie.create("userName", "paul")))
.assertEntity("The logged in user is 'paul'");
testRoute(route).run(HttpRequest.GET("/"))
.assertEntity("No user logged in");
//#optionalCookie
}
@Test
public void testDeleteCookie() {
//#deleteCookie
final Route route = deleteCookie("userName", () ->
complete("The user was logged out")
);
// tests:
final HttpHeader expected = SetCookie.create(
HttpCookie.create(
"userName",
"deleted",
Optional.of(DateTime.MinValue()),
OptionalLong.empty(),
Optional.empty(),
Optional.empty(),
false,
false,
Optional.empty()));
testRoute(route).run(HttpRequest.GET("/"))
.assertEntity("The user was logged out")
.assertHeaderExists(expected);
//#deleteCookie
}
@Test
public void testSetCookie() {
//#setCookie
final Route route = setCookie(HttpCookie.create("userName", "paul"), () ->
complete("The user was logged in")
);
// tests:
final HttpHeader expected = SetCookie.create(HttpCookie.create("userName", "paul"));
testRoute(route).run(HttpRequest.GET("/"))
.assertEntity("The user was logged in")
.assertHeaderExists(expected);
//#setCookie
}
}

View file

@ -13,4 +13,5 @@ Use the :ref:`-optionalCookie-java-` directive instead if you want to support mi
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/CookieDirectivesExamplesTest.java#cookie

View file

@ -11,4 +11,5 @@ Use the :ref:`-setCookie-java-` directive to update a cookie.
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/CookieDirectivesExamplesTest.java#deleteCookie

View file

@ -12,4 +12,5 @@ Use the :ref:`-cookie-java-` directive instead if the inner route does not handl
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/CookieDirectivesExamplesTest.java#optionalCookie

View file

@ -12,4 +12,5 @@ Use the :ref:`-deleteCookie-java-` directive to delete a cookie.
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/CookieDirectivesExamplesTest.java#setCookie