use of org.apache.hc.core5.http.nio.support.classic.AbstractClassicServerExchangeHandler in project httpcomponents-core by apache.
the class Http1IntegrationTest method testSlowResponseProducer.
@Test
public void testSlowResponseProducer() throws Exception {
server.register("*", () -> new AbstractClassicServerExchangeHandler(2048, Executors.newSingleThreadExecutor()) {
@Override
protected void handle(final HttpRequest request, final InputStream requestStream, final HttpResponse response, final OutputStream responseStream, final HttpContext context) throws IOException, HttpException {
if (!"/hello".equals(request.getPath())) {
response.setCode(HttpStatus.SC_NOT_FOUND);
return;
}
if (!Method.POST.name().equalsIgnoreCase(request.getMethod())) {
response.setCode(HttpStatus.SC_NOT_IMPLEMENTED);
return;
}
if (requestStream == null) {
return;
}
final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_TYPE);
final ContentType contentType = h1 != null ? ContentType.parse(h1.getValue()) : null;
final Charset charset = ContentType.getCharset(contentType, StandardCharsets.US_ASCII);
response.setCode(HttpStatus.SC_OK);
response.setHeader(h1);
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(requestStream, charset));
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(responseStream, charset))) {
try {
String l;
int count = 0;
while ((l = reader.readLine()) != null) {
writer.write(l);
writer.write("\r\n");
count++;
if (count % 500 == 0) {
Thread.sleep(500);
}
}
writer.flush();
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new InterruptedIOException(ex.getMessage());
}
}
}
});
final InetSocketAddress serverEndpoint = server.start();
client.start(Http1Config.custom().setBufferSize(256).build());
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello"));
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcd", 2000)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(LONG_TIMEOUT.getDuration(), LONG_TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
final String s1 = result1.getBody();
Assertions.assertNotNull(s1);
final StringTokenizer t1 = new StringTokenizer(s1, "\r\n");
while (t1.hasMoreTokens()) {
Assertions.assertEquals("0123456789abcd", t1.nextToken());
}
}
use of org.apache.hc.core5.http.nio.support.classic.AbstractClassicServerExchangeHandler in project httpcomponents-core by apache.
the class H2IntegrationTest method testSlowResponseProducer.
@Test
public void testSlowResponseProducer() throws Exception {
server.register("*", () -> new AbstractClassicServerExchangeHandler(2048, Executors.newSingleThreadExecutor()) {
@Override
protected void handle(final HttpRequest request, final InputStream requestStream, final HttpResponse response, final OutputStream responseStream, final HttpContext context) throws IOException, HttpException {
if (!"/hello".equals(request.getPath())) {
response.setCode(HttpStatus.SC_NOT_FOUND);
return;
}
if (!Method.POST.name().equalsIgnoreCase(request.getMethod())) {
response.setCode(HttpStatus.SC_NOT_IMPLEMENTED);
return;
}
if (requestStream == null) {
return;
}
final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_TYPE);
final ContentType contentType = h1 != null ? ContentType.parse(h1.getValue()) : null;
final Charset charset = ContentType.getCharset(contentType, StandardCharsets.US_ASCII);
response.setCode(HttpStatus.SC_OK);
response.setHeader(h1);
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(requestStream, charset));
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(responseStream, charset))) {
try {
String l;
int count = 0;
while ((l = reader.readLine()) != null) {
writer.write(l);
writer.write("\r\n");
count++;
if (count % 500 == 0) {
Thread.sleep(500);
}
}
writer.flush();
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new InterruptedIOException(ex.getMessage());
}
}
}
});
final InetSocketAddress serverEndpoint = server.start();
client.start(H2Config.custom().setInitialWindowSize(512).build());
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello"));
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcd", 2000)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(LONG_TIMEOUT.getDuration(), LONG_TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
final String s1 = result1.getBody();
Assertions.assertNotNull(s1);
final StringTokenizer t1 = new StringTokenizer(s1, "\r\n");
while (t1.hasMoreTokens()) {
Assertions.assertEquals("0123456789abcd", t1.nextToken());
}
}
Aggregations