Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scheme directive example in javadsl #20586

Merged
merged 2 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2015-2016 Lightbend Inc. <http://www.lightbend.com>
*/

package docs.http.javadsl.server.directives;

import java.util.Arrays;
import java.util.regex.Pattern;

import org.junit.Test;

import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.model.headers.Host;
import akka.http.javadsl.server.Route;
import akka.http.javadsl.server.RequestContext;
import akka.http.javadsl.testkit.JUnitRouteTest;
import java.util.function.Function;
import akka.http.javadsl.model.Uri;
import akka.http.javadsl.model.headers.Location;

public class SchemeDirectivesExamplesTest extends JUnitRouteTest {

@Test
public void testScheme() {
//#extractScheme
final Route route = extractScheme((scheme) ->
complete(String.format("The scheme is '%s'", scheme)));
testRoute(route).run(HttpRequest.GET("https://www.example.com/"))
.assertEntity("The scheme is 'https'");
//#extractScheme
}

@Test
public void testRedirection() {
//#scheme
final Route route = route(
scheme("http", ()->
extract((ctx) -> ctx.getRequest().getUri(), (uri)->
redirect(uri.scheme("https"), StatusCodes.MOVED_PERMANENTLY)
)
),
scheme("https", ()->
complete("Safe and secure!")
)
);

testRoute(route).run(HttpRequest.GET("http://www.example.com/hello"))
.assertStatusCode(StatusCodes.MOVED_PERMANENTLY)
.assertHeaderExists(Location.create("https://www.example.com/hello"))
;

testRoute(route).run(HttpRequest.GET("https://www.example.com/hello"))
.assertEntity("Safe and secure!");
//#scheme
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ For rejecting a request if it doesn't match a specified scheme name, see the :re

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/SchemeDirectivesExamplesTest.java#extractScheme
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ For simply extracting the scheme name, see the :ref:`-extractScheme-java-` direc

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/SchemeDirectivesExamplesTest.java#scheme