Search in sources :

Example 31 with BasicHttpRequest

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

the class TestBasicMessages method testRequestWithNoPath.

@Test
public void testRequestWithNoPath() throws Exception {
    final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("http://host"));
    Assertions.assertEquals(Method.GET.name(), request.getMethod());
    Assertions.assertEquals("/", request.getPath());
    Assertions.assertEquals(new URIAuthority("host"), request.getAuthority());
    Assertions.assertEquals("http", request.getScheme());
    Assertions.assertEquals(new URI("http://host/"), request.getUri());
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 32 with BasicHttpRequest

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

the class TestBasicMessageBuilders method testRequestBasics.

@Test
public void testRequestBasics() throws Exception {
    final BasicRequestBuilder builder = BasicRequestBuilder.get();
    Assertions.assertEquals(URI.create("/"), builder.getUri());
    Assertions.assertEquals("GET", builder.getMethod());
    Assertions.assertNull(builder.getScheme());
    Assertions.assertNull(builder.getAuthority());
    Assertions.assertNull(builder.getPath());
    Assertions.assertNull(builder.getHeaders());
    Assertions.assertNull(builder.getVersion());
    Assertions.assertNull(builder.getCharset());
    Assertions.assertNull(builder.getParameters());
    final BasicHttpRequest r1 = builder.build();
    Assertions.assertNotNull(r1);
    Assertions.assertEquals("GET", r1.getMethod());
    Assertions.assertNull(r1.getScheme());
    Assertions.assertNull(r1.getAuthority());
    Assertions.assertNull(r1.getPath());
    Assertions.assertEquals(URI.create("/"), r1.getUri());
    Assertions.assertNull(r1.getVersion());
    builder.setUri(URI.create("http://host:1234/blah?param=value"));
    builder.setVersion(HttpVersion.HTTP_1_1);
    Assertions.assertEquals("http", builder.getScheme());
    Assertions.assertEquals(new URIAuthority("host", 1234), builder.getAuthority());
    Assertions.assertEquals("/blah?param=value", builder.getPath());
    Assertions.assertEquals(URI.create("http://host:1234/blah?param=value"), builder.getUri());
    Assertions.assertEquals(HttpVersion.HTTP_1_1, builder.getVersion());
    final BasicHttpRequest r2 = builder.build();
    Assertions.assertEquals("GET", r2.getMethod());
    Assertions.assertEquals("http", r2.getScheme());
    Assertions.assertEquals(new URIAuthority("host", 1234), r2.getAuthority());
    Assertions.assertEquals("/blah?param=value", r2.getPath());
    Assertions.assertEquals(URI.create("http://host:1234/blah?param=value"), r2.getUri());
    Assertions.assertEquals(HttpVersion.HTTP_1_1, builder.getVersion());
    builder.setCharset(StandardCharsets.US_ASCII);
    builder.addParameter("param1", "value1");
    builder.addParameter("param2", null);
    builder.addParameters(new BasicNameValuePair("param3", "value3"), new BasicNameValuePair("param4", null));
    Assertions.assertEquals(builder.getParameters(), Arrays.asList(new BasicNameValuePair("param1", "value1"), new BasicNameValuePair("param2", null), new BasicNameValuePair("param3", "value3"), new BasicNameValuePair("param4", null)));
    Assertions.assertEquals(URI.create("http://host:1234/blah?param=value"), builder.getUri());
    final BasicHttpRequest r3 = builder.build();
    Assertions.assertEquals("GET", r3.getMethod());
    Assertions.assertEquals("http", r3.getScheme());
    Assertions.assertEquals(new URIAuthority("host", 1234), r3.getAuthority());
    Assertions.assertEquals("/blah?param=value&param1=value1&param2&param3=value3&param4", r3.getPath());
    Assertions.assertEquals(URI.create("http://host:1234/blah?param=value&param1=value1&param2&param3=value3&param4"), r3.getUri());
    builder.addHeader("h1", "v1");
    builder.addHeader("h1", "v2");
    builder.addHeader("h2", "v2");
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2")));
    assertThat(builder.getFirstHeader("h1"), HeaderMatcher.same("h1", "v1"));
    assertThat(builder.getLastHeader("h1"), HeaderMatcher.same("h1", "v2"));
    final BasicHttpRequest r4 = builder.build();
    assertThat(r4.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
    assertThat(r4.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2")));
    assertThat(r4.getFirstHeader("h1"), HeaderMatcher.same("h1", "v1"));
    assertThat(r4.getLastHeader("h1"), HeaderMatcher.same("h1", "v2"));
    builder.removeHeader(new BasicHeader("h1", "v2"));
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1")));
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h2", "v2")));
    final BasicHttpRequest r5 = builder.build();
    assertThat(r5.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1")));
    assertThat(r5.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h2", "v2")));
    builder.removeHeaders("h1");
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same());
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h2", "v2")));
    final BasicHttpRequest r6 = builder.build();
    assertThat(r6.getHeaders("h1"), HeadersMatcher.same());
    assertThat(r6.getHeaders(), HeadersMatcher.same(new BasicHeader("h2", "v2")));
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 33 with BasicHttpRequest

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

the class Http1AuthenticationTest method testGetRequestAuthentication.

@Test
public void testGetRequestAuthentication() throws Exception {
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
    final ListenerEndpoint listener = future.get();
    final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
    requester.start();
    final HttpHost target = new HttpHost("localhost", address.getPort());
    final HttpRequest request1 = new BasicHttpRequest(Method.GET, target, "/stuff");
    final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(request1, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
    final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    assertThat(message1, CoreMatchers.notNullValue());
    final HttpResponse response1 = message1.getHead();
    assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
    final String body1 = message1.getBody();
    assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
    final HttpRequest request2 = new BasicHttpRequest(Method.GET, target, "/stuff");
    request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
    final Future<Message<HttpResponse, String>> resultFuture2 = requester.execute(new BasicRequestProducer(request2, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
    final Message<HttpResponse, String> message2 = resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    assertThat(message2, CoreMatchers.notNullValue());
    final HttpResponse response2 = message2.getHead();
    assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
    final String body2 = message2.getBody();
    assertThat(body2, CoreMatchers.equalTo(""));
}
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) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) HttpHost(org.apache.hc.core5.http.HttpHost) Test(org.junit.Test)

Example 34 with BasicHttpRequest

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

the class Http1IntegrationTest method testMessageWithTrailers.

@Test
public void testMessageWithTrailers() throws Exception {
    server.register("/hello", () -> new AbstractServerExchangeHandler<Message<HttpRequest, String>>() {

        @Override
        protected AsyncRequestConsumer<Message<HttpRequest, String>> supplyConsumer(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context) throws HttpException {
            return new BasicRequestConsumer<>(entityDetails != null ? new StringAsyncEntityConsumer() : null);
        }

        @Override
        protected void handle(final Message<HttpRequest, String> requestMessage, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws HttpException, IOException {
            responseTrigger.submitResponse(new BasicResponseProducer(HttpStatus.SC_OK, new DigestingEntityProducer("MD5", new StringAsyncEntityProducer("Hello back with some trailers"))), context);
        }
    });
    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.GET, createRequestURI(serverEndpoint, "/hello"));
    final DigestingEntityConsumer<String> entityConsumer = new DigestingEntityConsumer<>("MD5", new StringAsyncEntityConsumer());
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, null), new BasicResponseConsumer<>(entityConsumer), 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("Hello back with some trailers", result1.getBody());
    final List<Header> trailers = entityConsumer.getTrailers();
    Assertions.assertNotNull(trailers);
    Assertions.assertEquals(2, trailers.size());
    final Map<String, String> map = new HashMap<>();
    for (final Header header : trailers) {
        map.put(TextUtils.toLowerCase(header.getName()), header.getValue());
    }
    final String digest = TextUtils.toHexString(entityConsumer.getDigest());
    Assertions.assertEquals("MD5", map.get("digest-algo"));
    Assertions.assertEquals(digest, map.get("digest"));
}
Also used : Message(org.apache.hc.core5.http.Message) DigestingEntityProducer(org.apache.hc.core5.http.nio.entity.DigestingEntityProducer) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) EntityDetails(org.apache.hc.core5.http.EntityDetails) HttpException(org.apache.hc.core5.http.HttpException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) DigestingEntityConsumer(org.apache.hc.core5.http.nio.entity.DigestingEntityConsumer) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AsyncRequestConsumer(org.apache.hc.core5.http.nio.AsyncRequestConsumer) AsyncServerRequestHandler(org.apache.hc.core5.http.nio.AsyncServerRequestHandler) Header(org.apache.hc.core5.http.Header) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer) Test(org.junit.Test)

Example 35 with BasicHttpRequest

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

the class Http1IntegrationTest method testNoEntityPost.

@Test
public void testNoEntityPost() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi back"));
    final InetSocketAddress serverEndpoint = server.start();
    client.start();
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    for (int i = 0; i < 5; i++) {
        final HttpRequest request = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello"));
        final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(request, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
        final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
        Assertions.assertNotNull(result);
        final HttpResponse response1 = result.getHead();
        final String entity1 = result.getBody();
        Assertions.assertNotNull(response1);
        Assertions.assertEquals(200, response1.getCode());
        Assertions.assertEquals("Hi back", entity1);
    }
}
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) Test(org.junit.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