use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicAuthenticationTest method testGetRequestAuthentication.
@Test
public void testGetRequestAuthentication() throws Exception {
server.start();
final HttpHost target = new HttpHost("localhost", server.getLocalPort());
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.GET, "/stuff");
try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
final String body1 = EntityUtils.toString(response1.getEntity());
assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
}
final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.GET, "/stuff");
request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body1 = EntityUtils.toString(response2.getEntity());
assertThat(body1, CoreMatchers.equalTo(""));
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicAuthenticationTest method testPostRequestAuthenticationNoExpectContinue.
@Test
public void testPostRequestAuthenticationNoExpectContinue() throws Exception {
server.start();
final HttpHost target = new HttpHost("localhost", server.getLocalPort());
final HttpCoreContext context = HttpCoreContext.create();
final Random rnd = new Random();
final byte[] stuff = new byte[10240];
for (int i = 0; i < stuff.length; i++) {
stuff[i] = (byte) ('a' + rnd.nextInt(10));
}
final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/stuff");
request1.setVersion(HttpVersion.HTTP_1_0);
request1.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
final String body1 = EntityUtils.toString(response1.getEntity());
assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
}
final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/stuff");
request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
request2.setVersion(HttpVersion.HTTP_1_0);
request2.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body1 = EntityUtils.toString(response2.getEntity());
assertThat(body1, CoreMatchers.equalTo(new String(stuff, StandardCharsets.US_ASCII)));
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testHttpPostNoEntity.
@Test
public void testHttpPostNoEntity() throws Exception {
this.server.registerHandler("*", (request, response, context) -> {
final HttpEntity entity = request.getEntity();
if (entity != null) {
final byte[] data = EntityUtils.toByteArray(entity);
response.setEntity(new ByteArrayEntity(data, null));
}
});
this.server.start();
this.client.start();
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
post.setEntity(null);
try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
final byte[] received = EntityUtils.toByteArray(response.getEntity());
Assertions.assertEquals(0, received.length);
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testHeaderTooLarge.
@Test
public void testHeaderTooLarge() throws Exception {
this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("All is well", StandardCharsets.US_ASCII)));
this.server.start(Http1Config.custom().setMaxLineLength(100).build(), null, null);
this.client.start();
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest(Method.GET, "/");
get1.setHeader("big-f-header", "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890");
try (final ClassicHttpResponse response1 = this.client.execute(host, get1, context)) {
Assertions.assertEquals(431, response1.getCode());
EntityUtils.consume(response1.getEntity());
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testHeaderTooLargePost.
@Test
public void testHeaderTooLargePost() throws Exception {
this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("All is well", StandardCharsets.US_ASCII)));
this.server.start(Http1Config.custom().setMaxLineLength(100).build(), null, null);
this.client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, RequestTargetHost.INSTANCE, RequestConnControl.INSTANCE));
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
final ClassicHttpRequest post1 = new BasicClassicHttpRequest(Method.POST, "/");
post1.setHeader("big-f-header", "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890");
final byte[] b = new byte[2048];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) ('a' + i % 10);
}
post1.setEntity(new ByteArrayEntity(b, ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response1 = this.client.execute(host, post1, context)) {
Assertions.assertEquals(431, response1.getCode());
EntityUtils.consume(response1.getEntity());
}
}
Aggregations