HttpMethod implements HttpFunction { @Override public void service(HttpRequest request, HttpResponse response) throws IOException { BufferedWriter writer = response.getWriter(); switch (request.getMethod()) { case "GET": response.setStatusCode(HttpURLConnection.HTTP_OK); writer.write("Hello world!"); break; case "PUT": response.setStatusCode(HttpURLConnection.HTTP_FORBIDDEN); writer.write("Forbidden!"); break; default: response.setStatusCode(HttpURLConnection.HTTP_BAD_METHOD); writer.write("Something blew up!"); break; } }  @Override void service(HttpRequest request, HttpResponse response);  }
@Test public void functionsHttpMethod_shouldAcceptGet() throws IOException { when(request.getMethod()).thenReturn("GET"); new HttpMethod().service(request, response); writerOut.flush(); verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_OK); assertThat(responseOut.toString()).isEqualTo("Hello world!"); }
@Test public void functionsHttpMethod_shouldForbidPut() throws IOException { when(request.getMethod()).thenReturn("PUT"); new HttpMethod().service(request, response); writerOut.flush(); verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_FORBIDDEN); assertThat(responseOut.toString()).isEqualTo("Forbidden!"); }
@Test public void functionsHttpMethod_shouldErrorOnPost() throws IOException { when(request.getMethod()).thenReturn("POST"); new HttpMethod().service(request, response); writerOut.flush(); verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_BAD_METHOD); assertThat(responseOut.toString()).isEqualTo("Something blew up!"); }