Skip to content

Commit

Permalink
Test that HTTP/1.1 still works when HTTP/2 is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Apr 13, 2021
1 parent 74b91b7 commit 252d4e9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
import org.eclipse.jetty.server.AbstractConnector;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Handler;
Expand Down Expand Up @@ -199,10 +199,10 @@ private AbstractConnector createConnector(InetSocketAddress address, Server serv
HttpConfiguration httpConfiguration = new HttpConfiguration();
httpConfiguration.setSendServerVersion(false);
List<ConnectionFactory> connectionFactories = new ArrayList<>();
connectionFactories.add(new HttpConnectionFactory(httpConfiguration));
if (getHttp2() != null && getHttp2().isEnabled()) {
connectionFactories.add(new HTTP2ServerConnectionFactory(httpConfiguration));
connectionFactories.add(new HTTP2CServerConnectionFactory(httpConfiguration));
}
connectionFactories.add(new HttpConnectionFactory(httpConfiguration));
JettyResourceFactory resourceFactory = getResourceFactory();
ServerConnector connector;
if (resourceFactory != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.Set;

import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
import org.eclipse.jetty.server.AbstractConnector;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Connector;
Expand Down Expand Up @@ -180,10 +180,10 @@ private AbstractConnector createConnector(InetSocketAddress address, Server serv
HttpConfiguration httpConfiguration = new HttpConfiguration();
httpConfiguration.setSendServerVersion(false);
List<ConnectionFactory> connectionFactories = new ArrayList<>();
connectionFactories.add(new HttpConnectionFactory(httpConfiguration));
if (getHttp2() != null && getHttp2().isEnabled()) {
connectionFactories.add(new HTTP2ServerConnectionFactory(httpConfiguration));
connectionFactories.add(new HTTP2CServerConnectionFactory(httpConfiguration));
}
connectionFactories.add(new HttpConnectionFactory(httpConfiguration));
ServerConnector connector = new ServerConnector(server, this.acceptors, this.selectors,
connectionFactories.toArray(new ConnectionFactory[0]));
connector.setHost(address.getHostString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,21 @@ public void cancelled() {
}
}

@Test
protected void whenHttp2IsEnabledAndSslIsDisabledThenHttp11CanStillBeUsed()
throws InterruptedException, ExecutionException, IOException {
AbstractReactiveWebServerFactory factory = getFactory();
Http2 http2 = new Http2();
http2.setEnabled(true);
factory.setHttp2(http2);
this.webServer = factory.getWebServer(new EchoHandler());
this.webServer.start();
Mono<String> result = getWebClient(this.webServer.getPort()).build().post().uri("/test")
.contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromValue("Hello World")).retrieve()
.bodyToMono(String.class);
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
}

protected WebClient prepareCompressionTest() {
Compression compression = new Compression();
compression.setEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,18 @@ public void cancelled() {
}
}

@Test
protected void whenHttp2IsEnabledAndSslIsDisabledThenHttp11CanStillBeUsed()
throws InterruptedException, ExecutionException, IOException, URISyntaxException {
AbstractServletWebServerFactory factory = getFactory();
Http2 http2 = new Http2();
http2.setEnabled(true);
factory.setHttp2(http2);
this.webServer = factory.getWebServer(exampleServletRegistration());
this.webServer.start();
assertThat(getResponse("http://localhost:" + this.webServer.getPort() + "/hello")).isEqualTo("Hello World");
}

protected Future<Object> initiateGetRequest(int port, String path) {
return initiateGetRequest(HttpClients.createMinimal(), port, path);
}
Expand Down

0 comments on commit 252d4e9

Please sign in to comment.