@Test public void handle() { ByteArrayOutputStream output = new ByteArrayOutputStream(); TestApiHandler handler = new TestApiHandler(); handler.handleRequest(new ByteArrayInputStream(("{" + "\"path\": \"/test\", " + "\"httpMethod\": \"GET\", " + "\"headers\": {" + "\"Authorization\": \"token\", " + "\"Accept\": \"application/json\"" + "}," + "\"queryStringParameters\": {" + "\"param1\": \"value1\", " + "\"param2\": \"value2\"" + "}" + "}") .getBytes(Charsets.UTF_8)), output, null); JSONObject response = new JSONObject(new String(output.toByteArray(), Charsets.UTF_8)); assertEquals(200, response.optInt("statusCode")); assertEquals("GET /test (Authorization=token,Accept=application/json,) (param1=value1,param2=value2,)", response.optString("body")); assertEquals("http: output = new ByteArrayOutputStream(); handler.handleRequest(new ByteArrayInputStream(("{ \"path\": \"/test\" }").getBytes(Charsets.UTF_8)), output, null); response = new JSONObject(new String(output.toByteArray(), Charsets.UTF_8)); assertEquals(400, response.optInt("statusCode")); assertEquals("", response.optString("body")); output = new ByteArrayOutputStream(); handler.handleRequest(new ByteArrayInputStream(("{" + "\"path\": \"/null\", " + "\"httpMethod\": \"GET\"" + "}") .getBytes(Charsets.UTF_8)), output, null); response = new JSONObject(new String(output.toByteArray(), Charsets.UTF_8)); assertEquals(404, response.optInt("statusCode")); assertEquals("", response.optString("body")); output = new ByteArrayOutputStream(); handler.handleRequest(new ByteArrayInputStream(("{" + "\"path\": \"/other\", " + "\"httpMethod\": \"GET\"" + "}") .getBytes(Charsets.UTF_8)), output, null); response = new JSONObject(new String(output.toByteArray(), Charsets.UTF_8)); assertEquals(200, response.optInt("statusCode")); assertEquals("else", response.optString("body")); output = new ByteArrayOutputStream(); handler.handleRequest(new ByteArrayInputStream(("{" + "\"path\": \"/error\", " + "\"httpMethod\": \"GET\"" + "}") .getBytes(Charsets.UTF_8)), output, null); response = new JSONObject(new String(output.toByteArray(), Charsets.UTF_8)); assertEquals(500, response.optInt("statusCode")); assertEquals("", response.optString("body")); output = new ByteArrayOutputStream(); handler.handleRequest(new ByteArrayInputStream(("{" + "\"path\": \"/unhandled\", " + "\"httpMethod\": \"GET\"" + "}") .getBytes(Charsets.UTF_8)), output, null); response = new JSONObject(new String(output.toByteArray(), Charsets.UTF_8)); assertEquals(500, response.optInt("statusCode")); assertEquals("", response.optString("body")); }
private JSONObject handle(JSONObject requestJson) { String path = requestJson.optString(PATH_PROPERTY, null); Multimap<String, String> headers = toStringMap(requestJson.optJSONObject(HEADERS_PROPERTY)); Multimap<String, String> queryParameters = toStringMap(requestJson.optJSONObject(QUERY_PARAMETERS_PROPERTY)); String method = requestJson.optString(HTTP_METHOD_PROPERTY, null); String body = requestJson.optString(BODY_PROPERTY, null); if (method != null) { logger.debug("{} {} request received", method, path); Route route = constructRoute(new RouteBuilderImpl()); ServerRequest request = buildRequest(method, path, headers, queryParameters, body); return handleWithRoute(route, request); } else { logger.debug("Received request with no HTTP method!"); return badRequest(); } }
ApiHandler implements RequestStreamHandler { private JSONObject handle(JSONObject requestJson) { String path = requestJson.optString(PATH_PROPERTY, null); Multimap<String, String> headers = toStringMap(requestJson.optJSONObject(HEADERS_PROPERTY)); Multimap<String, String> queryParameters = toStringMap(requestJson.optJSONObject(QUERY_PARAMETERS_PROPERTY)); String method = requestJson.optString(HTTP_METHOD_PROPERTY, null); String body = requestJson.optString(BODY_PROPERTY, null); if (method != null) { logger.debug("{} {} request received", method, path); Route route = constructRoute(new RouteBuilderImpl()); ServerRequest request = buildRequest(method, path, headers, queryParameters, body); return handleWithRoute(route, request); } else { logger.debug("Received request with no HTTP method!"); return badRequest(); } } }
ApiHandler implements RequestStreamHandler { private JSONObject handle(JSONObject requestJson) { String path = requestJson.optString(PATH_PROPERTY, null); Multimap<String, String> headers = toStringMap(requestJson.optJSONObject(HEADERS_PROPERTY)); Multimap<String, String> queryParameters = toStringMap(requestJson.optJSONObject(QUERY_PARAMETERS_PROPERTY)); String method = requestJson.optString(HTTP_METHOD_PROPERTY, null); String body = requestJson.optString(BODY_PROPERTY, null); if (method != null) { logger.debug("{} {} request received", method, path); Route route = constructRoute(new RouteBuilderImpl()); ServerRequest request = buildRequest(method, path, headers, queryParameters, body); return handleWithRoute(route, request); } else { logger.debug("Received request with no HTTP method!"); return badRequest(); } }  }
ApiHandler implements RequestStreamHandler { private JSONObject handle(JSONObject requestJson) { String path = requestJson.optString(PATH_PROPERTY, null); Multimap<String, String> headers = toStringMap(requestJson.optJSONObject(HEADERS_PROPERTY)); Multimap<String, String> queryParameters = toStringMap(requestJson.optJSONObject(QUERY_PARAMETERS_PROPERTY)); String method = requestJson.optString(HTTP_METHOD_PROPERTY, null); String body = requestJson.optString(BODY_PROPERTY, null); if (method != null) { logger.debug("{} {} request received", method, path); Route route = constructRoute(new RouteBuilderImpl()); ServerRequest request = buildRequest(method, path, headers, queryParameters, body); return handleWithRoute(route, request); } else { logger.debug("Received request with no HTTP method!"); return badRequest(); } }  @Override final void handleRequest(InputStream requestStream, OutputStream responseStream, Context __); }
ApiHandler implements RequestStreamHandler { private JSONObject handle(JSONObject requestJson) { String path = requestJson.optString(PATH_PROPERTY, null); Multimap<String, String> headers = toStringMap(requestJson.optJSONObject(HEADERS_PROPERTY)); Multimap<String, String> queryParameters = toStringMap(requestJson.optJSONObject(QUERY_PARAMETERS_PROPERTY)); String method = requestJson.optString(HTTP_METHOD_PROPERTY, null); String body = requestJson.optString(BODY_PROPERTY, null); if (method != null) { logger.debug("{} {} request received", method, path); Route route = constructRoute(new RouteBuilderImpl()); ServerRequest request = buildRequest(method, path, headers, queryParameters, body); return handleWithRoute(route, request); } else { logger.debug("Received request with no HTTP method!"); return badRequest(); } }  @Override final void handleRequest(InputStream requestStream, OutputStream responseStream, Context __);  }