Upgraded HttpCore to version 5.3-beta1
This commit is contained in:
parent
0b4b06d080
commit
0b4e653eda
|
@ -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<Cookie> 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<Cookie> 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -62,7 +62,7 @@
|
|||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<httpcore.version>5.3-alpha2</httpcore.version>
|
||||
<httpcore.version>5.3-beta1</httpcore.version>
|
||||
<log4j.version>2.23.0</log4j.version>
|
||||
<brotli.version>0.1.2</brotli.version>
|
||||
<conscrypt.version>2.5.2</conscrypt.version>
|
||||
|
|
Loading…
Reference in New Issue