Search in sources :

Example 1 with WriteStream

use of io.vertx.core.streams.WriteStream in project vertx-web by vert-x3.

the class WebClientTest method testResponseWriteStreamMissingBody.

@Test
public void testResponseWriteStreamMissingBody() throws Exception {
    AtomicInteger length = new AtomicInteger();
    AtomicBoolean ended = new AtomicBoolean();
    WriteStream<Buffer> stream = new WriteStream<Buffer>() {

        @Override
        public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
            return this;
        }

        @Override
        public WriteStream<Buffer> write(Buffer data) {
            length.addAndGet(data.length());
            return this;
        }

        @Override
        public void end() {
            ended.set(true);
        }

        @Override
        public WriteStream<Buffer> setWriteQueueMaxSize(int maxSize) {
            return this;
        }

        @Override
        public boolean writeQueueFull() {
            return false;
        }

        @Override
        public WriteStream<Buffer> drainHandler(Handler<Void> handler) {
            return this;
        }
    };
    testResponseMissingBody(BodyCodec.pipe(stream));
    assertTrue(ended.get());
    assertEquals(0, length.get());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 2 with WriteStream

use of io.vertx.core.streams.WriteStream in project vertx-web by vert-x3.

the class WebClientTest method testResponseBodyCodecError.

@Test
public void testResponseBodyCodecError() throws Exception {
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        resp.setChunked(true);
        resp.end(TestUtils.randomBuffer(2048));
    });
    startServer();
    RuntimeException cause = new RuntimeException();
    WriteStream<Buffer> stream = new WriteStream<Buffer>() {

        Handler<Throwable> exceptionHandler;

        @Override
        public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
            exceptionHandler = handler;
            return this;
        }

        @Override
        public WriteStream<Buffer> write(Buffer data) {
            exceptionHandler.handle(cause);
            return this;
        }

        @Override
        public void end() {
        }

        @Override
        public WriteStream<Buffer> setWriteQueueMaxSize(int maxSize) {
            return this;
        }

        @Override
        public boolean writeQueueFull() {
            return false;
        }

        @Override
        public WriteStream<Buffer> drainHandler(Handler<Void> handler) {
            return this;
        }
    };
    HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
    get.as(BodyCodec.pipe(stream)).send(onFailure(err -> {
        assertSame(cause, err);
        testComplete();
    }));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) AsyncFile(io.vertx.core.file.AsyncFile) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Arrays(java.util.Arrays) DecodeException(io.vertx.core.json.DecodeException) HttpServer(io.vertx.core.http.HttpServer) MultiMap(io.vertx.core.MultiMap) BodyCodec(io.vertx.ext.web.codec.BodyCodec) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Cert(io.vertx.test.core.tls.Cert) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HttpTestBase(io.vertx.test.core.HttpTestBase) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) ConnectException(java.net.ConnectException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) ProxyOptions(io.vertx.core.net.ProxyOptions) OpenOptions(io.vertx.core.file.OpenOptions) Files(java.nio.file.Files) VertxOptions(io.vertx.core.VertxOptions) HttpHeaders(io.vertx.core.http.HttpHeaders) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) ProxyType(io.vertx.core.net.ProxyType) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) WineAndCheese(io.vertx.ext.web.client.jackson.WineAndCheese) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) Collections(java.util.Collections) HttpServerResponse(io.vertx.core.http.HttpServerResponse) WriteStream(io.vertx.core.streams.WriteStream) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 3 with WriteStream

use of io.vertx.core.streams.WriteStream in project vertx-web by vert-x3.

the class WebClientTest method testResponseBodyStreamError.

