Search in sources :

Example 71 with BasicHttpRequest

use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.

the class TestBasicMessageBuilders method testRequestCopy.

@Test
public void testRequestCopy() throws Exception {
    final HttpRequest request = new BasicHttpRequest(Method.GET, URI.create("https://host:3456/stuff?blah"));
    request.addHeader("h1", "v1");
    request.addHeader("h1", "v2");
    request.addHeader("h2", "v2");
    request.setVersion(HttpVersion.HTTP_2);
    final BasicRequestBuilder builder = BasicRequestBuilder.copy(request);
    Assertions.assertEquals("GET", builder.getMethod());
    Assertions.assertEquals("https", builder.getScheme());
    Assertions.assertEquals(new URIAuthority("host", 3456), builder.getAuthority());
    Assertions.assertEquals("/stuff?blah", builder.getPath());
    Assertions.assertEquals(HttpVersion.HTTP_2, builder.getVersion());
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 72 with BasicHttpRequest

use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.

the class BenchmarkWorker method createRequestProducer.

private AsyncRequestProducer createRequestProducer() {
    String method = config.getMethod();
    if (method == null) {
        method = config.isHeadInsteadOfGet() ? Method.HEAD.name() : Method.GET.name();
    }
    final BasicHttpRequest request = new BasicHttpRequest(method, config.getUri());
    final String[] headers = config.getHeaders();
    if (headers != null) {
        for (final String s : headers) {
            final int pos = s.indexOf(':');
            if (pos != -1) {
                request.addHeader(new BasicHeader(s.substring(0, pos).trim(), s.substring(pos + 1)));
            }
        }
    }
    if (!config.isKeepAlive() && !config.isForceHttp2()) {
        request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE));
    }
    if (config.isUseAcceptGZip()) {
        request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"));
    }
    if (config.getSoapAction() != null && config.getSoapAction().length() > 0) {
        request.addHeader(new BasicHeader("SOAPAction", config.getSoapAction()));
    }
    final AsyncEntityProducer entityProducer;
    if (config.getPayloadFile() != null) {
        entityProducer = new FileEntityProducer(config.getPayloadFile(), config.getContentType(), config.isUseChunking());
    } else if (config.getPayloadText() != null) {
        entityProducer = new BasicAsyncEntityProducer(config.getPayloadText(), config.getContentType(), config.isUseChunking());
    } else {
        entityProducer = null;
    }
    return new AsyncRequestProducer() {

        @Override
        public void sendRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
            channel.sendRequest(request, entityProducer, context);
        }

        @Override
        public boolean isRepeatable() {
            return entityProducer == null || entityProducer.isRepeatable();
        }

        @Override
        public int available() {
            return entityProducer != null ? entityProducer.available() : 0;
        }

        @Override
        public void produce(final DataStreamChannel channel) throws IOException {
            if (entityProducer != null) {
                entityProducer.produce(channel);
            }
        }

        @Override
        public void failed(final Exception cause) {
            if (config.getVerbosity() >= 1) {
                System.out.println("Failed HTTP request: " + cause.getMessage());
            }
        }

        @Override
        public void releaseResources() {
            if (entityProducer != null) {
                entityProducer.releaseResources();
            }
        }
    };
}
Also used : FileEntityProducer(org.apache.hc.core5.http.nio.entity.FileEntityProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel) BasicHeader(org.apache.hc.core5.http.message.BasicHeader)

Example 73 with BasicHttpRequest

use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromMessageConnectMissingAuthority.

@Test
public void testConvertFromMessageConnectMissingAuthority() throws Exception {
    final HttpRequest request = new BasicHttpRequest("CONNECT", null, null);
    request.addHeader("Custom123", "Value");
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    Assertions.assertThrows(HttpException.class, () -> converter.convert(request), "CONNECT request authority is not set");
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Test(org.junit.jupiter.api.Test)

Example 74 with BasicHttpRequest

use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromMessageConnectionHeader.

@Test
public void testConvertFromMessageConnectionHeader() throws Exception {
    final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/");
    request.addHeader("Connection", "Keep-Alive");
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    Assertions.assertThrows(HttpException.class, () -> converter.convert(request), "Header 'Connection: Keep-Alive' is illegal for HTTP/2 messages");
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Test(org.junit.jupiter.api.Test)

Example 75 with BasicHttpRequest

use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromFieldsTEHeader.

@Test
public void testConvertFromFieldsTEHeader() throws Exception {
    final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/");
    request.addHeader("TE", "gzip");
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    Assertions.assertThrows(HttpException.class, () -> converter.convert(request), "Header 'TE: gzip' is illegal for HTTP/2 messages");
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpRequest (org.apache.hc.core5.http.HttpRequest)75 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)68 Test (org.junit.jupiter.api.Test)45 HttpResponse (org.apache.hc.core5.http.HttpResponse)41 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)37 Message (org.apache.hc.core5.http.Message)34 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)33 InetSocketAddress (java.net.InetSocketAddress)30 HttpHost (org.apache.hc.core5.http.HttpHost)30 Test (org.junit.Test)30 URI (java.net.URI)23 HttpException (org.apache.hc.core5.http.HttpException)21 IOException (java.io.IOException)20 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)19 URIAuthority (org.apache.hc.core5.net.URIAuthority)17 InterruptedIOException (java.io.InterruptedIOException)16 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)15 Header (org.apache.hc.core5.http.Header)14 URISyntaxException (java.net.URISyntaxException)8 ContentType (org.apache.hc.core5.http.ContentType)8