|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.server.e2e; |
| 18 | + |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | + |
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.InputStreamReader; |
| 24 | +import java.net.ServerSocket; |
| 25 | +import java.net.URI; |
| 26 | +import java.net.http.HttpClient; |
| 27 | +import java.net.http.HttpRequest; |
| 28 | +import java.net.http.HttpResponse; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.nio.file.Paths; |
| 32 | +import java.time.Duration; |
| 33 | +import java.time.Instant; |
| 34 | + |
| 35 | +import org.junit.jupiter.api.AfterEach; |
| 36 | +import org.junit.jupiter.api.Assumptions; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Tag; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.slf4j.Logger; |
| 41 | +import org.slf4j.LoggerFactory; |
| 42 | + |
| 43 | +/** |
| 44 | + * End-to-end test verifying that tika-server-standard supports HTTP/2 (h2c cleartext). |
| 45 | + * |
| 46 | + * Starts the real fat-jar, sends a request using Java's HttpClient configured for HTTP/2, |
| 47 | + * and asserts the response was served over HTTP/2. This validates the runtime classpath |
| 48 | + * has the Jetty http2-server jar and CXF negotiates h2c correctly. |
| 49 | + * |
| 50 | + * Run with: mvn test -pl tika-e2e-tests/tika-server -Pe2e |
| 51 | + * |
| 52 | + * Inspired by Lawrence Moorehead's original contribution (elemdisc/tika PR#1, TIKA-4679). |
| 53 | + */ |
| 54 | +@Tag("E2ETest") |
| 55 | +public class TikaServerHttp2Test { |
| 56 | + |
| 57 | + private static final Logger log = LoggerFactory.getLogger(TikaServerHttp2Test.class); |
| 58 | + private static final long SERVER_STARTUP_TIMEOUT_MS = 90_000; |
| 59 | + /** Health-check polls root (/), which always returns 200 without requiring endpoint config. */ |
| 60 | + private static final String HEALTH_PATH = "/"; |
| 61 | + |
| 62 | + private Process serverProcess; |
| 63 | + private int port; |
| 64 | + private String endPoint; |
| 65 | + |
| 66 | + @BeforeEach |
| 67 | + void startServer() throws Exception { |
| 68 | + port = findFreePort(); |
| 69 | + endPoint = "http://localhost:" + port; |
| 70 | + |
| 71 | + String serverHome = System.getProperty("tika.server.home"); |
| 72 | + if (serverHome == null) { |
| 73 | + // fall back to conventional location relative to this module |
| 74 | + Path repoRoot = Paths.get("").toAbsolutePath(); |
| 75 | + while (repoRoot != null && !repoRoot.resolve("tika-server").toFile().isDirectory()) { |
| 76 | + repoRoot = repoRoot.getParent(); |
| 77 | + } |
| 78 | + if (repoRoot == null) { |
| 79 | + throw new IllegalStateException("Cannot locate tika root. Pass -Dtika.server.home=/path/to/extracted-assembly"); |
| 80 | + } |
| 81 | + serverHome = repoRoot.resolve("tika-e2e-tests/tika-server/target/tika-server-dist").toAbsolutePath().toString(); |
| 82 | + } |
| 83 | + |
| 84 | + Path serverJar = Paths.get(serverHome, "tika-server.jar"); |
| 85 | + Assumptions.assumeTrue(Files.exists(serverJar), |
| 86 | + "tika-server.jar not found at " + serverJar + "; skipping HTTP/2 e2e test. " + |
| 87 | + "Build with: mvn package -pl tika-server/tika-server-standard && " + |
| 88 | + "mvn test -pl tika-e2e-tests/tika-server -Pe2e"); |
| 89 | + |
| 90 | + log.info("Starting tika-server from: {}", serverJar); |
| 91 | + ProcessBuilder pb = new ProcessBuilder( |
| 92 | + "java", "-jar", "tika-server.jar", |
| 93 | + "-p", String.valueOf(port), |
| 94 | + "-h", "localhost" |
| 95 | + ); |
| 96 | + pb.directory(Paths.get(serverHome).toFile()); |
| 97 | + pb.redirectErrorStream(true); |
| 98 | + serverProcess = pb.start(); |
| 99 | + |
| 100 | + // Drain output in background so the process doesn't block |
| 101 | + Thread drainThread = new Thread(() -> { |
| 102 | + try (BufferedReader reader = new BufferedReader( |
| 103 | + new InputStreamReader(serverProcess.getInputStream(), UTF_8))) { |
| 104 | + String line; |
| 105 | + while ((line = reader.readLine()) != null) { |
| 106 | + log.info("tika-server: {}", line); |
| 107 | + } |
| 108 | + } catch (Exception e) { |
| 109 | + log.debug("Server output stream closed", e); |
| 110 | + } |
| 111 | + }); |
| 112 | + drainThread.setDaemon(true); |
| 113 | + drainThread.start(); |
| 114 | + |
| 115 | + awaitServerStartup(); |
| 116 | + } |
| 117 | + |
| 118 | + @AfterEach |
| 119 | + void stopServer() throws Exception { |
| 120 | + if (serverProcess != null && serverProcess.isAlive()) { |
| 121 | + serverProcess.destroy(); |
| 122 | + if (!serverProcess.waitFor(5, java.util.concurrent.TimeUnit.SECONDS)) { |
| 123 | + serverProcess.destroyForcibly(); |
| 124 | + serverProcess.waitFor(30, java.util.concurrent.TimeUnit.SECONDS); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void testH2cTikaEndpoint() throws Exception { |
| 131 | + HttpClient httpClient = HttpClient.newBuilder() |
| 132 | + .version(HttpClient.Version.HTTP_2) |
| 133 | + .build(); |
| 134 | + HttpRequest request = HttpRequest.newBuilder() |
| 135 | + .uri(URI.create(endPoint + "/tika")) |
| 136 | + .header("Accept", "text/plain") |
| 137 | + .GET() |
| 138 | + .build(); |
| 139 | + |
| 140 | + HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString(UTF_8)); |
| 141 | + |
| 142 | + assertEquals(200, response.statusCode(), "Expected 200 from /tika"); |
| 143 | + assertEquals(HttpClient.Version.HTTP_2, response.version(), |
| 144 | + "Expected HTTP/2 protocol; server may be missing http2-server on classpath"); |
| 145 | + log.info("HTTP/2 h2c verified: {} {}", response.statusCode(), response.version()); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testH2cParseEndpoint() throws Exception { |
| 150 | + HttpClient httpClient = HttpClient.newBuilder() |
| 151 | + .version(HttpClient.Version.HTTP_2) |
| 152 | + .build(); |
| 153 | + |
| 154 | + // First: GET / to negotiate h2c upgrade, establishing an HTTP/2 connection |
| 155 | + HttpRequest warmup = HttpRequest.newBuilder() |
| 156 | + .uri(URI.create(endPoint + "/")) |
| 157 | + .GET() |
| 158 | + .build(); |
| 159 | + httpClient.send(warmup, HttpResponse.BodyHandlers.discarding()); |
| 160 | + |
| 161 | + // Now PUT /tika — the existing HTTP/2 connection is reused |
| 162 | + byte[] body = "Hello, HTTP/2 world!".getBytes(UTF_8); |
| 163 | + HttpRequest request = HttpRequest.newBuilder() |
| 164 | + .uri(URI.create(endPoint + "/tika")) |
| 165 | + .header("Content-Type", "text/plain") |
| 166 | + .PUT(HttpRequest.BodyPublishers.ofByteArray(body)) |
| 167 | + .build(); |
| 168 | + |
| 169 | + HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString(UTF_8)); |
| 170 | + |
| 171 | + assertEquals(200, response.statusCode(), "Expected 200 from /tika"); |
| 172 | + assertEquals(HttpClient.Version.HTTP_2, response.version(), |
| 173 | + "Expected HTTP/2 protocol on /tika endpoint"); |
| 174 | + log.info("HTTP/2 parse endpoint verified: {} bytes returned over {}", response.body().length(), response.version()); |
| 175 | + } |
| 176 | + |
| 177 | + private void awaitServerStartup() throws Exception { |
| 178 | + // Use HTTP/1.1 for the health-check poll so we don't depend on HTTP/2 during startup. |
| 179 | + // Both connectTimeout and request timeout are set to avoid hanging when Jetty has bound |
| 180 | + // the port but CXF has not yet finished initializing (accepts TCP but doesn't respond). |
| 181 | + HttpClient pollClient = HttpClient.newBuilder() |
| 182 | + .version(HttpClient.Version.HTTP_1_1) |
| 183 | + .connectTimeout(Duration.ofSeconds(5)) |
| 184 | + .build(); |
| 185 | + |
| 186 | + Instant deadline = Instant.now().plusMillis(SERVER_STARTUP_TIMEOUT_MS); |
| 187 | + while (Instant.now().isBefore(deadline)) { |
| 188 | + if (!serverProcess.isAlive()) { |
| 189 | + throw new IllegalStateException( |
| 190 | + "tika-server process exited unexpectedly with code " + serverProcess.exitValue()); |
| 191 | + } |
| 192 | + try { |
| 193 | + HttpRequest pollRequest = HttpRequest.newBuilder() |
| 194 | + .uri(URI.create(endPoint + HEALTH_PATH)) |
| 195 | + .timeout(Duration.ofSeconds(5)) |
| 196 | + .GET() |
| 197 | + .build(); |
| 198 | + HttpResponse<Void> resp = pollClient.send(pollRequest, HttpResponse.BodyHandlers.discarding()); |
| 199 | + if (resp.statusCode() == 200) { |
| 200 | + log.info("tika-server ready on port {}", port); |
| 201 | + return; |
| 202 | + } |
| 203 | + log.debug("Server returned {} on {}; still waiting...", resp.statusCode(), HEALTH_PATH); |
| 204 | + } catch (Exception e) { |
| 205 | + log.debug("Waiting for server on port {}: {}", port, e.getMessage()); |
| 206 | + } |
| 207 | + Thread.sleep(1000); |
| 208 | + } |
| 209 | + throw new IllegalStateException("tika-server did not start within " + SERVER_STARTUP_TIMEOUT_MS + " ms"); |
| 210 | + } |
| 211 | + |
| 212 | + private static int findFreePort() throws Exception { |
| 213 | + try (ServerSocket s = new ServerSocket(0)) { |
| 214 | + return s.getLocalPort(); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments