From 0b4e653edadc38b424de62d79623f34194cf1c64 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 21 Jun 2024 18:53:40 +0200 Subject: [PATCH] Upgraded HttpCore to version 5.3-beta1 --- .../testing/sync/TestCookieVirtualHost.java | 94 +++++++++++-------- .../sync/TestDefaultClientTlsStrategy.java | 10 ++ .../TestFutureRequestExecutionService.java | 1 + .../sync/TestMalformedServerResponse.java | 48 ++++++---- pom.xml | 2 +- 5 files changed, 95 insertions(+), 60 deletions(-) diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestCookieVirtualHost.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestCookieVirtualHost.java index 965e90091..6eea853d3 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestCookieVirtualHost.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestCookieVirtualHost.java @@ -33,29 +33,37 @@ import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.cookie.BasicCookieStore; import org.apache.hc.client5.http.cookie.Cookie; import org.apache.hc.client5.http.cookie.CookieStore; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.protocol.HttpClientContext; -import org.apache.hc.client5.testing.extension.sync.ClientProtocolLevel; -import org.apache.hc.client5.testing.extension.sync.TestClient; import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpStatus; -import org.apache.hc.core5.http.URIScheme; +import org.apache.hc.core5.http.impl.bootstrap.HttpServer; +import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.message.BasicHeader; +import org.apache.hc.core5.io.CloseMode; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** * This class tests cookie matching when using Virtual Host. */ -public class TestCookieVirtualHost extends AbstractIntegrationTestBase { +public class TestCookieVirtualHost { - public TestCookieVirtualHost() { - super(URIScheme.HTTP, ClientProtocolLevel.STANDARD); + private HttpServer server; + + @AfterEach + public void shutDown() throws Exception { + if (this.server != null) { + this.server.close(CloseMode.GRACEFUL); + } } @Test public void testCookieMatchingWithVirtualHosts() throws Exception { - configureServer(bootstrap -> bootstrap + server = ServerBootstrap.bootstrap() .register("app.mydomain.fr", "*", (request, response, context) -> { final int n = Integer.parseInt(request.getFirstHeader("X-Request").getValue()); @@ -93,45 +101,49 @@ public class TestCookieVirtualHost extends AbstractIntegrationTestBase { Assertions.fail("Unexpected value: " + n); break; } - })); + }) + .create(); + server.start(); - final HttpHost target = startServer(); + final HttpHost target = new HttpHost("localhost", server.getLocalPort()); + try (final CloseableHttpClient client = HttpClientBuilder.create().build()) { + final CookieStore cookieStore = new BasicCookieStore(); + final HttpClientContext context = HttpClientContext.create(); + context.setCookieStore(cookieStore); - final TestClient client = client(); + // First request : retrieve a domain cookie from remote server. + final HttpGet request1 = new HttpGet(new URI("http://app.mydomain.fr")); + request1.addHeader("X-Request", "1"); + client.execute(target, request1, context, response -> { + Assertions.assertEquals(200, response.getCode()); + EntityUtils.consume(response.getEntity()); + return null; + }); - final CookieStore cookieStore = new BasicCookieStore(); - final HttpClientContext context = HttpClientContext.create(); - context.setCookieStore(cookieStore); + // We should have one cookie set on domain. + final List cookies = cookieStore.getCookies(); + Assertions.assertNotNull(cookies); + Assertions.assertEquals(1, cookies.size()); + Assertions.assertEquals("name1", cookies.get(0).getName()); - // First request : retrieve a domain cookie from remote server. - final HttpGet request1 = new HttpGet(new URI("http://app.mydomain.fr")); - request1.addHeader("X-Request", "1"); - client.execute(target, request1, context, response -> { - EntityUtils.consume(response.getEntity()); - return null; - }); + // Second request : send the cookie back. + final HttpGet request2 = new HttpGet(new URI("http://app.mydomain.fr")); + request2.addHeader("X-Request", "2"); + client.execute(target, request2, context, response -> { + Assertions.assertEquals(200, response.getCode()); + EntityUtils.consume(response.getEntity()); + return null; + }); - // We should have one cookie set on domain. - final List cookies = cookieStore.getCookies(); - Assertions.assertNotNull(cookies); - Assertions.assertEquals(1, cookies.size()); - Assertions.assertEquals("name1", cookies.get(0).getName()); - - // Second request : send the cookie back. - final HttpGet request2 = new HttpGet(new URI("http://app.mydomain.fr")); - request2.addHeader("X-Request", "2"); - client.execute(target, request2, context, response -> { - EntityUtils.consume(response.getEntity()); - return null; - }); - - // Third request : Host header - final HttpGet request3 = new HttpGet(new URI("http://app.mydomain.fr")); - request3.addHeader("X-Request", "3"); - client.execute(target, request3, context, response -> { - EntityUtils.consume(response.getEntity()); - return null; - }); + // Third request : Host header + final HttpGet request3 = new HttpGet(new URI("http://app.mydomain.fr")); + request3.addHeader("X-Request", "3"); + client.execute(target, request3, context, response -> { + Assertions.assertEquals(200, response.getCode()); + EntityUtils.consume(response.getEntity()); + return null; + }); + } } } diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestDefaultClientTlsStrategy.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestDefaultClientTlsStrategy.java index 31090af44..0242a9cc3 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestDefaultClientTlsStrategy.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestDefaultClientTlsStrategy.java @@ -100,6 +100,7 @@ public class TestDefaultClientTlsStrategy { // @formatter:off this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -129,6 +130,7 @@ public class TestDefaultClientTlsStrategy { // @formatter:off this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -155,6 +157,7 @@ public class TestDefaultClientTlsStrategy { // @formatter:off this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -185,6 +188,7 @@ public class TestDefaultClientTlsStrategy { this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) .setSslSetupHandler(sslParameters -> sslParameters.setNeedClientAuth(true)) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -217,6 +221,7 @@ public class TestDefaultClientTlsStrategy { // @formatter:off this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -250,6 +255,7 @@ public class TestDefaultClientTlsStrategy { // @formatter:off this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -283,6 +289,7 @@ public class TestDefaultClientTlsStrategy { this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) .setSslSetupHandler(sslParameters -> sslParameters.setProtocols(new String[] {"SSLv3"})) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -335,6 +342,7 @@ public class TestDefaultClientTlsStrategy { this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) .setSslSetupHandler(sslParameters -> sslParameters.setProtocols(new String[] {cipherSuite})) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -358,6 +366,7 @@ public class TestDefaultClientTlsStrategy { // @formatter:off this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); @@ -419,6 +428,7 @@ public class TestDefaultClientTlsStrategy { // @formatter:off this.server = ServerBootstrap.bootstrap() .setSslContext(SSLTestContexts.createServerSSLContext()) + .setRequestRouter((r, c) -> null) .create(); // @formatter:on this.server.start(); diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java index f7fd40617..b4008b20b 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java @@ -72,6 +72,7 @@ public class TestFutureRequestExecutionService { @BeforeEach public void before() throws Exception { this.localServer = ServerBootstrap.bootstrap() + .setCanonicalHostName("localhost") .register("/wait", (request, response, context) -> { try { while(blocked.get()) { diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestMalformedServerResponse.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestMalformedServerResponse.java index 75fd4c027..d3e118e67 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestMalformedServerResponse.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestMalformedServerResponse.java @@ -43,11 +43,22 @@ import org.apache.hc.core5.http.impl.io.DefaultBHttpServerConnection; import org.apache.hc.core5.http.io.HttpConnectionFactory; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.io.CloseMode; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class TestMalformedServerResponse { + private HttpServer server; + + @AfterEach + public void shutDown() throws Exception { + if (this.server != null) { + this.server.close(CloseMode.GRACEFUL); + } + } + static class BrokenServerConnection extends DefaultBHttpServerConnection { public BrokenServerConnection(final String scheme, final Http1Config h1Config) { @@ -85,30 +96,31 @@ public class TestMalformedServerResponse { @Test public void testNoContentResponseWithGarbage() throws Exception { - try (final HttpServer server = ServerBootstrap.bootstrap() + server = ServerBootstrap.bootstrap() + .setCanonicalHostName("localhost") .setConnectionFactory(new BrokenServerConnectionFactory()) .register("/nostuff", (request, response, context) -> response.setCode(HttpStatus.SC_NO_CONTENT)) .register("/stuff", (request, response, context) -> { response.setCode(HttpStatus.SC_OK); response.setEntity(new StringEntity("Some important stuff")); }) - .create()) { - server.start(); - final HttpHost target = new HttpHost("localhost", server.getLocalPort()); - try (final CloseableHttpClient httpclient = HttpClientBuilder.create().build()) { - final HttpGet get1 = new HttpGet("/nostuff"); - httpclient.execute(target, get1, response -> { - Assertions.assertEquals(HttpStatus.SC_NO_CONTENT, response.getCode()); - EntityUtils.consume(response.getEntity()); - return null; - }); - final HttpGet get2 = new HttpGet("/stuff"); - httpclient.execute(target, get2, response -> { - Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); - EntityUtils.consume(response.getEntity()); - return null; - }); - } + .create(); + server.start(); + + final HttpHost target = new HttpHost("localhost", server.getLocalPort()); + try (final CloseableHttpClient httpclient = HttpClientBuilder.create().build()) { + final HttpGet get1 = new HttpGet("/nostuff"); + httpclient.execute(target, get1, response -> { + Assertions.assertEquals(HttpStatus.SC_NO_CONTENT, response.getCode()); + EntityUtils.consume(response.getEntity()); + return null; + }); + final HttpGet get2 = new HttpGet("/stuff"); + httpclient.execute(target, get2, response -> { + Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); + EntityUtils.consume(response.getEntity()); + return null; + }); } } diff --git a/pom.xml b/pom.xml index b710bc42f..9ff2082c8 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ 1.8 1.8 - 5.3-alpha2 + 5.3-beta1 2.23.0 0.1.2 2.5.2