use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromFieldsKeepAliveHeader.
@Test
public void testConvertFromFieldsKeepAliveHeader() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/");
request.addHeader("Keep-Alive", "timeout=5, max=1000");
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
Assertions.assertThrows(HttpException.class, () -> converter.convert(request), "Header 'Keep-Alive: timeout=5, max=1000' is illegal for HTTP/2 messages");
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromMessageConnect.
@Test
public void testConvertFromMessageConnect() throws Exception {
final HttpRequest request = new BasicHttpRequest("CONNECT", new HttpHost("host:80"), null);
request.addHeader("custom123", "Value");
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
final List<Header> headers = converter.convert(request);
Assertions.assertNotNull(headers);
Assertions.assertEquals(3, headers.size());
final Header header1 = headers.get(0);
Assertions.assertEquals(":method", header1.getName());
Assertions.assertEquals("CONNECT", header1.getValue());
final Header header2 = headers.get(1);
Assertions.assertEquals(":authority", header2.getName());
Assertions.assertEquals("host:80", header2.getValue());
final Header header3 = headers.get(2);
Assertions.assertEquals("custom123", header3.getName());
Assertions.assertEquals("Value", header3.getValue());
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class H2IntegrationTest method testPrematureResponse.
@Test
public void testPrematureResponse() throws Exception {
server.register("*", new Supplier<AsyncServerExchangeHandler>() {
@Override
public AsyncServerExchangeHandler get() {
return new AsyncServerExchangeHandler() {
private final AtomicReference<AsyncResponseProducer> responseProducer = new AtomicReference<>();
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
capacityChannel.update(Integer.MAX_VALUE);
}
@Override
public void consume(final ByteBuffer src) throws IOException {
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
}
@Override
public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
final AsyncResponseProducer producer;
final Header h = request.getFirstHeader("password");
if (h != null && "secret".equals(h.getValue())) {
producer = new BasicResponseProducer(HttpStatus.SC_OK, "All is well");
} else {
producer = new BasicResponseProducer(HttpStatus.SC_UNAUTHORIZED, "You shall not pass");
}
responseProducer.set(producer);
producer.sendResponse(responseChannel, context);
}
@Override
public int available() {
final AsyncResponseProducer producer = this.responseProducer.get();
return producer.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
final AsyncResponseProducer producer = this.responseProducer.get();
producer.produce(channel);
}
@Override
public void failed(final Exception cause) {
}
@Override
public void releaseResources() {
}
};
}
});
final InetSocketAddress serverEndpoint = server.start();
client.start();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/echo"));
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcdef", 5000)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
Assertions.assertNotNull("You shall not pass", result1.getBody());
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class H2IntegrationTest method testHeaderTooLargePost.
@Test
public void testHeaderTooLargePost() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final InetSocketAddress serverEndpoint = server.start(H2Config.custom().setMaxHeaderListSize(100).build());
client.start(new DefaultHttpProcessor(new H2RequestContent(), new H2RequestTargetHost(), new H2RequestConnControl()), H2Config.DEFAULT);
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"));
request1.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);
}
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, AsyncEntityProducers.create(b, ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(431, response1.getCode());
Assertions.assertEquals("Maximum header list size exceeded", result1.getBody());
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class H2IntegrationTest method testExcessOfConcurrentStreams.
@Test
public void testExcessOfConcurrentStreams() throws Exception {
server.register("/", () -> new MultiLineResponseHandler("0123456789abcdef", 2000));
final InetSocketAddress serverEndpoint = server.start(H2Config.custom().setMaxConcurrentStreams(20).build());
client.start(H2Config.custom().setMaxConcurrentStreams(20).build());
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Queue<Future<Message<HttpResponse, Void>>> queue = new LinkedList<>();
for (int i = 0; i < 2000; i++) {
final HttpRequest request1 = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/"));
final Future<Message<HttpResponse, Void>> future = streamEndpoint.execute(new BasicRequestProducer(request1, null), new BasicResponseConsumer<>(new DiscardingEntityConsumer<>()), null);
queue.add(future);
}
while (!queue.isEmpty()) {
final Future<Message<HttpResponse, Void>> future = queue.remove();
final Message<HttpResponse, Void> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
}
}
Aggregations