|
| 1 | +package org.mapfish.print; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.URI; |
| 7 | +import java.net.URISyntaxException; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import java.util.stream.Stream; |
| 10 | +import org.json.JSONArray; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.mapfish.print.servlet.MapPrinterServlet; |
| 15 | +import org.springframework.http.HttpMethod; |
| 16 | +import org.springframework.http.HttpStatus; |
| 17 | +import org.springframework.http.client.ClientHttpRequest; |
| 18 | +import org.springframework.http.client.ClientHttpResponse; |
| 19 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 20 | + |
| 21 | +/** |
| 22 | + * Test the servlet print API. |
| 23 | + * |
| 24 | + * <p>To run this test make sure that the test servers are running: |
| 25 | + * |
| 26 | + * <p>./gradlew examples:farmRun |
| 27 | + * |
| 28 | + * <p>Or run the tests with the following task (which automatically starts the servers): |
| 29 | + * |
| 30 | + * <p>./gradlew examples:geoserver |
| 31 | + */ |
| 32 | +@ExtendWith(SpringExtension.class) |
| 33 | +public class ContextPathTest extends AbstractApiTest { |
| 34 | + |
| 35 | + protected static final String PRINT_SERVER = "http://print-context-path:8080/"; |
| 36 | + |
| 37 | + protected ClientHttpRequest getRequest(String path, HttpMethod method) |
| 38 | + throws IOException, URISyntaxException { |
| 39 | + var actualUrl = |
| 40 | + Stream.of(path.split("/")).filter(s -> !s.isEmpty()).collect(Collectors.joining("/")); |
| 41 | + return httpRequestFactory.createRequest(new URI(PRINT_SERVER + actualUrl), method); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @EnabledIfEnvironmentVariable(named = "MAPFISH_PRINT_TESTS_CLUSTER_MODE", matches = "true") |
| 46 | + public void testIndexRedirect() throws Exception { |
| 47 | + ClientHttpRequest request = getRequest("print", HttpMethod.GET); |
| 48 | + try (ClientHttpResponse response = request.execute()) { |
| 49 | + assertEquals(HttpStatus.FOUND, response.getStatusCode()); |
| 50 | + assertEquals("/print/", response.getHeaders().getLocation().toString()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @EnabledIfEnvironmentVariable(named = "MAPFISH_PRINT_TESTS_CLUSTER_MODE", matches = "true") |
| 56 | + public void testIndex() throws Exception { |
| 57 | + ClientHttpRequest request = getRequest("print/", HttpMethod.GET); |
| 58 | + try (ClientHttpResponse response = request.execute()) { |
| 59 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 60 | + assertEquals("text/html", response.getHeaders().getContentType().toString()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @EnabledIfEnvironmentVariable(named = "MAPFISH_PRINT_TESTS_CLUSTER_MODE", matches = "true") |
| 66 | + public void testListApps() throws Exception { |
| 67 | + ClientHttpRequest request = |
| 68 | + getRequest("print" + MapPrinterServlet.LIST_APPS_URL, HttpMethod.GET); |
| 69 | + try (ClientHttpResponse response = request.execute()) { |
| 70 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 71 | + assertEquals(getJsonMediaType(), response.getHeaders().getContentType()); |
| 72 | + final JSONArray appIdsJson = new JSONArray(getBodyAsText(response)); |
| 73 | + assertFalse(appIdsJson.isEmpty()); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments