diff --git a/brouter-server/build.gradle b/brouter-server/build.gradle index 2e377ab..c52d524 100644 --- a/brouter-server/build.gradle +++ b/brouter-server/build.gradle @@ -70,8 +70,6 @@ distributions { } dependencies { - testImplementation 'junit:junit:4.13.1' - implementation project(':brouter-codec') implementation project(':brouter-core') implementation project(':brouter-expressions') @@ -79,4 +77,6 @@ dependencies { implementation project(':brouter-mapaccess') implementation project(':brouter-util') + testImplementation 'junit:junit:4.13.2' + testImplementation 'org.json:json:20180813' } diff --git a/brouter-server/src/main/java/btools/server/request/RequestHandler.java b/brouter-server/src/main/java/btools/server/request/RequestHandler.java index fb69d30..5364c5b 100644 --- a/brouter-server/src/main/java/btools/server/request/RequestHandler.java +++ b/brouter-server/src/main/java/btools/server/request/RequestHandler.java @@ -1,7 +1,7 @@ package btools.server.request; -import java.util.HashMap; import java.util.List; +import java.util.Map; import btools.router.OsmNodeNamed; import btools.router.OsmTrack; @@ -10,9 +10,9 @@ import btools.server.ServiceContext; public abstract class RequestHandler { protected ServiceContext serviceContext; - protected HashMap params; + protected Map params; - public RequestHandler(ServiceContext serviceContext, HashMap params) { + public RequestHandler(ServiceContext serviceContext, Map params) { this.serviceContext = serviceContext; this.params = params; } diff --git a/brouter-server/src/test/java/btools/server/RequestHandlerTest.java b/brouter-server/src/test/java/btools/server/RequestHandlerTest.java new file mode 100644 index 0000000..2d5f698 --- /dev/null +++ b/brouter-server/src/test/java/btools/server/RequestHandlerTest.java @@ -0,0 +1,27 @@ +package btools.server; + +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +import java.util.HashMap; + +import btools.router.RoutingContext; +import btools.server.request.ServerHandler; + +public class RequestHandlerTest { + @Test + @Ignore("Parameters are currently handled by RouteServer, not RequestHandler") + public void parseParameters() { + HashMap params = new HashMap<>(); + params.put("lonlats", "8.799297,49.565883|8.811764,49.563606"); + params.put("profile", "trekking"); + params.put("alternativeidx", "0"); + params.put("profile:test", "bar"); + ServerHandler serverHandler = new ServerHandler(null, params); + RoutingContext routingContext = serverHandler.readRoutingContext(); + + Assert.assertEquals("trekking", routingContext.localFunction); + Assert.assertEquals("bar", routingContext.keyValues.get("test")); + } +} diff --git a/brouter-server/src/test/java/btools/server/RouteServerTest.java b/brouter-server/src/test/java/btools/server/RouteServerTest.java new file mode 100644 index 0000000..a1bc161 --- /dev/null +++ b/brouter-server/src/test/java/btools/server/RouteServerTest.java @@ -0,0 +1,115 @@ +package btools.server; + + +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.ConnectException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +public class RouteServerTest { + private static final String host = "localhost"; + private static final String port = "17777"; + private static final String baseUrl = "http://" + host + ":" + port + "/"; + + @BeforeClass + public static void setupServer() throws IOException { + File workingDir = new File(".").getCanonicalFile(); + File segmentDir = new File(workingDir, "../brouter-map-creator/build/resources/test/tmp/segments"); + File profileDir = new File(workingDir, "../misc/profiles2"); + File customProfileDir = workingDir; + + Runnable runnable = () -> { + try { + RouteServer.main(new String[]{segmentDir.getAbsolutePath(), profileDir.getAbsolutePath(), customProfileDir.getAbsolutePath(), port, "1"}); + } catch (Exception e) { + e.printStackTrace(); + } + }; + + Thread thread = new Thread(runnable); + thread.start(); + + // Busy-wait for server startup + URL requestUrl = new URL(baseUrl); + HttpURLConnection httpConnection = (HttpURLConnection) requestUrl.openConnection(); + for (int i = 10; i >= 0; i--) { + try { + httpConnection.setConnectTimeout(10); + httpConnection.connect(); + break; + } catch (ConnectException e) { + if (i == 0) { + throw e; + } + } + } + } + + @Test + public void defaultRouteTrekking() throws IOException { + URL requestUrl = new URL(baseUrl + "brouter?lonlats=8.723037,50.000491|8.712737,50.002899&nogos=&profile=trekking&alternativeidx=0&format=geojson"); + + HttpURLConnection httpConnection = (HttpURLConnection) requestUrl.openConnection(); + httpConnection.connect(); + + Assert.assertEquals(HttpURLConnection.HTTP_OK, httpConnection.getResponseCode()); + + InputStream inputStream = httpConnection.getInputStream(); + JSONObject geoJson = new JSONObject(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)); + Assert.assertEquals("1204", geoJson.query("/features/0/properties/track-length")); + } + + @Test + public void overrideParameter() throws IOException { + URL requestUrl = new URL(baseUrl + "brouter?lonlats=8.723037,50.000491|8.712737,50.002899&nogos=&profile=trekking&alternativeidx=0&format=geojson&profile:avoid_unsafe=1"); + HttpURLConnection httpConnection = (HttpURLConnection) requestUrl.openConnection(); + httpConnection.connect(); + + Assert.assertEquals(HttpURLConnection.HTTP_OK, httpConnection.getResponseCode()); + + InputStream inputStream = httpConnection.getInputStream(); + JSONObject geoJson = new JSONObject(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)); + Assert.assertEquals("1902", geoJson.query("/features/0/properties/track-length")); + } + + @Test + public void robots() throws IOException { + URL requestUrl = new URL(baseUrl + "robots.txt"); + HttpURLConnection httpConnection = (HttpURLConnection) requestUrl.openConnection(); + httpConnection.connect(); + + Assert.assertEquals(HttpURLConnection.HTTP_OK, httpConnection.getResponseCode()); + + String content = new String(httpConnection.getInputStream().readAllBytes(), StandardCharsets.UTF_8); + Assert.assertTrue(content.contains("Disallow: /")); + } + + @Test + public void invalidUrl() throws IOException { + URL requestUrl = new URL(baseUrl + "invalid"); + HttpURLConnection httpConnection = (HttpURLConnection) requestUrl.openConnection(); + httpConnection.connect(); + + Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, httpConnection.getResponseCode()); + } + + @Test + @Ignore("Broken implementation (uppercase / lowercase issue)") + public void invalidReferrer() throws IOException { + URL requestUrl = new URL(baseUrl + "brouter/%7C/%2C"); + HttpURLConnection httpConnection = (HttpURLConnection) requestUrl.openConnection(); + httpConnection.setRequestProperty("Referer", "http://brouter.de/brouter-web/"); + httpConnection.connect(); + + Assert.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, httpConnection.getResponseCode()); + } +}