Search in sources :

Example 41 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class HttpTest method testRequestBodyWrite.

private void testRequestBodyWrite(boolean chunked) {
    Buffer body = Buffer.buffer();
    server.requestHandler(req -> {
        req.bodyHandler(buffer -> {
            assertEquals(body, buffer);
            req.response().end();
        });
    });
    server.listen(onSuccess(server -> {
        HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete());
        int numWrites = 10;
        int chunkSize = 100;
        if (chunked) {
            req.setChunked(true);
        } else {
            req.headers().set("Content-Length", String.valueOf(numWrites * chunkSize));
        }
        for (int i = 0; i < numWrites; i++) {
            Buffer b = TestUtils.randomBuffer(chunkSize);
            body.appendBuffer(b);
            req.write(b);
        }
        req.end();
    }));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) HttpClientRequest(io.vertx.core.http.HttpClientRequest)

Example 42 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class HttpTest method testServerConnectionClose.

@Test
public void testServerConnectionClose() throws Exception {
    // Test server connection close + client close handler
    server.requestHandler(req -> {
        req.connection().close();
    });
    CountDownLatch listenLatch = new CountDownLatch(1);
    server.listen(onSuccess(s -> listenLatch.countDown()));
    awaitLatch(listenLatch);
    HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
        fail();
    });
    req.connectionHandler(conn -> {
        conn.closeHandler(v -> {
            testComplete();
        });
    });
    req.sendHead();
    await();
}
Also used : VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) HttpClientRequest(io.vertx.core.http.HttpClientRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 43 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class HttpTest method testFollowRedirectWithRequestNotEnded.

private void testFollowRedirectWithRequestNotEnded(boolean expectFail) throws Exception {
    Buffer buff1 = Buffer.buffer(TestUtils.randomAlphaString(2048));
    Buffer buff2 = Buffer.buffer(TestUtils.randomAlphaString(2048));
    Buffer expected = Buffer.buffer().appendBuffer(buff1).appendBuffer(buff2);
    AtomicBoolean redirected = new AtomicBoolean();
    CountDownLatch latch = new CountDownLatch(1);
    server.requestHandler(req -> {
        boolean redirect = redirected.compareAndSet(false, true);
        if (redirect) {
            Buffer body = Buffer.buffer();
            req.handler(buff -> {
                if (body.length() == 0) {
                    HttpServerResponse resp = req.response();
                    resp.setStatusCode(307).putHeader(HttpHeaders.LOCATION, "http://localhost:8080/whatever");
                    if (expectFail) {
                        resp.setChunked(true).write("whatever");
                        vertx.runOnContext(v -> {
                            resp.close();
                        });
                    } else {
                        resp.end();
                    }
                    latch.countDown();
                }
                body.appendBuffer(buff);
            });
            req.endHandler(v -> {
                assertEquals(expected, body);
            });
        } else {
            req.response().end();
        }
    });
    startServer();
    AtomicBoolean called = new AtomicBoolean();
    HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
        assertEquals(200, resp.statusCode());
        testComplete();
    }).setFollowRedirects(true).exceptionHandler(err -> {
        if (expectFail) {
            if (called.compareAndSet(false, true)) {
                testComplete();
            }
        } else {
            fail(err);
        }
    }).setChunked(true).write(buff1);
    awaitLatch(latch);
    // Wait so we end the request while having received the server response (but we can't be notified)
    if (!expectFail) {
        Thread.sleep(500);
        req.end(buff2);
    }
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 44 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class HttpTest method testFoo.