@Test
public void testResponseBodyStreamError() throws Exception {
    CompletableFuture<Void> fail = new CompletableFuture<>();
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        resp.setChunked(true);
        resp.write(TestUtils.randomBuffer(2048));
        fail.thenAccept(v -> resp.close());
    });
    startServer();
    AtomicInteger received = new AtomicInteger();
    WriteStream<Buffer> stream = new WriteStream<Buffer>() {

        @Override
        public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
            return this;
        }

        @Override
        public WriteStream<Buffer> write(Buffer data) {
            received.addAndGet(data.length());
            return this;
        }

        @Override
        public void end() {
        }

        @Override
        public WriteStream<Buffer> setWriteQueueMaxSize(int maxSize) {
            return this;
        }

        @Override
        public boolean writeQueueFull() {
            return false;
        }

        @Override
        public WriteStream<Buffer> drainHandler(Handler<Void> handler) {
            return this;
        }
    };
    HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
    get.as(BodyCodec.pipe(stream)).send(onFailure(err -> testComplete()));
    assertWaitUntil(() -> received.get() == 2048);
    fail.complete(null);
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) AsyncFile(io.vertx.core.file.AsyncFile) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Arrays(java.util.Arrays) DecodeException(io.vertx.core.json.DecodeException) HttpServer(io.vertx.core.http.HttpServer) MultiMap(io.vertx.core.MultiMap) BodyCodec(io.vertx.ext.web.codec.BodyCodec) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Cert(io.vertx.test.core.tls.Cert) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HttpTestBase(io.vertx.test.core.HttpTestBase) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) ConnectException(java.net.ConnectException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) ProxyOptions(io.vertx.core.net.ProxyOptions) OpenOptions(io.vertx.core.file.OpenOptions) Files(java.nio.file.Files) VertxOptions(io.vertx.core.VertxOptions) HttpHeaders(io.vertx.core.http.HttpHeaders) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) ProxyType(io.vertx.core.net.ProxyType) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) WineAndCheese(io.vertx.ext.web.client.jackson.WineAndCheese) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerResponse(io.vertx.core.http.HttpServerResponse) WriteStream(io.vertx.core.streams.WriteStream) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 4 with WriteStream

use of io.vertx.core.streams.WriteStream in project georocket by georocket.

the class StoreClientImportTest method importTagsAndProperties.

/**
 * Test importing tags and properties
 * @param context the test context
 * @throws Exception if something goes wrong
 */
@Test
public void importTagsAndProperties(TestContext context) throws Exception {
    String url = "/store?tags=testTag%2CtestTag2&props=hello%3Awo%5C%3Arld%2Challo2%3Aworld2";
    stubFor(post(urlEqualTo(url)).willReturn(aResponse().withStatus(202)));
    Async async = context.async();
    WriteStream<Buffer> w = client.getStore().startImport(null, Arrays.asList("testTag", "testTag2"), Arrays.asList("hello:wo\\:rld", "hallo2:world2"), context.asyncAssertSuccess(v -> {
        verifyPosted(url, XML, context);
        async.complete();
    }));
    w.end(Buffer.buffer(XML));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) Arrays(java.util.Arrays) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) WireMock.postRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) Optional(java.util.Optional) WireMock.urlEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo) VerificationException(com.github.tomakehurst.wiremock.client.VerificationException) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 5 with WriteStream

use of io.vertx.core.streams.WriteStream in project georocket by georocket.

the class StoreClientImportTest method importLayerWithSpecialChars.

/**
 * Test importing to a layer with special characters
 * @param context the test context
 */
@Test
public void importLayerWithSpecialChars(TestContext context) {
    String url = "/store/he%2Bllo/world/";
    stubFor(post(urlEqualTo(url)).willReturn(aResponse().withStatus(202)));
    Async async = context.async();
    WriteStream<Buffer> w = client.getStore().startImport("he+llo/world", context.asyncAssertSuccess(v -> {
        verifyPosted(url, XML, context);
        async.complete();
    }));
    w.end(Buffer.buffer(XML));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) Arrays(java.util.Arrays) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) WireMock.postRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) Optional(java.util.Optional) WireMock.urlEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo) VerificationException(com.github.tomakehurst.wiremock.client.VerificationException) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

WriteStream (io.vertx.core.streams.WriteStream)19 Buffer (io.vertx.core.buffer.Buffer)17 Test (org.junit.Test)17 Arrays (java.util.Arrays)15 Optional (java.util.Optional)9 VerificationException (com.github.tomakehurst.wiremock.client.VerificationException)8 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)8 WireMock.equalTo (com.github.tomakehurst.wiremock.client.WireMock.equalTo)8 WireMock.post (com.github.tomakehurst.wiremock.client.WireMock.post)8 WireMock.postRequestedFor (com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor)8 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)8 WireMock.urlEqualTo (com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo)8 WireMock.verify (com.github.tomakehurst.wiremock.client.WireMock.verify)8 Handler (io.vertx.core.Handler)8 Async (io.vertx.ext.unit.Async)8 TestContext (io.vertx.ext.unit.TestContext)8 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)8 RunWith (org.junit.runner.RunWith)8 ReadStream (io.vertx.core.streams.ReadStream)7 File (java.io.File)7