2015-05-21 17:17:55 +02:00
|
|
|
/*
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
|
2015-05-21 17:17:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.http.javadsl.server;
|
|
|
|
|
|
2015-05-07 17:00:45 +02:00
|
|
|
import akka.http.scaladsl.model.HttpRequest;
|
2016-01-13 01:00:15 +01:00
|
|
|
import org.junit.Test;
|
2015-05-21 17:17:55 +02:00
|
|
|
import akka.http.javadsl.testkit.*;
|
2015-05-07 17:00:45 +02:00
|
|
|
import akka.http.javadsl.server.values.*;
|
2015-05-21 17:17:55 +02:00
|
|
|
|
|
|
|
|
public class HandlerBindingTest extends JUnitRouteTest {
|
2016-01-13 01:00:15 +01:00
|
|
|
Parameter<Integer> aParam = Parameters.intValue("a");
|
|
|
|
|
Parameter<Integer> bParam = Parameters.intValue("b");
|
|
|
|
|
Parameter<Integer> cParam = Parameters.intValue("c");
|
|
|
|
|
Parameter<Integer> dParam = Parameters.intValue("d");
|
|
|
|
|
|
2015-05-21 17:17:55 +02:00
|
|
|
@Test
|
|
|
|
|
public void testHandlerWithoutExtractions() {
|
2016-01-13 01:00:15 +01:00
|
|
|
Route route = handleWith(ctx -> ctx.complete("Ok"));
|
|
|
|
|
TestResponse response = runRoute(route, HttpRequest.GET("/"));
|
|
|
|
|
response.assertEntity("Ok");
|
2015-05-21 17:17:55 +02:00
|
|
|
}
|
|
|
|
|
@Test
|
|
|
|
|
public void testHandler1() {
|
2016-01-13 01:00:15 +01:00
|
|
|
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");
|
2015-05-21 17:17:55 +02:00
|
|
|
}
|
|
|
|
|
@Test
|
|
|
|
|
public void testHandler2() {
|
2016-01-13 01:00:15 +01:00
|
|
|
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");
|
2015-05-21 17:17:55 +02:00
|
|
|
}
|
|
|
|
|
@Test
|
|
|
|
|
public void testHandler3() {
|
2016-01-13 01:00:15 +01:00
|
|
|
Route route =
|
|
|
|
|
handleWith3(
|
|
|
|
|
aParam,
|
|
|
|
|
bParam,
|
|
|
|
|
cParam,
|
|
|
|
|
(ctx, a, b, c) -> ctx.complete("Sum: " + (a + b + c)));
|
2015-05-21 17:17:55 +02:00
|
|
|
TestResponse response = runRoute(route, HttpRequest.GET("?a=23&b=42&c=30"));
|
|
|
|
|
response.assertStatusCode(200);
|
|
|
|
|
response.assertEntity("Sum: 95");
|
|
|
|
|
}
|
|
|
|
|
@Test
|
|
|
|
|
public void testHandler4() {
|
2016-01-13 01:00:15 +01:00
|
|
|
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");
|
2015-05-21 17:17:55 +02:00
|
|
|
}
|
2016-01-13 01:00:15 +01:00
|
|
|
public RouteResult sum(RequestContext ctx, int a, int b, int c, int d) {
|
|
|
|
|
return ctx.complete("Sum: "+(a + b + c + d));
|
2015-05-21 17:17:55 +02:00
|
|
|
}
|
|
|
|
|
@Test
|
2016-01-13 01:00:15 +01:00
|
|
|
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");
|
2015-05-21 17:17:55 +02:00
|
|
|
}
|
|
|
|
|
}
|