Skip to content
Permalink
Browse files
Add http method test for vertx-http server
### Motivation

As discussed at #1953, adding test to verify all http-method works for vertx-http-server.



Reviewers: Enrico Olivelli <eolivelli@gmail.com>, Sijie Guo <sijie@apache.org>

This closes #1963 from rdhabalia/gc_test
  • Loading branch information
rdhabalia authored and sijie committed Feb 27, 2019
1 parent b689d83 commit 4e32ef618fd3b6a60d9ff7f78ea40c8d11464d96
Showing 1 changed file with 21 additions and 5 deletions.
@@ -47,7 +47,7 @@ public void testStartBasicHttpServer() throws Exception {
httpServer.initialize(httpServiceProvider);
assertTrue(httpServer.startServer(0));
int port = httpServer.getListeningPort();
HttpResponse httpResponse = sendGet(getUrl(port, HttpRouter.HEARTBEAT));
HttpResponse httpResponse = send(getUrl(port, HttpRouter.HEARTBEAT), HttpServer.Method.GET);
assertEquals(HttpServer.StatusCode.OK.getValue(), httpResponse.responseCode);
assertEquals(HeartbeatService.HEARTBEAT.trim(), httpResponse.responseBody.trim());
httpServer.stopServer();
@@ -60,17 +60,33 @@ public void testStartMetricsServiceOnRouterPath() throws Exception {
httpServer.initialize(httpServiceProvider);
assertTrue(httpServer.startServer(0));
int port = httpServer.getListeningPort();
HttpResponse httpResponse = sendGet(getUrl(port, HttpRouter.METRICS));
HttpResponse httpResponse = send(getUrl(port, HttpRouter.METRICS), HttpServer.Method.GET);
assertEquals(HttpServer.StatusCode.OK.getValue(), httpResponse.responseCode);
httpServer.stopServer();
}

// HTTP GET request
private HttpResponse sendGet(String url) throws IOException {
@Test
public void testHttpMethods() throws Exception {
VertxHttpServer httpServer = new VertxHttpServer();
HttpServiceProvider httpServiceProvider = NullHttpServiceProvider.getInstance();
httpServer.initialize(httpServiceProvider);
assertTrue(httpServer.startServer(0));
int port = httpServer.getListeningPort();
HttpResponse httpResponse = send(getUrl(port, HttpRouter.GC), HttpServer.Method.GET);
assertEquals(HttpServer.StatusCode.OK.getValue(), httpResponse.responseCode);
httpResponse = send(getUrl(port, HttpRouter.GC), HttpServer.Method.POST);
assertEquals(HttpServer.StatusCode.OK.getValue(), httpResponse.responseCode);
httpResponse = send(getUrl(port, HttpRouter.GC), HttpServer.Method.PUT);
assertEquals(HttpServer.StatusCode.OK.getValue(), httpResponse.responseCode);
httpServer.stopServer();
}

// HTTP request
private HttpResponse send(String url, HttpServer.Method method) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional, default is GET
con.setRequestMethod("GET");
con.setRequestMethod(method.toString());
int responseCode = con.getResponseCode();
StringBuilder response = new StringBuilder();
BufferedReader in = null;

0 comments on commit 4e32ef6

Please sign in to comment.