+doc #20466 example snippet for akka http java dsl: ParameterDirectives (#20823)

This commit is contained in:
Hawstein 2016-06-24 05:29:01 +08:00 committed by Konrad Malawski
parent 2836e990cb
commit 6777e7f7d9
9 changed files with 154 additions and 13 deletions

View file

@ -0,0 +1,121 @@
/*
* Copyright (C) 2016-2016 Lightbend Inc. <http://www.lightbend.com>
*/
package docs.http.javadsl.server.directives;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.Route;
import akka.http.javadsl.testkit.JUnitRouteTest;
import org.junit.Test;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ParameterDirectivesExamplesTest extends JUnitRouteTest {
@Test
public void testParameter() {
//#parameter
final Route route = parameter("color", color ->
complete("The color is '" + color + "'")
);
// tests:
testRoute(route).run(HttpRequest.GET("/?color=blue"))
.assertEntity("The color is 'blue'");
testRoute(route).run(HttpRequest.GET("/"))
.assertStatusCode(StatusCodes.NOT_FOUND)
.assertEntity("Request is missing required query parameter 'color'");
//#parameter
}
@Test
public void testParameters() {
//#parameters
final Route route = parameter("color", color ->
parameter("backgroundColor", backgroundColor ->
complete("The color is '" + color
+ "' and the background is '" + backgroundColor + "'")
)
);
// tests:
testRoute(route).run(HttpRequest.GET("/?color=blue&backgroundColor=red"))
.assertEntity("The color is 'blue' and the background is 'red'");
testRoute(route).run(HttpRequest.GET("/?color=blue"))
.assertStatusCode(StatusCodes.NOT_FOUND)
.assertEntity("Request is missing required query parameter 'backgroundColor'");
//#parameters
}
@Test
public void testParameterMap() {
//#parameterMap
final Function<Entry, String> paramString =
entry -> entry.getKey() + " = '" + entry.getValue() + "'";
final Route route = parameterMap(params -> {
final String pString = params.entrySet()
.stream()
.map(paramString::apply)
.collect(Collectors.joining(", "));
return complete("The parameters are " + pString);
});
// tests:
testRoute(route).run(HttpRequest.GET("/?color=blue&count=42"))
.assertEntity("The parameters are color = 'blue', count = '42'");
testRoute(route).run(HttpRequest.GET("/?x=1&x=2"))
.assertEntity("The parameters are x = '2'");
//#parameterMap
}
@Test
public void testParameterMultiMap() {
//#parameterMultiMap
final Route route = parameterMultiMap(params -> {
final String pString = params.entrySet()
.stream()
.map(e -> e.getKey() + " -> " + e.getValue().size())
.collect(Collectors.joining(", "));
return complete("There are parameters " + pString);
});
// tests:
testRoute(route).run(HttpRequest.GET("/?color=blue&count=42"))
.assertEntity("There are parameters color -> 1, count -> 1");
testRoute(route).run(HttpRequest.GET("/?x=23&x=42"))
.assertEntity("There are parameters x -> 2");
//#parameterMultiMap
}
@Test
public void testParameterSeq() {
//#parameterSeq
final Function<Entry, String> paramString =
entry -> entry.getKey() + " = '" + entry.getValue() + "'";
final Route route = parameterList(params -> {
final String pString = params.stream()
.map(paramString::apply)
.collect(Collectors.joining(", "));
return complete("The parameters are " + pString);
});
// tests:
testRoute(route).run(HttpRequest.GET("/?color=blue&count=42"))
.assertEntity("The parameters are color = 'blue', count = '42'");
testRoute(route).run(HttpRequest.GET("/?x=1&x=2"))
.assertEntity("The parameters are x = '1', x = '2'");
//#parameterSeq
}
}

View file

@ -7,6 +7,7 @@ ParameterDirectives
:maxdepth: 1
parameter
parameters
parameterMap
parameterMultiMap
parameterSeq

View file

@ -12,4 +12,5 @@ See :ref:`which-parameter-directive-java` to understand when to use which direct
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/ParameterDirectivesExamplesTest.java#parameter

View file

@ -12,4 +12,5 @@ See also :ref:`which-parameter-directive-java` to understand when to use which d
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/ParameterDirectivesExamplesTest.java#parameterMap

View file

@ -17,4 +17,5 @@ See :ref:`which-parameter-directive-java` to understand when to use which direct
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/ParameterDirectivesExamplesTest.java#parameterMultiMap

View file

@ -13,4 +13,5 @@ See :ref:`which-parameter-directive-java` to understand when to use which direct
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/ParameterDirectivesExamplesTest.java#parameterSeq

View file

@ -0,0 +1,15 @@
.. _-parameters-java-:
parameters
=========
Extracts multiple *query* parameter values from the request.
Description
-----------
See :ref:`which-parameter-directive-java` to understand when to use which directive.
Example
-------
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/ParameterDirectivesExamplesTest.java#parameters

View file

@ -636,9 +636,9 @@ private[persistence] trait Eventsourced extends Snapshotter with PersistenceStas
writeInProgress = false
() // it will be stopped by the first WriteMessageFailure message
case _: RecoveryTick =>
// we may have one of these in the mailbox before the scheduled timeout
// is cancelled when recovery has completed, just consume it so the concrete actor never sees it
case _: RecoveryTick
// we may have one of these in the mailbox before the scheduled timeout
// is cancelled when recovery has completed, just consume it so the concrete actor never sees it
}
def onWriteMessageComplete(err: Boolean): Unit

View file

@ -606,8 +606,8 @@ object TestSubscriber {
}
/**
* Request and expect a stream element.
*/
* Request and expect a stream element.
*/
def requestNext(element: T): Self = {
subscription.request(1)
expectNext(element)
@ -620,16 +620,16 @@ object TestSubscriber {
}
/**
* Request and expect a stream element.
*/
* Request and expect a stream element.
*/
def requestNext(): T = {
subscription.request(1)
expectNext()
}
/**
* Request and expect a stream element during the specified time or timeout.
*/
* Request and expect a stream element during the specified time or timeout.
*/
def requestNext(d: FiniteDuration): T = {
subscription.request(1)
expectNext(d)