private void testFoo(String location, String expected) throws Exception {
    int status = 301;
    Map<String, String> headers = Collections.singletonMap("Location", location);
    HttpMethod method = HttpMethod.GET;
    String baseURI = "https://localhost:8080";
    class MockReq implements HttpClientRequest {

        public HttpClientRequest exceptionHandler(Handler<Throwable> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest write(Buffer data) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setWriteQueueMaxSize(int maxSize) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest drainHandler(Handler<Void> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest handler(Handler<HttpClientResponse> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest pause() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest resume() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest endHandler(Handler<Void> endHandler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setFollowRedirects(boolean followRedirects) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setChunked(boolean chunked) {
            throw new UnsupportedOperationException();
        }

        public boolean isChunked() {
            return false;
        }

        public HttpMethod method() {
            return method;
        }

        public String getRawMethod() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setRawMethod(String method) {
            throw new UnsupportedOperationException();
        }

        public String absoluteURI() {
            return baseURI;
        }

        public String uri() {
            throw new UnsupportedOperationException();
        }

        public String path() {
            throw new UnsupportedOperationException();
        }

        public String query() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setHost(String host) {
            throw new UnsupportedOperationException();
        }

        public String getHost() {
            throw new UnsupportedOperationException();
        }

        public MultiMap headers() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest putHeader(String name, String value) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest putHeader(CharSequence name, CharSequence value) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest putHeader(String name, Iterable<String> values) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest putHeader(CharSequence name, Iterable<CharSequence> values) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest write(String chunk) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest write(String chunk, String enc) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest continueHandler(@Nullable Handler<Void> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest sendHead() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest sendHead(Handler<HttpVersion> completionHandler) {
            throw new UnsupportedOperationException();
        }

        public void end(String chunk) {
            throw new UnsupportedOperationException();
        }

        public void end(String chunk, String enc) {
            throw new UnsupportedOperationException();
        }

        public void end(Buffer chunk) {
            throw new UnsupportedOperationException();
        }

        public void end() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest setTimeout(long timeoutMs) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest pushHandler(Handler<HttpClientRequest> handler) {
            throw new UnsupportedOperationException();
        }

        public boolean reset(long code) {
            return false;
        }

        public HttpConnection connection() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest connectionHandler(@Nullable Handler<HttpConnection> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest writeCustomFrame(int type, int flags, Buffer payload) {
            throw new UnsupportedOperationException();
        }

        public boolean writeQueueFull() {
            throw new UnsupportedOperationException();
        }
    }
    HttpClientRequest req = new MockReq();
    class MockResp implements HttpClientResponse {

        public HttpClientResponse resume() {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse exceptionHandler(Handler<Throwable> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse handler(Handler<Buffer> handler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse pause() {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse endHandler(Handler<Void> endHandler) {
            throw new UnsupportedOperationException();
        }

        public HttpVersion version() {
            throw new UnsupportedOperationException();
        }

        public int statusCode() {
            return status;
        }

        public String statusMessage() {
            throw new UnsupportedOperationException();
        }

        public MultiMap headers() {
            throw new UnsupportedOperationException();
        }

        public String getHeader(String headerName) {
            return headers.get(headerName);
        }

        public String getHeader(CharSequence headerName) {
            return getHeader(headerName.toString());
        }

        public String getTrailer(String trailerName) {
            throw new UnsupportedOperationException();
        }

        public MultiMap trailers() {
            throw new UnsupportedOperationException();
        }

        public List<String> cookies() {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse bodyHandler(Handler<Buffer> bodyHandler) {
            throw new UnsupportedOperationException();
        }

        public HttpClientResponse customFrameHandler(Handler<HttpFrame> handler) {
            throw new UnsupportedOperationException();
        }

        public NetSocket netSocket() {
            throw new UnsupportedOperationException();
        }

        public HttpClientRequest request() {
            return req;
        }
    }
    MockResp resp = new MockResp();
    Function<HttpClientResponse, Future<HttpClientRequest>> handler = client.redirectHandler();
    Future<HttpClientRequest> redirection = handler.apply(resp);
    if (expected != null) {
        assertEquals(location, redirection.result().absoluteURI());
    } else {
        assertTrue(redirection == null || redirection.failed());
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Handler(io.vertx.core.Handler) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Future(io.vertx.core.Future) CompletableFuture(java.util.concurrent.CompletableFuture) HttpMethod(io.vertx.core.http.HttpMethod) Nullable(io.vertx.codegen.annotations.Nullable)

Example 45 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class HttpTest method testRequestHeaders.

private void testRequestHeaders(boolean individually) {
    MultiMap headers = getHeaders(10);
    server.requestHandler(req -> {
        assertTrue(headers.size() < req.headers().size());
        for (Map.Entry<String, String> entry : headers) {
            assertEquals(entry.getValue(), req.headers().get(entry.getKey()));
            assertEquals(entry.getValue(), req.getHeader(entry.getKey()));
        }
        req.response().end();
    });
    server.listen(onSuccess(server -> {
        HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete());
        if (individually) {
            for (Map.Entry<String, String> header : headers) {
                req.headers().add(header.getKey(), header.getValue());
            }
        } else {
            req.headers().setAll(headers);
        }
        req.end();
    }));
    await();
}
Also used : VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) MultiMap(io.vertx.core.MultiMap) HttpClientRequest(io.vertx.core.http.HttpClientRequest) MultiMap(io.vertx.core.MultiMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HttpClientRequest (io.vertx.core.http.HttpClientRequest)75 Test (org.junit.Test)59 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)42 Buffer (io.vertx.core.buffer.Buffer)41 HttpServerResponse (io.vertx.core.http.HttpServerResponse)36 HttpClientOptions (io.vertx.core.http.HttpClientOptions)34 HttpMethod (io.vertx.core.http.HttpMethod)32 CountDownLatch (java.util.concurrent.CountDownLatch)32 HttpServerOptions (io.vertx.core.http.HttpServerOptions)31 CompletableFuture (java.util.concurrent.CompletableFuture)31 AtomicReference (java.util.concurrent.atomic.AtomicReference)31 HttpVersion (io.vertx.core.http.HttpVersion)29 HttpConnection (io.vertx.core.http.HttpConnection)28 TestUtils.assertIllegalStateException (io.vertx.test.core.TestUtils.assertIllegalStateException)28 TimeUnit (java.util.concurrent.TimeUnit)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)28 HttpClient (io.vertx.core.http.HttpClient)27 NetSocket (io.vertx.core.net.NetSocket)27 ArrayList (java.util.ArrayList)26 Collections (java.util.Collections)26