Search in sources :

Example 1 with CONTENT_TYPE

use of io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE in project async-http-client by AsyncHttpClient.

the class BasicHttpsTest method multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy.

@Test
public void multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy() throws Throwable {
    logger.debug(">>> multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy");
    KeepAliveStrategy keepAliveStrategy = (ahcRequest, nettyRequest, nettyResponse) -> !ahcRequest.getUri().isSecured();
    withClient(config().setSslEngineFactory(createSslEngineFactory()).setKeepAliveStrategy(keepAliveStrategy)).run(client -> withServer(server).run(server -> {
        server.enqueueEcho();
        server.enqueueEcho();
        server.enqueueEcho();
        String body = "hello there";
        client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute();
        client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute();
        Response response = client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get();
        assertEquals(response.getResponseBody(), body);
    }));
    logger.debug("<<< multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy");
}
Also used : AfterClass(org.testng.annotations.AfterClass) KeepAliveStrategy(org.asynchttpclient.channel.KeepAliveStrategy) Arrays(java.util.Arrays) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) BeforeClass(org.testng.annotations.BeforeClass) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) EventCollectingHandler(org.asynchttpclient.test.EventCollectingHandler) TestUtils(org.asynchttpclient.test.TestUtils) ExecutionException(java.util.concurrent.ExecutionException) Dsl.config(org.asynchttpclient.Dsl.config) CONTENT_TYPE(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE) HttpServer(org.asynchttpclient.testserver.HttpServer) HttpTest(org.asynchttpclient.testserver.HttpTest) SECONDS(java.util.concurrent.TimeUnit.SECONDS) HttpServletResponse(javax.servlet.http.HttpServletResponse) KeepAliveStrategy(org.asynchttpclient.channel.KeepAliveStrategy) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 2 with CONTENT_TYPE

use of io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE in project async-http-client by AsyncHttpClient.

the class BasicHttpsTest method reconnectAfterFailedCertificationPath.

@Test
public void reconnectAfterFailedCertificationPath() throws Throwable {
    logger.debug(">>> reconnectAfterFailedCertificationPath");
    AtomicBoolean trust = new AtomicBoolean();
    withClient(config().setMaxRequestRetry(0).setSslEngineFactory(createSslEngineFactory(trust))).run(client -> withServer(server).run(server -> {
        server.enqueueEcho();
        server.enqueueEcho();
        String body = "hello there";
        // first request fails because server certificate is rejected
        Throwable cause = null;
        try {
            client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(TIMEOUT, SECONDS);
        } catch (final ExecutionException e) {
            cause = e.getCause();
        }
        assertNotNull(cause);
        // second request should succeed
        trust.set(true);
        Response response = client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(TIMEOUT, SECONDS);
        assertEquals(response.getResponseBody(), body);
    }));
    logger.debug("<<< reconnectAfterFailedCertificationPath");
}
Also used : AfterClass(org.testng.annotations.AfterClass) KeepAliveStrategy(org.asynchttpclient.channel.KeepAliveStrategy) Arrays(java.util.Arrays) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) BeforeClass(org.testng.annotations.BeforeClass) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) EventCollectingHandler(org.asynchttpclient.test.EventCollectingHandler) TestUtils(org.asynchttpclient.test.TestUtils) ExecutionException(java.util.concurrent.ExecutionException) Dsl.config(org.asynchttpclient.Dsl.config) CONTENT_TYPE(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE) HttpServer(org.asynchttpclient.testserver.HttpServer) HttpTest(org.asynchttpclient.testserver.HttpTest) SECONDS(java.util.concurrent.TimeUnit.SECONDS) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 3 with CONTENT_TYPE

use of io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE in project cf-java-client by cloudfoundry.

the class MultipartHttpClientRequest method done.

public Mono<Void> done() {
    AsciiString boundary = generateMultipartBoundary();
    AsciiString delimiter = getDelimiter(boundary);
    AsciiString closeDelimiter = getCloseDelimiter(boundary);
    List<PartHttpClientRequest> parts = this.partConsumers.stream().map(partConsumer -> {
        PartHttpClientRequest part = new PartHttpClientRequest(this.objectMapper);
        partConsumer.accept(part);
        return part;
    }).collect(Collectors.toList());
    Long contentLength = parts.stream().mapToLong(part -> delimiter.length() + CRLF.length() + part.getLength()).sum() + closeDelimiter.length();
    NettyOutbound intermediateRequest = this.request.chunkedTransfer(false).header(CONTENT_TYPE, BOUNDARY_PREAMBLE.concat(boundary)).header(CONTENT_LENGTH, String.valueOf(contentLength));
    for (PartHttpClientRequest part : parts) {
        intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(delimiter.toByteArray()));
        intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(CRLF.toByteArray()));
        intermediateRequest = intermediateRequest.sendObject(part.renderedHeaders);
        intermediateRequest = part.sendPayload(intermediateRequest);
    }
    intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(closeDelimiter.toByteArray()));
    return intermediateRequest.then();
}
Also used : CONTENT_LENGTH(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) CONTENT_DISPOSITION(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_DISPOSITION) AsciiString(io.netty.util.AsciiString) NettyOutbound(reactor.ipc.netty.NettyOutbound) Random(java.util.Random) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) Path(java.nio.file.Path) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MULTIPART_FORM_DATA(io.netty.handler.codec.http.HttpHeaderValues.MULTIPART_FORM_DATA) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpClientRequest(reactor.ipc.netty.http.client.HttpClientRequest) CONTENT_TYPE(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE) Exceptions(reactor.core.Exceptions) Comparator(java.util.Comparator) NettyOutbound(reactor.ipc.netty.NettyOutbound) AsciiString(io.netty.util.AsciiString)

Aggregations

CONTENT_TYPE (io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE)3 Arrays (java.util.Arrays)2 ExecutionException (java.util.concurrent.ExecutionException)2 SECONDS (java.util.concurrent.TimeUnit.SECONDS)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Dsl.config (org.asynchttpclient.Dsl.config)2 KeepAliveStrategy (org.asynchttpclient.channel.KeepAliveStrategy)2 EventCollectingHandler (org.asynchttpclient.test.EventCollectingHandler)2 TestUtils (org.asynchttpclient.test.TestUtils)2 HttpServer (org.asynchttpclient.testserver.HttpServer)2 HttpTest (org.asynchttpclient.testserver.HttpTest)2 Assert.assertEquals (org.testng.Assert.assertEquals)2 Assert.assertNotNull (org.testng.Assert.assertNotNull)2 AfterClass (org.testng.annotations.AfterClass)2 BeforeClass (org.testng.annotations.BeforeClass)2 Test (org.testng.annotations.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1