From 87a919608871b5c3e39a11a58be395658d3db253 Mon Sep 17 00:00:00 2001 From: Hawstein Date: Tue, 7 Jun 2016 02:45:36 +0800 Subject: [PATCH] +doc #20466 example snippet for akka http java dsl: FormFieldDirectives (#20731) --- .../FormFieldDirectivesExamplesTest.java | 137 ++++++++++++++++++ .../form-field-directives/formField.rst | 3 +- .../form-field-directives/formFieldList.rst | 3 +- .../form-field-directives/formFieldMap.rst | 3 +- .../formFieldMultiMap.rst | 3 +- 5 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 akka-docs/rst/java/code/docs/http/javadsl/server/directives/FormFieldDirectivesExamplesTest.java diff --git a/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FormFieldDirectivesExamplesTest.java b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FormFieldDirectivesExamplesTest.java new file mode 100644 index 0000000000..69c0e11239 --- /dev/null +++ b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FormFieldDirectivesExamplesTest.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2016-2016 Lightbend Inc. + */ +package docs.http.javadsl.server.directives; + +import akka.http.javadsl.model.FormData; +import akka.http.javadsl.model.HttpRequest; +import akka.http.javadsl.model.StatusCodes; +import akka.http.javadsl.server.Route; +import akka.http.javadsl.server.StringUnmarshallers; +import akka.http.javadsl.testkit.JUnitRouteTest; +import akka.japi.Pair; +import org.junit.Test; + +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class FormFieldDirectivesExamplesTest extends JUnitRouteTest { + + @Test + public void testFormField() { + //#formField + final Route route = route( + formField("color", color -> + complete("The color is '" + color + "'") + ), + formField(StringUnmarshallers.INTEGER, "id", id -> + complete("The id is '" + id + "'") + ) + ); + + // tests: + final FormData formData = FormData.create(Pair.create("color", "blue")); + testRoute(route).run(HttpRequest.POST("/").withEntity(formData.toEntity())) + .assertEntity("The color is 'blue'"); + + testRoute(route).run(HttpRequest.GET("/")) + .assertStatusCode(StatusCodes.BAD_REQUEST) + .assertEntity("Request is missing required form field 'color'"); + //#formField + } + + @Test + public void testFormFieldMap() { + //#formFieldMap + final Function, String> mapToString = map -> + map.entrySet() + .stream() + .map(e -> e.getKey() + " = '" + e.getValue() +"'") + .collect(Collectors.joining(", ")); + + + final Route route = formFieldMap(fields -> + complete("The form fields are " + mapToString.apply(fields)) + ); + + // tests: + final FormData formDataDiffKey = + FormData.create( + Pair.create("color", "blue"), + Pair.create("count", "42")); + testRoute(route).run(HttpRequest.POST("/").withEntity(formDataDiffKey.toEntity())) + .assertEntity("The form fields are color = 'blue', count = '42'"); + + final FormData formDataSameKey = + FormData.create( + Pair.create("x", "1"), + Pair.create("x", "5")); + testRoute(route).run(HttpRequest.POST("/").withEntity(formDataSameKey.toEntity())) + .assertEntity( "The form fields are x = '5'"); + //#formFieldMap + } + + @Test + public void testFormFieldMultiMap() { + //#formFieldMultiMap + final Function>, String> mapToString = map -> + map.entrySet() + .stream() + .map(e -> e.getKey() + " -> " + e.getValue().size()) + .collect(Collectors.joining(", ")); + + final Route route = formFieldMultiMap(fields -> + complete("There are form fields " + mapToString.apply(fields)) + ); + + // test: + final FormData formDataDiffKey = + FormData.create( + Pair.create("color", "blue"), + Pair.create("count", "42")); + testRoute(route).run(HttpRequest.POST("/").withEntity(formDataDiffKey.toEntity())) + .assertEntity("There are form fields color -> 1, count -> 1"); + + final FormData formDataSameKey = + FormData.create( + Pair.create("x", "23"), + Pair.create("x", "4"), + Pair.create("x", "89")); + testRoute(route).run(HttpRequest.POST("/").withEntity(formDataSameKey.toEntity())) + .assertEntity("There are form fields x -> 3"); + //#formFieldMultiMap + } + + @Test + public void testFormFieldList() { + //#formFieldList + final Function>, String> listToString = list -> + list.stream() + .map(e -> e.getKey() + " = '" + e.getValue() +"'") + .collect(Collectors.joining(", ")); + + final Route route = formFieldList(fields -> + complete("The form fields are " + listToString.apply(fields)) + ); + + // tests: + final FormData formDataDiffKey = + FormData.create( + Pair.create("color", "blue"), + Pair.create("count", "42")); + testRoute(route).run(HttpRequest.POST("/").withEntity(formDataDiffKey.toEntity())) + .assertEntity("The form fields are color = 'blue', count = '42'"); + + final FormData formDataSameKey = + FormData.create( + Pair.create("x", "23"), + Pair.create("x", "4"), + Pair.create("x", "89")); + testRoute(route).run(HttpRequest.POST("/").withEntity(formDataSameKey.toEntity())) + .assertEntity("The form fields are x = '23', x = '4', x = '89'"); + //#formFieldList + } +} diff --git a/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formField.rst b/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formField.rst index 6711ae2b37..5b9265ea8d 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formField.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formField.rst @@ -8,4 +8,5 @@ Allows extracting a single Form field sent in the request. 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/FormFieldDirectivesExamplesTest.java#formField diff --git a/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldList.rst b/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldList.rst index 260e57db43..7f6ba96934 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldList.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldList.rst @@ -17,4 +17,5 @@ can cause performance issues or even an ``OutOfMemoryError`` s. 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/FormFieldDirectivesExamplesTest.java#formFieldList diff --git a/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldMap.rst b/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldMap.rst index f8c34c0f8e..5b678f0518 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldMap.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldMap.rst @@ -16,4 +16,5 @@ See :ref:`-formFieldList-java-` for details. 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/FormFieldDirectivesExamplesTest.java#formFieldMap diff --git a/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldMultiMap.rst b/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldMultiMap.rst index 7e4322023c..a922975c5b 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldMultiMap.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/form-field-directives/formFieldMultiMap.rst @@ -19,4 +19,5 @@ Use of this directive can result in performance degradation or even in ``OutOfMe 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/FormFieldDirectivesExamplesTest.java#formFieldMultiMap