=pro merge akka-http-tests-java8 with akka-http-tests (raw)
This commit is contained in:
parent
f6147972d7
commit
e6448dfd05
7 changed files with 51 additions and 229 deletions
16
akka-http-tests/src/test/java/AllJavaTests.java
Normal file
16
akka-http-tests/src/test/java/AllJavaTests.java
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
import akka.http.javadsl.server.HandlerBindingTest;
|
||||
import docs.http.javadsl.server.HandlerExampleDocTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
HandlerBindingTest.class,
|
||||
HandlerExampleDocTest.class
|
||||
})
|
||||
public class AllJavaTests {
|
||||
}
|
||||
|
|
@ -4,146 +4,80 @@
|
|||
|
||||
package akka.http.javadsl.server;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import akka.http.scaladsl.model.HttpRequest;
|
||||
import org.junit.Test;
|
||||
import akka.http.javadsl.testkit.*;
|
||||
import akka.http.javadsl.server.values.*;
|
||||
|
||||
public class HandlerBindingTest extends JUnitRouteTest {
|
||||
Parameter<Integer> aParam = Parameters.intValue("a");
|
||||
Parameter<Integer> bParam = Parameters.intValue("b");
|
||||
Parameter<Integer> cParam = Parameters.intValue("c");
|
||||
Parameter<Integer> dParam = Parameters.intValue("d");
|
||||
|
||||
@Test
|
||||
public void testHandlerWithoutExtractions() {
|
||||
Route route = handleWith(
|
||||
new Handler() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx) {
|
||||
return ctx.complete("Ok");
|
||||
}
|
||||
}
|
||||
);
|
||||
runRoute(route, HttpRequest.GET("/"))
|
||||
.assertEntity("Ok");
|
||||
}
|
||||
@Test
|
||||
public void testHandlerWithSomeExtractions() {
|
||||
final Parameter<Integer> a = Parameters.intValue("a");
|
||||
final Parameter<Integer> b = Parameters.intValue("b");
|
||||
|
||||
Route route = handleWith(
|
||||
new Handler() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx) {
|
||||
return ctx.complete("Ok a:" + a.get(ctx) + " b:" + b.get(ctx));
|
||||
}
|
||||
}, a, b
|
||||
);
|
||||
runRoute(route, HttpRequest.GET("?a=23&b=42"))
|
||||
.assertEntity("Ok a:23 b:42");
|
||||
}
|
||||
@Test
|
||||
public void testHandlerIfExtractionFails() {
|
||||
final Parameter<Integer> a = Parameters.intValue("a");
|
||||
|
||||
Route route = handleWith(
|
||||
new Handler() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx) {
|
||||
return ctx.complete("Ok " + a.get(ctx));
|
||||
}
|
||||
}, a
|
||||
);
|
||||
runRoute(route, HttpRequest.GET("/"))
|
||||
.assertStatusCode(404)
|
||||
.assertEntity("Request is missing required query parameter 'a'");
|
||||
Route route = handleWith(ctx -> ctx.complete("Ok"));
|
||||
TestResponse response = runRoute(route, HttpRequest.GET("/"));
|
||||
response.assertEntity("Ok");
|
||||
}
|
||||
@Test
|
||||
public void testHandler1() {
|
||||
final Parameter<Integer> a = Parameters.intValue("a");
|
||||
|
||||
Route route = handleWith1(a,
|
||||
new Handler1<Integer>() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx, Integer a) {
|
||||
return ctx.complete("Ok " + a);
|
||||
}
|
||||
}
|
||||
);
|
||||
runRoute(route, HttpRequest.GET("?a=23"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("Ok 23");
|
||||
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");
|
||||
}
|
||||
@Test
|
||||
public void testHandler2() {
|
||||
Route route = handleWith2(
|
||||
Parameters.intValue("a"),
|
||||
Parameters.intValue("b"),
|
||||
new Handler2<Integer, Integer>() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx, Integer a, Integer b) {
|
||||
return ctx.complete("Sum: " + (a + b));
|
||||
}
|
||||
}
|
||||
);
|
||||
runRoute(route, HttpRequest.GET("?a=23&b=42"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("Sum: 65");
|
||||
Route route =
|
||||
handleWith2(
|
||||
aParam,
|
||||
bParam,
|
||||
(ctx, a, b) -> ctx.complete("Sum: " + (a + b)));
|
||||
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42"));
|
||||
response.assertStatusCode(200);
|
||||
response.assertEntity("Sum: 65");
|
||||
}
|
||||
@Test
|
||||
public void testHandler3() {
|
||||
Route route = handleWith3(
|
||||
Parameters.intValue("a"),
|
||||
Parameters.intValue("b"),
|
||||
Parameters.intValue("c"),
|
||||
new Handler3<Integer, Integer, Integer>() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx, Integer a, Integer b, Integer c) {
|
||||
return ctx.complete("Sum: " + (a + b + c));
|
||||
}
|
||||
}
|
||||
);
|
||||
Route route =
|
||||
handleWith3(
|
||||
aParam,
|
||||
bParam,
|
||||
cParam,
|
||||
(ctx, a, b, c) -> ctx.complete("Sum: " + (a + b + c)));
|
||||
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42&c=30"));
|
||||
response.assertStatusCode(200);
|
||||
response.assertEntity("Sum: 95");
|
||||
}
|
||||
@Test
|
||||
public void testHandler4() {
|
||||
Route route = handleWith4(
|
||||
Parameters.intValue("a"),
|
||||
Parameters.intValue("b"),
|
||||
Parameters.intValue("c"),
|
||||
Parameters.intValue("d"),
|
||||
new Handler4<Integer, Integer, Integer, Integer>() {
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx, Integer a, Integer b, Integer c, Integer d) {
|
||||
return ctx.complete("Sum: " + (a + b + c + d));
|
||||
}
|
||||
}
|
||||
);
|
||||
runRoute(route, HttpRequest.GET("?a=23&b=42&c=30&d=45"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("Sum: 140");
|
||||
Route route =
|
||||
handleWith4(
|
||||
aParam,
|
||||
bParam,
|
||||
cParam,
|
||||
dParam,
|
||||
(ctx, a, b, c, d) -> ctx.complete("Sum: " + (a + b + c + d)));
|
||||
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42&c=30&d=45"));
|
||||
response.assertStatusCode(200);
|
||||
response.assertEntity("Sum: 140");
|
||||
}
|
||||
public RouteResult sum(RequestContext ctx, int a, int b, int c, int d) {
|
||||
return ctx.complete("Sum: "+(a + b + c + d));
|
||||
}
|
||||
@Test
|
||||
public void testReflectiveInstanceHandler() {
|
||||
class Test {
|
||||
public RouteResult negate(RequestContext ctx, int a) {
|
||||
return ctx.complete("Negated: " + (- a));
|
||||
}
|
||||
}
|
||||
Route route = handleReflectively(new Test(), "negate", Parameters.intValue("a"));
|
||||
runRoute(route, HttpRequest.GET("?a=23"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("Negated: -23");
|
||||
}
|
||||
|
||||
public static RouteResult squared(RequestContext ctx, int a) {
|
||||
return ctx.complete("Squared: " + (a * a));
|
||||
}
|
||||
@Test
|
||||
public void testStaticReflectiveHandler() {
|
||||
Route route = handleReflectively(HandlerBindingTest.class, "squared", Parameters.intValue("a"));
|
||||
runRoute(route, HttpRequest.GET("?a=23"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("Squared: 529");
|
||||
public void testHandler4MethodRef() {
|
||||
Route route =
|
||||
handleWith4(
|
||||
aParam,
|
||||
bParam,
|
||||
cParam,
|
||||
dParam,
|
||||
this::sum);
|
||||
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42&c=30&d=45"));
|
||||
response.assertStatusCode(200);
|
||||
response.assertEntity("Sum: 140");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,312 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.http.javadsl.server;
|
||||
|
||||
import akka.dispatch.Mapper;
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
import akka.http.javadsl.model.HttpResponse;
|
||||
import akka.http.javadsl.model.StatusCodes;
|
||||
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;
|
||||
import scala.concurrent.ExecutionContext;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
public class HandlerExampleDocTest extends JUnitRouteTest {
|
||||
@Test
|
||||
public void testSimpleHandler() {
|
||||
//#simple-handler-example-full
|
||||
class TestHandler extends akka.http.javadsl.server.AllDirectives {
|
||||
//#simple-handler
|
||||
Handler handlerString = new Handler() {
|
||||
static final long serialVersionUID = 1L;
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx) {
|
||||
return ctx.complete(String.format("This was a %s request to %s",
|
||||
ctx.request().method().value(), ctx.request().getUri()));
|
||||
}
|
||||
};
|
||||
Handler handlerResponse = new Handler() {
|
||||
static final long serialVersionUID = 1L;
|
||||
@Override
|
||||
public RouteResult apply(RequestContext ctx) {
|
||||
// with full control over the returned HttpResponse:
|
||||
final HttpResponse response = HttpResponse.create()
|
||||
.withEntity(String.format("Accepted %s request to %s",
|
||||
ctx.request().method().value(), ctx.request().getUri()))
|
||||
.withStatus(StatusCodes.ACCEPTED);
|
||||
return ctx.complete(response);
|
||||
}
|
||||
};
|
||||
//#simple-handler
|
||||
|
||||
Route createRoute() {
|
||||
return route(
|
||||
get(
|
||||
handleWith(handlerString)
|
||||
),
|
||||
post(
|
||||
path("abc").route(
|
||||
handleWith(handlerResponse)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// actual testing code
|
||||
TestRoute r = testRoute(new TestHandler().createRoute());
|
||||
r.run(HttpRequest.GET("/test"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("This was a GET request to http://example.com/test");
|
||||
|
||||
r.run(HttpRequest.POST("/test"))
|
||||
.assertStatusCode(404);
|
||||
|
||||
r.run(HttpRequest.POST("/abc"))
|
||||
.assertStatusCode(202)
|
||||
.assertEntity("Accepted POST request to http://example.com/abc");
|
||||
//#simple-handler-example-full
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculator() {
|
||||
//#handler2-example-full
|
||||
class TestHandler extends akka.http.javadsl.server.AllDirectives {
|
||||
final RequestVal<Integer> xParam = Parameters.intValue("x");
|
||||
final RequestVal<Integer> yParam = Parameters.intValue("y");
|
||||
|
||||
final RequestVal<Integer> xSegment = PathMatchers.intValue();
|
||||
final RequestVal<Integer> ySegment = PathMatchers.intValue();
|
||||
|
||||
//#handler2
|
||||
final Handler2<Integer, Integer> multiply =
|
||||
new Handler2<Integer, Integer>() {
|
||||
static final long serialVersionUID = 1L;
|
||||
@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
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeferredResultAsyncHandler() {
|
||||
//#async-example-full
|
||||
//#async-service-definition
|
||||
class CalculatorService {
|
||||
public Future<Integer> multiply(final int x, final int y, ExecutionContext ec) {
|
||||
return akka.dispatch.Futures.future(() -> x * y, ec);
|
||||
}
|
||||
|
||||
public Future<Integer> add(final int x, final int y, ExecutionContext ec) {
|
||||
return akka.dispatch.Futures.future(() -> x + y, ec);
|
||||
}
|
||||
}
|
||||
//#async-service-definition
|
||||
|
||||
class TestHandler extends akka.http.javadsl.server.AllDirectives {
|
||||
RequestVal<Integer> xParam = Parameters.intValue("x");
|
||||
RequestVal<Integer> yParam = Parameters.intValue("y");
|
||||
|
||||
//#async-handler-1
|
||||
// would probably be injected or passed at construction time in real code
|
||||
CalculatorService calculatorService = new CalculatorService();
|
||||
public Future<RouteResult> multiplyAsync(final RequestContext ctx, int x, int y) {
|
||||
Future<Integer> result = calculatorService.multiply(x, y, ctx.executionContext());
|
||||
Mapper<Integer, RouteResult> func = new Mapper<Integer, RouteResult>() {
|
||||
@Override
|
||||
public RouteResult apply(Integer product) {
|
||||
return ctx.complete("x * y = " + product);
|
||||
}
|
||||
}; // cannot be written as lambda, unfortunately
|
||||
return result.map(func, ctx.executionContext());
|
||||
}
|
||||
Route multiplyAsyncRoute =
|
||||
path("multiply").route(
|
||||
handleWithAsync2(xParam, yParam, this::multiplyAsync)
|
||||
);
|
||||
//#async-handler-1
|
||||
|
||||
//#async-handler-2
|
||||
public RouteResult addAsync(final RequestContext ctx, int x, int y) {
|
||||
Future<Integer> result = calculatorService.add(x, y, ctx.executionContext());
|
||||
Mapper<Integer, RouteResult> func = new Mapper<Integer, RouteResult>() {
|
||||
@Override
|
||||
public RouteResult apply(Integer sum) {
|
||||
return ctx.complete("x + y = " + sum);
|
||||
}
|
||||
}; // cannot be written as lambda, unfortunately
|
||||
return ctx.completeWith(result.map(func, ctx.executionContext()));
|
||||
}
|
||||
Route addAsyncRoute =
|
||||
path("add").route(
|
||||
handleWith2(xParam, yParam, this::addAsync)
|
||||
);
|
||||
//#async-handler-2
|
||||
|
||||
Route createRoute() {
|
||||
return route(
|
||||
get(
|
||||
pathPrefix("calculator").route(
|
||||
multiplyAsyncRoute,
|
||||
addAsyncRoute
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 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=23&y=5"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x + y = 28");
|
||||
//#async-example-full
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue