EchoRequestHandler implements RequestHandler { @Override public HttpResponse processRequest(HttpRequest request) { HttpResponse response = new HttpResponse(); response.setRequest(request); HttpStatusCode code; try { code = generateResponseCode(request.getQueryParameter("code")); } catch(InvalidHttpRequestException e) { return new HttpResponse(HttpStatusCode.BAD_REQUEST, e.getMessage().getBytes()); } response.setStatusCode(code); Integer bodyLength = null; String s = request.getQueryParameter("length"); if (s != null) { try { bodyLength = Integer.parseInt(s); } catch (Exception e) { return new HttpResponse(HttpStatusCode.BAD_REQUEST,("invalid body length \"" + s + "\"").getBytes()); } } byte[] body; if (bodyLength == null) { body = ("SYNTHETIC " + code + "\n").getBytes(); } else { body = generateRandomContent(bodyLength); } response.setBody(body); log.info(this + " responded with synthetic " + response.getStatusCode()); try { delayIfNecessary(request.getQueryParameter("delay")); } catch(InvalidHttpRequestException e) { return new HttpResponse(HttpStatusCode.BAD_REQUEST, e.getMessage().getBytes()); } return response; } EchoRequestHandler();  EchoRequestHandler(Long serverWideDelayMs); @Override boolean accepts(HttpRequest request); @Override HttpResponse processRequest(HttpRequest request); void setDelay(long serverWideDelayMs); Long getDelay(); @Override String toString();  }
@Test public void delay_RequestContainsDelayInvalidDelayValue() throws Exception { EchoRequestHandler h = getRequestHandlerToTest(); HttpRequest request = new HttpRequest(HttpMethod.GET, "/something?delay=blah"); HttpResponse response = h.processRequest(request); assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode()); String body = new String(response.getBody()); assertEquals("invalid delay parameter value \"blah\"", body); }
@Test public void customResponseCode() throws Exception { EchoRequestHandler h = getRequestHandlerToTest(); HttpRequest request = new HttpRequest(HttpMethod.GET, "/something?code=500"); HttpResponse response = h.processRequest(request); assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR, response.getStatusCode()); assertEquals("SYNTHETIC 500 Internal Server Error\n", new String(response.getBody())); }
@Test public void invalidCustomResponseCode() throws Exception { EchoRequestHandler h = getRequestHandlerToTest(); HttpRequest request = new HttpRequest(HttpMethod.GET, "/something?code=something"); HttpResponse response = h.processRequest(request); assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode()); assertEquals("invalid HTTP status code \"something\"", new String(response.getBody())); }
@Test public void customBodyLength() throws Exception { EchoRequestHandler h = getRequestHandlerToTest(); HttpRequest request = new HttpRequest(HttpMethod.GET, "/something?length=50"); HttpResponse response = h.processRequest(request); assertEquals(HttpStatusCode.OK, response.getStatusCode()); byte[] body = response.getBody(); assertEquals(50, body.length); assertEquals('\n', body[49]); log.info(new String(body)); }
@Test public void invalidCustomBodyLength() throws Exception { EchoRequestHandler h = getRequestHandlerToTest(); HttpRequest request = new HttpRequest(HttpMethod.GET, "/something?length=blah"); HttpResponse response = h.processRequest(request); assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode()); assertEquals("invalid body length \"blah\"", new String(response.getBody())); }