use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testSimpleHttpPostsChunked.
/**
* This test case executes a series of simple POST requests with chunk
* coded content content.
*/
@Test
public void testSimpleHttpPostsChunked() 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(20000);
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]);
}
}
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testHttpContent.
@Test
public void testHttpContent() throws Exception {
final String[] patterns = { "0123456789ABCDEF", "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that" };
// Initialize the server-side request handler
this.server.registerHandler("*", (request, response, context) -> {
int n = 1;
String s = request.getPath();
if (s.startsWith("/?n=")) {
s = s.substring(4);
try {
n = Integer.parseInt(s);
if (n <= 0) {
throw new HttpException("Invalid request: " + "number of repetitions cannot be negative or zero");
}
} catch (final NumberFormatException ex) {
throw new HttpException("Invalid request: " + "number of repetitions is invalid");
}
}
final HttpEntity entity = request.getEntity();
if (entity != null) {
final String line = EntityUtils.toString(entity);
final ContentType contentType = ContentType.parse(entity.getContentType());
final Charset charset = ContentType.getCharset(contentType, StandardCharsets.ISO_8859_1);
response.setEntity(new RepeatingEntity(line, charset, n, n % 2 == 0));
}
});
this.server.start();
this.client.start();
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
for (final String pattern : patterns) {
for (int n = 1000; n < 1020; n++) {
final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST.name(), "/?n=" + n);
post.setEntity(new StringEntity(pattern, ContentType.TEXT_PLAIN, n % 2 == 0));
try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
final HttpEntity entity = response.getEntity();
Assertions.assertNotNull(entity);
final InputStream inStream = entity.getContent();
final ContentType contentType = ContentType.parse(entity.getContentType());
final Charset charset = ContentType.getCharset(contentType, StandardCharsets.ISO_8859_1);
Assertions.assertNotNull(inStream);
final BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, charset));
String line;
int count = 0;
while ((line = reader.readLine()) != null) {
Assertions.assertEquals(pattern, line);
count++;
}
Assertions.assertEquals(n, count);
}
}
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testSimpleBasicHttpRequests.
/**
* This test case executes a series of simple GET requests
*/
@Test
public void testSimpleBasicHttpRequests() 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) -> {
String s = request.getPath();
if (s.startsWith("/?")) {
s = s.substring(2);
}
final int index = Integer.parseInt(s);
final byte[] data = testData.get(index);
final ByteArrayEntity entity = new ByteArrayEntity(data, null);
response.setEntity(entity);
});
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 get = new BasicClassicHttpRequest(Method.GET, "/?" + r);
try (final ClassicHttpResponse response = this.client.execute(host, get, 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]);
}
}
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testNoContentResponse.
@Test
public void testNoContentResponse() throws Exception {
final int reqNo = 20;
// Initialize the server-side request handler
this.server.registerHandler("*", (request, response, context) -> response.setCode(HttpStatus.SC_NO_CONTENT));
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 get = new BasicClassicHttpRequest(Method.GET, "/?" + r);
try (final ClassicHttpResponse response = this.client.execute(host, get, context)) {
Assertions.assertNull(response.getEntity());
}
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testSimpleHttpPostsWithContentLength.
/**
* This test case executes a series of simple POST requests with content length
* delimited content.
*/
@Test
public void testSimpleHttpPostsWithContentLength() 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));
}
});
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));
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