use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromFieldsTETrailerHeader.
@Test
public void testConvertFromFieldsTETrailerHeader() throws Exception {
final List<Header> headers = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("te", "trailers"));
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
final HttpRequest request = converter.convert(headers);
Assertions.assertNotNull(request);
Assertions.assertEquals("GET", request.getMethod());
Assertions.assertEquals("http", request.getScheme());
Assertions.assertEquals(new URIAuthority("www.example.com"), request.getAuthority());
Assertions.assertEquals("/", request.getPath());
final Header[] allHeaders = request.getHeaders();
Assertions.assertEquals(1, allHeaders.length);
Assertions.assertEquals("te", allHeaders[0].getName());
Assertions.assertEquals("trailers", allHeaders[0].getValue());
}
use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromFieldsBasic.
@Test
public void testConvertFromFieldsBasic() throws Exception {
final List<Header> headers = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("custom123", "value"));
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
final HttpRequest request = converter.convert(headers);
Assertions.assertNotNull(request);
Assertions.assertEquals("GET", request.getMethod());
Assertions.assertEquals("http", request.getScheme());
Assertions.assertEquals(new URIAuthority("www.example.com"), request.getAuthority());
Assertions.assertEquals("/", request.getPath());
final Header[] allHeaders = request.getHeaders();
Assertions.assertEquals(1, allHeaders.length);
Assertions.assertEquals("custom123", allHeaders[0].getName());
Assertions.assertEquals("value", allHeaders[0].getValue());
}
use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.
the class ClassicTestClient method execute.
public ClassicHttpResponse execute(final HttpHost targetHost, final ClassicHttpRequest request, final HttpContext context) throws HttpException, IOException {
final HttpRequester requester = this.requesterRef.get();
if (requester == null) {
throw new IllegalStateException("Requester has not been started");
}
if (request.getAuthority() == null) {
request.setAuthority(new URIAuthority(targetHost));
}
request.setScheme(targetHost.getSchemeName());
return requester.execute(targetHost, request, socketConfig.getSoTimeout(), context);
}
use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.
the class H2MultiplexingRequester method execute.
private void execute(final AsyncClientExchangeHandler exchangeHandler, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final CancellableDependency cancellableDependency, final Timeout timeout, final HttpContext context) {
Args.notNull(exchangeHandler, "Exchange handler");
Args.notNull(timeout, "Timeout");
Args.notNull(context, "Context");
try {
exchangeHandler.produceRequest((request, entityDetails, httpContext) -> {
final String scheme = request.getScheme();
final URIAuthority authority = request.getAuthority();
if (authority == null) {
throw new ProtocolException("Request authority not specified");
}
final HttpHost target = new HttpHost(scheme, authority);
connPool.getSession(target, timeout, new FutureCallback<IOSession>() {
@Override
public void completed(final IOSession ioSession) {
ioSession.enqueue(new RequestExecutionCommand(new AsyncClientExchangeHandler() {
@Override
public void releaseResources() {
exchangeHandler.releaseResources();
}
@Override
public void produceRequest(final RequestChannel channel, final HttpContext httpContext) throws HttpException, IOException {
channel.sendRequest(request, entityDetails, httpContext);
}
@Override
public int available() {
return exchangeHandler.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
exchangeHandler.produce(channel);
}
@Override
public void consumeInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
exchangeHandler.consumeInformation(response, httpContext);
}
@Override
public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
exchangeHandler.consumeResponse(response, entityDetails, httpContext);
}
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
exchangeHandler.updateCapacity(capacityChannel);
}
@Override
public void consume(final ByteBuffer src) throws IOException {
exchangeHandler.consume(src);
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
exchangeHandler.streamEnd(trailers);
}
@Override
public void cancel() {
exchangeHandler.cancel();
}
@Override
public void failed(final Exception cause) {
exchangeHandler.failed(cause);
}
}, pushHandlerFactory, cancellableDependency, context), Command.Priority.NORMAL);
if (!ioSession.isOpen()) {
exchangeHandler.failed(new ConnectionClosedException());
}
}
@Override
public void failed(final Exception ex) {
exchangeHandler.failed(ex);
}
@Override
public void cancelled() {
exchangeHandler.cancel();
}
});
}, context);
} catch (final IOException | HttpException ex) {
exchangeHandler.failed(ex);
}
}
use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.
the class ClassicRequestBuilderTest method builder.
@Test
public void builder() {
final Header header = new BasicHeader("header2", "blah");
final ClassicHttpRequest classicHttpRequest = ClassicRequestBuilder.get().setVersion(HttpVersion.HTTP_1_1).setCharset(StandardCharsets.US_ASCII).setAuthority(new URIAuthority("host")).setEntity("<html><body><h1>Access denied</h1></body></html>", ContentType.TEXT_HTML).setHeader(new BasicHeader("header2", "blah")).setHeader("X-Test-Filter", "active").setHeader(header).setPath("path/").setScheme("http").addHeader(header).addHeader("header", ".addHeader(header)").addParameter(new BasicHeader("header2", "blah")).addParameter("param1", "value1").addParameters(new BasicNameValuePair("param3", "value3"), new BasicNameValuePair("param4", null)).setAbsoluteRequestUri(true).setEntity(new StringEntity("requestBody")).setEntity(new ByteArrayEntity(new byte[10240], ContentType.TEXT_PLAIN)).setEntity(new byte[10240], ContentType.TEXT_HTML).setEntity("requestBody").setUri("theUri").setHttpHost(new HttpHost("httpbin.org")).build();
assertAll("Should return address of Oracle's headquarter", () -> assertNotNull(classicHttpRequest.getEntity()), () -> assertEquals(Method.GET.name(), classicHttpRequest.getMethod()), () -> assertEquals("http", classicHttpRequest.getScheme()), () -> assertEquals("httpbin.org", classicHttpRequest.getAuthority().getHostName()), () -> assertEquals(HttpVersion.HTTP_1_1, classicHttpRequest.getVersion()), () -> assertEquals(4, classicHttpRequest.getHeaders().length), () -> assertNotNull(ClassicRequestBuilder.get().toString()), () -> assertEquals("http://httpbin.org/theUri?header2=blah¶m1=value1¶m3=value3¶m4", new String(classicHttpRequest.getRequestUri().getBytes())));
}
Aggregations