use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testHttpPostsWithExpectationVerification.
/**
* This test case executes a series of simple POST requests that do not
* meet the target server expectations.
*/
@Test
public void testHttpPostsWithExpectationVerification() throws Exception {
final int reqNo = 20;
// Initialize the server-side request handler
this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("No content")));
this.server.start(null, null, handler -> new BasicHttpServerExpectationDecorator(handler) {
@Override
protected ClassicHttpResponse verify(final ClassicHttpRequest request, final HttpContext context) {
final Header someheader = request.getFirstHeader("Secret");
if (someheader != null) {
final int secretNumber;
try {
secretNumber = Integer.parseInt(someheader.getValue());
} catch (final NumberFormatException ex) {
final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_BAD_REQUEST);
response.setEntity(new StringEntity(ex.toString()));
return response;
}
if (secretNumber >= 2) {
final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
response.setEntity(new StringEntity("Wrong secret number", ContentType.TEXT_PLAIN));
return response;
}
}
return null;
}
});
this.client.start();
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
for (int r = 0; r < reqNo; r++) {
final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
post.addHeader("Secret", Integer.toString(r));
final byte[] b = new byte[2048];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) ('a' + r);
}
post.setEntity(new ByteArrayEntity(b, ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
final HttpEntity responseEntity = response.getEntity();
Assertions.assertNotNull(responseEntity);
EntityUtils.consume(responseEntity);
if (r >= 2) {
Assertions.assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getCode());
} else {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
}
}
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testAbsentHostHeader.
@Test
public void testAbsentHostHeader() throws Exception {
// Initialize the server-side request handler
this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("All is well", StandardCharsets.US_ASCII)));
this.server.start();
this.client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, new RequestConnControl()));
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest(Method.GET, "/");
get1.setVersion(HttpVersion.HTTP_1_0);
try (final ClassicHttpResponse response1 = this.client.execute(host, get1, context)) {
Assertions.assertEquals(200, response1.getCode());
EntityUtils.consume(response1.getEntity());
}
final BasicClassicHttpRequest get2 = new BasicClassicHttpRequest(Method.GET, "/");
try (final ClassicHttpResponse response2 = this.client.execute(host, get2, context)) {
Assertions.assertEquals(400, response2.getCode());
EntityUtils.consume(response2.getEntity());
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testHttpPostNoContentLength.
@Test
public void testHttpPostNoContentLength() 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(new DefaultHttpProcessor(RequestTargetHost.INSTANCE, RequestConnControl.INSTANCE, RequestUserAgent.INSTANCE, new RequestExpectContinue()));
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 testHttpPostIdentity.
@Test
public void testHttpPostIdentity() 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(new DefaultHttpProcessor((request, entity, context) -> request.addHeader(HttpHeaders.TRANSFER_ENCODING, "identity"), RequestTargetHost.INSTANCE, RequestConnControl.INSTANCE, RequestUserAgent.INSTANCE, new RequestExpectContinue()));
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_NOT_IMPLEMENTED, response.getCode());
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testHttpPostsWithExpectContinue.
/**
* This test case executes a series of simple POST requests using
* the 'expect: continue' handshake.
*/
@Test
public void testHttpPostsWithExpectContinue() throws Exception {
final int reqNo = 20;
final Random rnd = new Random();
// Prepare some random data
final List<byte[]> testData = new ArrayList<>(reqNo);
for (int i = 0; i < reqNo; i++) {
final int size = rnd.nextInt(5000);
final byte[] data = new byte[size];
rnd.nextBytes(data);
testData.add(data);
}
// Initialize the server-side request handler
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, true));
}
});
this.server.start();
this.client.start();
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
for (int r = 0; r < reqNo; r++) {
final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
final byte[] data = testData.get(r);
post.setEntity(new ByteArrayEntity(data, null, true));
try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
final byte[] received = EntityUtils.toByteArray(response.getEntity());
final byte[] expected = testData.get(r);
Assertions.assertEquals(expected.length, received.length);
for (int i = 0; i < expected.length; i++) {
Assertions.assertEquals(expected[i], received[i]);
}
}
}
}
Aggregations