Skip to content

Commit

Permalink
Integrated review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuch committed Nov 13, 2024
1 parent 7c3ad2c commit d401c83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
13 changes: 6 additions & 7 deletions test/jdk/java/net/httpclient/http2/StreamFlowControlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,19 @@
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

import jdk.httpclient.test.lib.common.HttpServerAdapters;
import jdk.httpclient.test.lib.common.HttpServerAdapters.HttpHeadHandler;
import jdk.httpclient.test.lib.common.HttpServerAdapters.HttpHeadOrGetHandler;
import jdk.httpclient.test.lib.common.HttpServerAdapters.HttpTestServer;
import jdk.httpclient.test.lib.http2.BodyOutputStream;
import jdk.httpclient.test.lib.http2.Http2Handler;
Expand Down Expand Up @@ -269,7 +266,7 @@ public void setup() throws Exception {
var https2TestServer = new Http2TestServer("localhost", true, sslContext);
https2TestServer.addHandler(new Http2TestHandler(), "/https2/");
this.https2TestServer = HttpTestServer.of(https2TestServer);
this.https2TestServer.addHandler(new HttpHeadHandler(), "/https2/head/");
this.https2TestServer.addHandler(new HttpHeadOrGetHandler(), "/https2/head/");
https2URI = "https://" + this.https2TestServer.serverAuthority() + "/https2/x";
String h2Head = "https://" + this.https2TestServer.serverAuthority() + "/https2/head/z";

Expand Down Expand Up @@ -352,8 +349,10 @@ public void handle(Http2TestExchange t) throws IOException {
} finally {
if (t instanceof FCHttp2TestExchange fct) {
fct.responseSent(query);
} else fail("Exchange is not %s but %s"
.formatted(FCHttp2TestExchange.class.getName(), t.getClass().getName()));
} else {
fail("Exchange is not %s but %s"
.formatted(FCHttp2TestExchange.class.getName(), t.getClass().getName()));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,41 +431,44 @@ private void doHandle(HttpTestExchange t) throws IOException {
* an empty body.
* The response is always returned with fixed length.
*/
public static class HttpHeadHandler implements HttpTestHandler {
public static class HttpHeadOrGetHandler implements HttpTestHandler {
final String responseBody;
public HttpHeadHandler() {
public HttpHeadOrGetHandler() {
this("pâté de tête persillé");
}
public HttpHeadHandler(String responseBody) {
public HttpHeadOrGetHandler(String responseBody) {
this.responseBody = Objects.requireNonNull(responseBody);
}

@Override
public void handle(HttpTestExchange t) throws IOException {
t.getRequestBody().readAllBytes();
String method = t.getRequestMethod();
switch (method) {
case "HEAD" -> {
byte[] resp = responseBody.getBytes(StandardCharsets.UTF_8);
if (t.getExchangeVersion() != HTTP_1_1) {
// with HTTP/2 or HTTP/3 the server will not send content-length
t.getResponseHeaders().addHeader("Content-Length", String.valueOf(resp.length));
try (var exchg = t) {
exchg.getRequestBody().readAllBytes();
String method = exchg.getRequestMethod();
switch (method) {
case "HEAD" -> {
byte[] resp = responseBody.getBytes(StandardCharsets.UTF_8);
if (exchg.getExchangeVersion() != HTTP_1_1) {
// with HTTP/2 or HTTP/3 the server will not send content-length
exchg.getResponseHeaders()
.addHeader("Content-Length", String.valueOf(resp.length));
}
exchg.sendResponseHeaders(200, resp.length);
exchg.getResponseBody().close();
}
t.sendResponseHeaders(200, resp.length);
}
case "GET" -> {
byte[] resp = responseBody.getBytes(StandardCharsets.UTF_8);
t.sendResponseHeaders(200, resp.length);
try (var os = t.getResponseBody()) {
os.write(resp);
case "GET" -> {
byte[] resp = responseBody.getBytes(StandardCharsets.UTF_8);
exchg.sendResponseHeaders(200, resp.length);
try (var os = exchg.getResponseBody()) {
os.write(resp);
}
}
default -> {
exchg.sendResponseHeaders(405, 0);
exchg.getResponseBody().close();
}
}
default -> {
t.sendResponseHeaders(405, 0);
}
}
t.getResponseBody().close();
t.close();
}
}

Expand Down

0 comments on commit d401c83

Please sign in to comment.