!htp #17965 more consistent and 22-ified javadsl handleWith
This commit is contained in:
parent
b137117399
commit
569c0bf079
27 changed files with 339 additions and 272 deletions
|
|
@ -1,16 +1,14 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
import akka.http.javadsl.server.HandlerBindingTest;
|
||||
import docs.http.javadsl.server.HandlerExampleSpec;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
HandlerBindingTest.class,
|
||||
HandlerExampleSpec.class
|
||||
HandlerBindingTest.class
|
||||
})
|
||||
public class AllJavaTests {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import akka.http.scaladsl.model.HttpRequest;
|
|||
import org.junit.Test;
|
||||
import akka.http.javadsl.testkit.*;
|
||||
import akka.http.javadsl.server.values.*;
|
||||
import static akka.http.javadsl.server.Directives.*;
|
||||
|
||||
public class HandlerBindingTest extends JUnitRouteTest {
|
||||
Parameter<Integer> aParam = Parameters.intValue("a");
|
||||
|
|
@ -24,7 +23,7 @@ public class HandlerBindingTest extends JUnitRouteTest {
|
|||
}
|
||||
@Test
|
||||
public void testHandler1() {
|
||||
Route route = handleWith(aParam, (ctx, a) -> ctx.complete("Ok " + a));
|
||||
Route route = handleWith1(aParam, (ctx, a) -> ctx.complete("Ok " + a));
|
||||
TestResponse response = runRoute(route, HttpRequest.GET("?a=23"));
|
||||
response.assertStatusCode(200);
|
||||
response.assertEntity("Ok 23");
|
||||
|
|
@ -32,7 +31,7 @@ public class HandlerBindingTest extends JUnitRouteTest {
|
|||
@Test
|
||||
public void testHandler2() {
|
||||
Route route =
|
||||
handleWith(
|
||||
handleWith2(
|
||||
aParam,
|
||||
bParam,
|
||||
(ctx, a, b) -> ctx.complete("Sum: " + (a + b)));
|
||||
|
|
@ -43,7 +42,7 @@ public class HandlerBindingTest extends JUnitRouteTest {
|
|||
@Test
|
||||
public void testHandler3() {
|
||||
Route route =
|
||||
handleWith(
|
||||
handleWith3(
|
||||
aParam,
|
||||
bParam,
|
||||
cParam,
|
||||
|
|
@ -55,7 +54,7 @@ public class HandlerBindingTest extends JUnitRouteTest {
|
|||
@Test
|
||||
public void testHandler4() {
|
||||
Route route =
|
||||
handleWith(
|
||||
handleWith4(
|
||||
aParam,
|
||||
bParam,
|
||||
cParam,
|
||||
|
|
@ -71,7 +70,7 @@ public class HandlerBindingTest extends JUnitRouteTest {
|
|||
@Test
|
||||
public void testHandler4MethodRef() {
|
||||
Route route =
|
||||
handleWith(
|
||||
handleWith4(
|
||||
aParam,
|
||||
bParam,
|
||||
cParam,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.http.javadsl.server;
|
||||
|
||||
import akka.dispatch.Futures;
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
import akka.http.javadsl.server.*;
|
||||
import akka.http.javadsl.server.values.Parameters;
|
||||
import akka.http.javadsl.server.values.PathMatchers;
|
||||
import akka.http.javadsl.testkit.JUnitRouteTest;
|
||||
import akka.http.javadsl.testkit.TestRoute;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HandlerExampleDocTest extends JUnitRouteTest {
|
||||
@Test
|
||||
public void testSimpleHandler() {
|
||||
//#simple-handler-example-full
|
||||
class TestHandler extends akka.http.javadsl.server.AllDirectives {
|
||||
//#simple-handler
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx) {
|
||||
return ctx.complete("This was a " + ctx.request().method().value() +
|
||||
" request to "+ctx.request().getUri());
|
||||
}
|
||||
};
|
||||
//#simple-handler
|
||||
|
||||
Route createRoute() {
|
||||
return route(
|
||||
get(
|
||||
handleWith(handler)
|
||||
),
|
||||
post(
|
||||
path("abc").route(
|
||||
handleWith(handler)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// actual testing code
|
||||
TestRoute r = testRoute(new TestHandler().createRoute());
|
||||
r.run(HttpRequest.GET("/test"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("This was a GET request to /test");
|
||||
|
||||
r.run(HttpRequest.POST("/test"))
|
||||
.assertStatusCode(404);
|
||||
|
||||
r.run(HttpRequest.POST("/abc"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("This was a POST request to /abc");
|
||||
//#simple-handler-example-full
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculator() {
|
||||
//#handler2-example-full
|
||||
class TestHandler extends akka.http.javadsl.server.AllDirectives {
|
||||
RequestVal<Integer> xParam = Parameters.intValue("x");
|
||||
RequestVal<Integer> yParam = Parameters.intValue("y");
|
||||
|
||||
RequestVal<Integer> xSegment = PathMatchers.intValue();
|
||||
RequestVal<Integer> ySegment = PathMatchers.intValue();
|
||||
|
||||
//#handler2
|
||||
final Handler2<Integer, Integer> multiply =
|
||||
new Handler2<Integer, Integer>() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx, Integer x, Integer y) {
|
||||
int result = x * y;
|
||||
return ctx.complete("x * y = " + result);
|
||||
}
|
||||
};
|
||||
|
||||
final Route multiplyXAndYParam = handleWith2(xParam, yParam, multiply);
|
||||
//#handler2
|
||||
|
||||
Route createRoute() {
|
||||
return route(
|
||||
get(
|
||||
pathPrefix("calculator").route(
|
||||
path("multiply").route(
|
||||
multiplyXAndYParam
|
||||
),
|
||||
path("path-multiply", xSegment, ySegment).route(
|
||||
handleWith2(xSegment, ySegment, multiply)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// actual testing code
|
||||
TestRoute r = testRoute(new TestHandler().createRoute());
|
||||
r.run(HttpRequest.GET("/calculator/multiply?x=12&y=42"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x * y = 504");
|
||||
|
||||
r.run(HttpRequest.GET("/calculator/path-multiply/23/5"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x * y = 115");
|
||||
//#handler2-example-full
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculatorJava8() {
|
||||
//#handler2-java8-example-full
|
||||
class TestHandler extends akka.http.javadsl.server.AllDirectives {
|
||||
final RequestVal<Integer> xParam = Parameters.intValue("x");
|
||||
final RequestVal<Integer> yParam = Parameters.intValue("y");
|
||||
|
||||
//#handler2-java8
|
||||
final Handler2<Integer, Integer> multiply =
|
||||
(ctx, x, y) -> ctx.complete("x * y = " + (x * y));
|
||||
|
||||
final Route multiplyXAndYParam = handleWith2(xParam, yParam, multiply);
|
||||
//#handler2-java8
|
||||
|
||||
RouteResult subtract(RequestContext ctx, int x, int y) {
|
||||
return ctx.complete("x - y = " + (x - y));
|
||||
}
|
||||
|
||||
Route createRoute() {
|
||||
return route(
|
||||
get(
|
||||
pathPrefix("calculator").route(
|
||||
path("multiply").route(
|
||||
// use Handler explicitly
|
||||
multiplyXAndYParam
|
||||
),
|
||||
path("add").route(
|
||||
// create Handler as lambda expression
|
||||
handleWith2(xParam, yParam,
|
||||
(ctx, x, y) -> ctx.complete("x + y = " + (x + y)))
|
||||
),
|
||||
path("subtract").route(
|
||||
// create handler by lifting method
|
||||
handleWith2(xParam, yParam, this::subtract)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// actual testing code
|
||||
TestRoute r = testRoute(new TestHandler().createRoute());
|
||||
r.run(HttpRequest.GET("/calculator/multiply?x=12&y=42"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x * y = 504");
|
||||
|
||||
r.run(HttpRequest.GET("/calculator/add?x=12&y=42"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x + y = 54");
|
||||
|
||||
r.run(HttpRequest.GET("/calculator/subtract?x=42&y=12"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x - y = 30");
|
||||
//#handler2-java8-example-full
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculatorReflective() {
|
||||
//#reflective-example-full
|
||||
class TestHandler extends akka.http.javadsl.server.AllDirectives {
|
||||
RequestVal<Integer> xParam = Parameters.intValue("x");
|
||||
RequestVal<Integer> yParam = Parameters.intValue("y");
|
||||
|
||||
RequestVal<Integer> xSegment = PathMatchers.intValue();
|
||||
RequestVal<Integer> ySegment = PathMatchers.intValue();
|
||||
|
||||
|
||||
//#reflective
|
||||
public RouteResult multiply(RequestContext ctx, Integer x, Integer y) {
|
||||
int result = x * y;
|
||||
return ctx.complete("x * y = " + result);
|
||||
}
|
||||
|
||||
Route multiplyXAndYParam = handleReflectively(this, "multiply", xParam, yParam);
|
||||
//#reflective
|
||||
|
||||
Route createRoute() {
|
||||
return route(
|
||||
get(
|
||||
pathPrefix("calculator").route(
|
||||
path("multiply").route(
|
||||
multiplyXAndYParam
|
||||
),
|
||||
path("path-multiply", xSegment, ySegment).route(
|
||||
handleWith2(xSegment, ySegment, this::multiply)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// actual testing code
|
||||
TestRoute r = testRoute(new TestHandler().createRoute());
|
||||
r.run(HttpRequest.GET("/calculator/multiply?x=12&y=42"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x * y = 504");
|
||||
|
||||
r.run(HttpRequest.GET("/calculator/path-multiply/23/5"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x * y = 115");
|
||||
//#reflective-example-full
|
||||
}
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.http.javadsl.server;
|
||||
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
import akka.http.javadsl.server.*;
|
||||
import akka.http.javadsl.server.values.Parameters;
|
||||
import akka.http.javadsl.testkit.JUnitRouteTest;
|
||||
import akka.http.javadsl.testkit.TestRoute;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HandlerExampleSpec extends JUnitRouteTest {
|
||||
@Test
|
||||
public void testCalculator() {
|
||||
//#handler2-example-full
|
||||
class TestHandler extends akka.http.javadsl.server.AllDirectives {
|
||||
RequestVal<Integer> xParam = Parameters.intValue("x");
|
||||
RequestVal<Integer> yParam = Parameters.intValue("y");
|
||||
|
||||
//#handler2
|
||||
Handler2<Integer, Integer> multiply =
|
||||
(ctx, x, y) -> ctx.complete("x * y = " + (x * y));
|
||||
|
||||
Route multiplyXAndYParam = handleWith(xParam, yParam, multiply);
|
||||
//#handler2
|
||||
|
||||
Route createRoute() {
|
||||
return route(
|
||||
get(
|
||||
pathPrefix("calculator").route(
|
||||
path("multiply").route(
|
||||
multiplyXAndYParam
|
||||
),
|
||||
path("add").route(
|
||||
handleWith(xParam, yParam,
|
||||
(ctx, x, y) -> ctx.complete("x + y = " + (x + y)))
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// actual testing code
|
||||
TestRoute r = testRoute(new TestHandler().createRoute());
|
||||
r.run(HttpRequest.GET("/calculator/multiply?x=12&y=42"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x * y = 504");
|
||||
|
||||
r.run(HttpRequest.GET("/calculator/add?x=12&y=42"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x + y = 54");
|
||||
//#handler2-example-full
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue