Search in sources :

Example 36 with BasicHttpRequest

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

the class Http1IntegrationTest method testAbsentHostHeader.

@Test
public void testAbsentHostHeader() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final InetSocketAddress serverEndpoint = server.start();
    client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, RequestConnControl.INSTANCE), Http1Config.DEFAULT);
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final HttpRequest request1 = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/hello"));
    request1.setVersion(HttpVersion.HTTP_1_0);
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, null), 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(200, response1.getCode());
    Assertions.assertEquals("Hi there", result1.getBody());
    final HttpRequest request2 = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/hello"));
    request2.setVersion(HttpVersion.HTTP_1_1);
    final Future<Message<HttpResponse, String>> future2 = streamEndpoint.execute(new BasicRequestProducer(request2, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
    final Message<HttpResponse, String> result2 = future2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    Assertions.assertNotNull(result2);
    final HttpResponse response2 = result2.getHead();
    Assertions.assertNotNull(response2);
    Assertions.assertEquals(400, response2.getCode());
    Assertions.assertEquals("Host header is absent", result2.getBody());
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) Test(org.junit.Test)

Example 37 with BasicHttpRequest

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

the class BasicRequestBuilder method build.

@Override
public BasicHttpRequest build() {
    String path = getPath();
    final List<NameValuePair> parameters = getParameters();
    if (parameters != null && !parameters.isEmpty()) {
        try {
            final URI uri = new URIBuilder(path).setCharset(getCharset()).addParameters(parameters).build();
            path = uri.toASCIIString();
        } catch (final URISyntaxException ex) {
        // should never happen
        }
    }
    final BasicHttpRequest result = new BasicHttpRequest(getMethod(), getScheme(), getAuthority(), path);
    result.setVersion(getVersion());
    result.setHeaders(getHeaders());
    result.setAbsoluteRequestUri(isAbsoluteRequestUri());
    return result;
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) URIBuilder(org.apache.hc.core5.net.URIBuilder)

Example 38 with BasicHttpRequest

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

the class AsyncRequestBuilder method build.

@Override
public AsyncRequestProducer build() {
    String path = getPath();
    if (TextUtils.isEmpty(path)) {
        path = "/";
    }
    AsyncEntityProducer entityProducerCopy = entityProducer;
    final String method = getMethod();
    final List<NameValuePair> parameters = getParameters();
    if (parameters != null && !parameters.isEmpty()) {
        final Charset charset = getCharset();
        if (entityProducerCopy == null && (Method.POST.isSame(method) || Method.PUT.isSame(method))) {
            final String content = WWWFormCodec.format(parameters, charset != null ? charset : ContentType.APPLICATION_FORM_URLENCODED.getCharset());
            entityProducerCopy = new StringAsyncEntityProducer(content, ContentType.APPLICATION_FORM_URLENCODED);
        } else {
            try {
                final URI uri = new URIBuilder(path).setCharset(charset).addParameters(parameters).build();
                path = uri.toASCIIString();
            } catch (final URISyntaxException ex) {
            // should never happen
            }
        }
    }
    if (entityProducerCopy != null && Method.TRACE.isSame(method)) {
        throw new IllegalStateException(Method.TRACE + " requests may not include an entity");
    }
    final BasicHttpRequest request = new BasicHttpRequest(method, getScheme(), getAuthority(), path);
    request.setVersion(getVersion());
    request.setHeaders(getHeaders());
    request.setAbsoluteRequestUri(isAbsoluteRequestUri());
    return new BasicRequestProducer(request, entityProducerCopy);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) Charset(java.nio.charset.Charset) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) URIBuilder(org.apache.hc.core5.net.URIBuilder)

Example 39 with BasicHttpRequest

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

the class TestDefaultConnectionReuseStrategy method testRequestExplicitClose.

@Test
public void testRequestExplicitClose() throws Exception {
    final HttpRequest request = new BasicHttpRequest(Method.GET, "/");
    request.addHeader("Connection", "close");
    final HttpResponse response = new BasicHttpResponse(200, "OK");
    response.addHeader("Transfer-Encoding", "chunked");
    response.addHeader("Connection", "keep-alive");
    Assertions.assertFalse(reuseStrategy.keepAlive(request, response, context));
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Test(org.junit.jupiter.api.Test)

Example 40 with BasicHttpRequest

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

the class TestDefaultConnectionReuseStrategy method testRequestNoExplicitClose.

@Test
public void testRequestNoExplicitClose() throws Exception {
    final HttpRequest request = new BasicHttpRequest(Method.GET, "/");
    request.addHeader("Connection", "blah, blah, blah");
    final HttpResponse response = new BasicHttpResponse(200, "OK");
    response.addHeader("Transfer-Encoding", "chunked");
    response.addHeader("Connection", "keep-alive");
    Assertions.assertTrue(reuseStrategy.keepAlive(request, response, context));
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) 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)47 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