CustomHttpResponseHandler { public boolean handle(HttpServerRequest request) { if (!request.uri().startsWith(path)) { return false; } HttpResponseStatus rs; String info = ""; try { String code = request.uri().substring(path.length()); while (code.startsWith(SLASH)) { code = code.substring(1); } int index = code.indexOf(SLASH); if(index != -1) { code = code.substring(0, index); } int codeAsInt = Integer.parseInt(code); rs = HttpResponseStatus.valueOf(codeAsInt); } catch (Exception ex) { LOG.warn("can't parse wanted response code from {}", request.uri(), ex); rs = HttpResponseStatus.BAD_REQUEST; info = ": missing, wrong or non-numeric status-code in request URL"; } request.response().setStatusCode(rs.code()).setStatusMessage(rs.reasonPhrase()).end(rs.toString() + info); return true; } CustomHttpResponseHandler(String rootPath); boolean handle(HttpServerRequest request);  }
@Test public void testRequestNotHandled(TestContext context){ HttpServerRequest request = new HttpServerRequest("/gateleen/server/someResource", mock(HttpServerResponse.class)); final boolean handled = responseHandler.handle(request); context.assertFalse(handled, "Request should not have been handled"); }
@Test public void testRequestHandledWithStatus503(TestContext context){ HttpServerResponse response = spy(new DummyHttpServerResponse()); HttpServerRequest request = new HttpServerRequest("/gateleen/server/return-with-status-code/503", response); final boolean handled = responseHandler.handle(request); context.assertTrue(handled, "Request should have been handled"); verify(response, times(1)).setStatusCode(eq(StatusCode.SERVICE_UNAVAILABLE.getStatusCode())); verify(response, times(1)).setStatusMessage(eq(StatusCode.SERVICE_UNAVAILABLE.getStatusMessage())); }
@Test public void testRequestHandledWithStatus405AndAdditionalPathSegments(TestContext context){ HttpServerResponse response = spy(new DummyHttpServerResponse()); HttpServerRequest request = new HttpServerRequest("/gateleen/server/return-with-status-code/405/ignored/path/segments", response); final boolean handled = responseHandler.handle(request); context.assertTrue(handled, "Request should have been handled"); verify(response, times(1)).setStatusCode(eq(StatusCode.METHOD_NOT_ALLOWED.getStatusCode())); verify(response, times(1)).setStatusMessage(eq(StatusCode.METHOD_NOT_ALLOWED.getStatusMessage())); }
@Test public void testRequestHandledWithInvalidStatus(TestContext context){ HttpServerResponse response = spy(new DummyHttpServerResponse()); HttpServerRequest request = new HttpServerRequest("/gateleen/server/return-with-status-code/not_a_number", response); final boolean handled = responseHandler.handle(request); context.assertTrue(handled, "Request should have been handled"); verify(response, times(1)).setStatusCode(eq(StatusCode.BAD_REQUEST.getStatusCode())); verify(response, times(1)).setStatusMessage(eq(StatusCode.BAD_REQUEST.getStatusMessage())); verify(response, times(1)).end(eq("400 Bad Request: missing, wrong or non-numeric status-code in request URL")); }
@Test public void testRequestHandledWithUnknownStatus(TestContext context){ HttpServerResponse response = spy(new DummyHttpServerResponse()); HttpServerRequest request = new HttpServerRequest("/gateleen/server/return-with-status-code/1234", response); final boolean handled = responseHandler.handle(request); context.assertTrue(handled, "Request should have been handled"); verify(response, times(1)).setStatusCode(eq(1234)); verify(response, times(1)).setStatusMessage(eq("Unknown Status (1234)")); }
@Test public void testRequestHandledWithMissingStatus(TestContext context){ HttpServerResponse response = spy(new DummyHttpServerResponse()); HttpServerRequest request = new HttpServerRequest("/gateleen/server/return-with-status-code/", response); final boolean handled = responseHandler.handle(request); context.assertTrue(handled, "Request should have been handled"); verify(response, times(1)).setStatusCode(eq(StatusCode.BAD_REQUEST.getStatusCode())); verify(response, times(1)).setStatusMessage(eq(StatusCode.BAD_REQUEST.getStatusMessage())); verify(response, times(1)).end(eq("400 Bad Request: missing, wrong or non-numeric status-code in request URL")); }