HelloHttp implements HttpFunction { @Override public void service(HttpRequest request, HttpResponse response) throws IOException { String name = request.getFirstQueryParameter("name").orElse("world"); try { JsonElement requestParsed = gson.fromJson(request.getReader(), JsonElement.class); JsonObject requestJson = null; if (requestParsed != null && requestParsed.isJsonObject()) { requestJson = requestParsed.getAsJsonObject(); } if (requestJson != null && requestJson.has("name")) { name = requestJson.get("name").getAsString(); } } catch (JsonParseException e) { logger.severe("Error parsing JSON: " + e.getMessage()); } var writer = new PrintWriter(response.getWriter()); writer.printf("Hello %s!", name); }  @Override void service(HttpRequest request, HttpResponse response);  }
@Test public void helloHttp_noParamsGet() throws IOException { new HelloHttp().service(request, response); writerOut.flush(); assertThat(responseOut.toString()).isEqualTo("Hello world!"); }
@Test public void helloHttp_urlParamsGet() throws IOException { when(request.getFirstQueryParameter("name")).thenReturn(Optional.of("Tom")); new HelloHttp().service(request, response); writerOut.flush(); assertThat(responseOut.toString()).isEqualTo("Hello Tom!"); }
@Test public void helloHttp_bodyParamsPost() throws IOException { String requestJson = gson.toJson(Map.of("name", "Jane")); BufferedReader jsonReader = new BufferedReader(new StringReader("{'name': 'Jane'}")); when(request.getReader()).thenReturn(jsonReader); new HelloHttp().service(request, response); writerOut.flush(); assertThat(responseOut.toString()).isEqualTo("Hello Jane!"); }