Skip to content

Commit

Permalink
working virtual proxy client
Browse files Browse the repository at this point in the history
  • Loading branch information
patriot1burke committed May 17, 2024
1 parent 5c25573 commit 4f67806
Show file tree
Hide file tree
Showing 4 changed files with 433 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

import java.util.concurrent.TimeUnit;

import jakarta.enterprise.event.Observes;
import jakarta.inject.Singleton;

Expand All @@ -17,6 +19,9 @@
import io.quarkus.vertx.http.runtime.devmode.DevSpaceProxyRecorder;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.impl.VertxBuilder;
import io.vertx.core.impl.VertxThread;
import io.vertx.core.spi.VertxThreadFactory;
import io.vertx.ext.web.Router;

public class DevSpaceProxyTest {
Expand Down Expand Up @@ -44,7 +49,10 @@ public class DevSpaceProxyTest {
public static class RouteProducer {
void observeRouter(@Observes Router router) {
router.route().handler(
request -> request.response().setStatusCode(200).putHeader("Content-Type", "text/plain").end("local"));
request -> {
System.out.println("************ CALLED LOCAL SERVER **************");
request.response().setStatusCode(200).putHeader("Content-Type", "text/plain").end("local");
});
}

}
Expand All @@ -53,7 +61,13 @@ void observeRouter(@Observes Router router) {

@BeforeAll
public static void before() {
vertx = Vertx.vertx();
vertx = new VertxBuilder()
.threadFactory(new VertxThreadFactory() {
public VertxThread newVertxThread(Runnable target, String name, boolean worker, long maxExecTime,
TimeUnit maxExecTimeUnit) {
return new VertxThread(target, "TEST-VERTX." + name, worker, maxExecTime, maxExecTimeUnit);
}
}).init().vertx();
myService = vertx.createHttpServer();
ProxyUtils.await(1000, myService.requestHandler(request -> {
request.response().setStatusCode(200).putHeader("Content-Type", "text/plain").end("my-service");
Expand All @@ -65,16 +79,18 @@ public static void before() {
ServiceConfig config = new ServiceConfig("my-service", "localhost", SERVICE_PORT);
proxyServer.init(vertx, proxyRouter, config);
ProxyUtils.await(1000, proxy.requestHandler(proxyRouter).listen(PROXY_PORT));
DevSpaceProxyRecorder.startSession();

}

@AfterAll
public static void after() {
System.out.println(" ------- CLEANUP TEST ------");
if (vertx != null) {
ProxyUtils.await(1000, myService.close());
System.out.println(" ------- Cleaned up my-service ------");
ProxyUtils.await(1000, proxy.close());
System.out.println(" ------- Cleaned up proxy ------");
ProxyUtils.await(1000, vertx.close());
System.out.println(" ------- Cleaned up test vertx ------");
}
}

Expand Down Expand Up @@ -103,20 +119,23 @@ public void testProxy() {
.statusCode(200)
.body(equalTo("my-service"));
// invoke local directly
given()
.when()
.get("/yo")
.then()
.statusCode(200)
.body(equalTo("local"));
given()
.when()
.body("hello")
.contentType("text/plain")
.post("/yo")
.then()
.statusCode(200)
.body(equalTo("local"));
/*
* given()
* .when()
* .get("/yo")
* .then()
* .statusCode(200)
* .body(equalTo("local"));
* given()
* .when()
* .body("hello")
* .contentType("text/plain")
* .post("/yo")
* .then()
* .statusCode(200)
* .body(equalTo("local"));
*
*/
// invoke proxy
given()
.when()
Expand All @@ -136,13 +155,15 @@ public void testProxy() {
.body(equalTo("my-service"));
}

//@Test
@Test
public void testGlobalSession() throws Exception {

try {
DevSpaceProxyRecorder.startSession();
System.out.println("------------------ POST REQUEST BODY ---------------------");
given()
.when()
.port(PROXY_PORT)
.contentType("text/plain")
.body("hello")
.post("/hey")
Expand All @@ -153,6 +174,7 @@ public void testGlobalSession() throws Exception {
System.out.println("-------------------- GET REQUEST --------------------");
given()
.when()
.port(PROXY_PORT)
.get("/yo")
.then()
.statusCode(200)
Expand All @@ -161,16 +183,19 @@ public void testGlobalSession() throws Exception {
System.out.println("------------------ POST REQUEST NO BODY ---------------------");
given()
.when()
.post("/hey")
.port(PROXY_PORT)
.post("/nobody")
.then()
.statusCode(200)
.contentType(equalTo("text/plain"))
.body(equalTo("local"));
} finally {
DevSpaceProxyRecorder.closeSession();
}
System.out.println("-------------------- After Shutdown GET REQUEST --------------------");
given()
.when()
.port(PROXY_PORT)
.get("/yo")
.then()
.statusCode(200)
Expand Down
Loading

0 comments on commit 4f67806

Please sign in to comment.