io.vertx.rxjava.core.http.HttpServer Java Examples

The following examples show how to use io.vertx.rxjava.core.http.HttpServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testGet() {
  int times = 5;
  waitFor(times);
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.response().setChunked(true).end("some_content"));
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Single<HttpResponse<Buffer>> single = client
        .get(8080, "localhost", "/the_uri")
        .as(BodyCodec.buffer())
        .rxSend();
      for (int i = 0; i < times; i++) {
        single.subscribe(resp -> {
          Buffer body = resp.body();
          assertEquals("some_content", body.toString("UTF-8"));
          complete();
        }, this::fail);
      }
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #2
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testPost() {
  int times = 5;
  waitFor(times);
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.bodyHandler(buff -> {
    assertEquals("onetwothree", buff.toString());
    req.response().end();
  }));
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Observable<Buffer> stream = Observable.just(Buffer.buffer("one"), Buffer.buffer("two"), Buffer.buffer("three"));
      Single<HttpResponse<Buffer>> single = client
        .post(8080, "localhost", "/the_uri")
        .rxSendStream(stream);
      for (int i = 0; i < times; i++) {
        single.subscribe(resp -> complete(), this::fail);
      }
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #3
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testResponseMissingBody() throws Exception {
  int times = 5;
  waitFor(times);
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.response().setStatusCode(403).end());
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Single<HttpResponse<Buffer>> single = client
        .get(8080, "localhost", "/the_uri")
        .rxSend();
      for (int i = 0; i < times; i++) {
        single.subscribe(resp -> {
          assertEquals(403, resp.statusCode());
          assertNull(resp.body());
          complete();
        }, this::fail);
      }
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #4
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testResponseBodyAsAsJsonMapped() throws Exception {
  JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.response().end(expected.encode()));
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Single<HttpResponse<WineAndCheese>> single = client
        .get(8080, "localhost", "/the_uri")
        .as(BodyCodec.json(WineAndCheese.class))
        .rxSend();
      single.subscribe(resp -> {
        assertEquals(200, resp.statusCode());
        assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body());
        testComplete();
      }, this::fail);
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #5
Source File: CoreRxifiedApiTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeploy() throws Exception {
  AtomicInteger count = new AtomicInteger();
  vertx.deployVerticle(new AbstractVerticle() {
    @Override
    public void start() throws Exception {
      HttpServer s1 = vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(req -> {
      });
      HttpServer s2 = vertx.createHttpServer(new HttpServerOptions().setPort(8081)).requestHandler(req -> {
      });
      Single<HttpServer> f1 = s1.rxListen();
      Single<HttpServer> f2 = s2.rxListen();
      Action1<HttpServer> done = server -> {
        if (count.incrementAndGet() == 2) {
          testComplete();
        }
      };
      f1.subscribe(done);
      f2.subscribe(done);
    }
  });
  await();
}
 
Example #6
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testObserverToFuture() {
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(req -> {});
  AtomicInteger count = new AtomicInteger();
  Observer<HttpServer> observer = new Observer<HttpServer>() {
    @Override
    public void onCompleted() {
      server.close();
      assertEquals(1, count.get());
      testComplete();
    }

    @Override
    public void onError(Throwable e) {
      fail(e.getMessage());
    }

    @Override
    public void onNext(HttpServer httpServer) {
      count.incrementAndGet();
    }
  };
  Single<HttpServer> onListen = server.rxListen();
  onListen.subscribe(observer);
  await();
}
 
Example #7
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpClient() {
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> {
    req.response().setChunked(true).end("some_content");
  });
  try {
    server.listen(ar -> {
      HttpClient client = vertx.createHttpClient(new HttpClientOptions());
      client.rxGet(8080, "localhost", "/the_uri").subscribe(resp -> {
        Buffer content = Buffer.buffer();
        Observable<Buffer> observable = resp.toObservable();
        observable.forEach(content::appendBuffer, err -> fail(), () -> {
          assertEquals("some_content", content.toString("UTF-8"));
          testComplete();
        });
      });
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #8
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testWebsocketClientFlatMap() {
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.webSocketStream().handler(ws -> {
    ws.write(Buffer.buffer("some_content"));
    ws.close();
  });
  server.listen(ar -> {
    HttpClient client = vertx.createHttpClient(new HttpClientOptions());
    Buffer content = Buffer.buffer();
    client.
        rxWebSocket(8080, "localhost", "/the_uri").
        flatMapObservable(WebSocket::toObservable).
        forEach(content::appendBuffer, err -> fail(), () -> {
          server.close();
          assertEquals("some_content", content.toString("UTF-8"));
          testComplete();
        });
  });
  await();
}
 
Example #9
Source File: HttpServerVert.java    From Learn-Java-12-Programming with MIT License 5 votes vote down vote up
public void start1(Future<Void> startFuture) {
    String name = this.getClass().getSimpleName() + "(" + Thread.currentThread().getName() + ", localhost:" + port + ")";

    HttpServer server = vertx.createHttpServer();

    server.requestStream().toObservable()
          .subscribe(request -> request.response()
                    .setStatusCode(200)
                    .end("Hello from " + name + "!\n")
           );

    server.rxListen(port).subscribe();

    System.out.println(name + " is waiting...");
}
 
Example #10
Source File: AuditVerticle.java    From vertx-microservices-workshop with Apache License 2.0 5 votes vote down vote up
private Single<HttpServer> configureTheHTTPServer() {
  //----
  // Use a Vert.x Web router for this REST API.
  Router router = Router.router(vertx);
  router.get("/").handler(this::retrieveOperations);
  HttpServer server = vertx.createHttpServer().requestHandler(router::accept);
  Integer port = config().getInteger("http.port", 0);
  return server.rxListen(port);
  //----
}
 
Example #11
Source File: AuditVerticle.java    From vertx-microservices-workshop with Apache License 2.0 5 votes vote down vote up
private Single<HttpServer> configureTheHTTPServer() {

    //TODO
    //----
    Single<HttpServer> httpServerSingle = Single.error(new UnsupportedOperationException("not yet implemented"));
    //----

    return httpServerSingle;
  }