Skip to content

Instantly share code, notes, and snippets.

@alexlehm
Created March 5, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexlehm/e507c38f25d35b37b80b to your computer and use it in GitHub Desktop.
Save alexlehm/e507c38f25d35b37b80b to your computer and use it in GitHub Desktop.
HttpTest.java
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpServer;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.test.core.VertxTestBase;
public class HttpTest extends VertxTestBase {
private static final int HTTP_PORT = 12345;
private SomeClass fixture;
@Test
public void testHandle() {
final Logger logger=LoggerFactory.getLogger(this.getClass());
fixture=new SomeClass(vertx);
final HttpServer server=vertx.createHttpServer().requestHandler(req -> {
logger.info("calling fixture.handler");
fixture.handle(req);
}).listen(HTTP_PORT);
HttpClientOptions options = new HttpClientOptions().setDefaultHost("localhost").setDefaultPort(HTTP_PORT);
HttpClient httpClient=vertx.createHttpClient(options);
AtomicInteger statusCode=new AtomicInteger(0);
AtomicReference<String> result=new AtomicReference<>("");
HttpClientRequest cReq=httpClient.get("/url", cResp -> {
statusCode.set(cResp.statusCode());
cResp.bodyHandler(buffer -> result.set(buffer.toString()));
cResp.endHandler(v -> {
server.close();
assertEquals(200,statusCode.get());
assertEquals("finished",result.get());
testComplete();
});
});
cReq.end();
await();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment