This commit is contained in:
parent
2836e990cb
commit
6777e7f7d9
9 changed files with 154 additions and 13 deletions
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ ParameterDirectives
|
||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
|
|
||||||
parameter
|
parameter
|
||||||
|
parameters
|
||||||
parameterMap
|
parameterMap
|
||||||
parameterMultiMap
|
parameterMultiMap
|
||||||
parameterSeq
|
parameterSeq
|
||||||
|
|
|
||||||
|
|
@ -12,4 +12,5 @@ See :ref:`which-parameter-directive-java` to understand when to use which direct
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -12,4 +12,5 @@ See also :ref:`which-parameter-directive-java` to understand when to use which d
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -17,4 +17,5 @@ See :ref:`which-parameter-directive-java` to understand when to use which direct
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -13,4 +13,5 @@ See :ref:`which-parameter-directive-java` to understand when to use which direct
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -636,7 +636,7 @@ private[persistence] trait Eventsourced extends Snapshotter with PersistenceStas
|
||||||
writeInProgress = false
|
writeInProgress = false
|
||||||
() // it will be stopped by the first WriteMessageFailure message
|
() // it will be stopped by the first WriteMessageFailure message
|
||||||
|
|
||||||
case _: RecoveryTick =>
|
case _: RecoveryTick ⇒
|
||||||
// we may have one of these in the mailbox before the scheduled timeout
|
// 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
|
// is cancelled when recovery has completed, just consume it so the concrete actor never sees it
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue