### Question:
HttpResponseHandler extends SimpleChannelUpstreamHandler { @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { HttpResponse response = null; Integer statusCode = null; if(e.getMessage() instanceof HttpResponse){ response = (HttpResponse) e.getMessage(); statusCode = response.getStatus().getCode(); } Channel channel = ctx.getChannel(); if ((statusCode == null) || (statusCode != null && statusCode.intValue() != HttpStatus.SC_CONTINUE)) { channel.close(); } if(ctx.getAttachment() instanceof TokenValidationListener) { TokenValidationListener listener = (TokenValidationListener) ctx.getAttachment(); listener.responseReceived(response); } else { ResponseListener listener = (ResponseListener) ctx.getAttachment(); if (statusCode != null && statusCode >= HTTP_STATUS_300 && (listener.getErrorMessage(statusCode) != null)) { String content = listener.getErrorMessage(statusCode); if (content != null) { response.headers().set(HttpHeaders.Names.CONTENT_TYPE, HttpResponseFactory.APPLICATION_JSON); response.setContent(ChannelBuffers.copiedBuffer(content.getBytes(CharsetUtil.UTF_8))); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.getBytes(CharsetUtil.UTF_8).length); } } listener.responseReceived(response); } }  @Override void messageReceived(ChannelHandlerContext ctx, MessageEvent e); @Override void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e);  }### Answer:
@Test public void when_status_eror_not_mapped_return_backend_response() throws Exception { response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST); doReturn(response).when(e).getMessage(); handler.messageReceived(ctx, e); assertEquals(new String(response.getContent().array()), ""); }
@Test public void when_status_error_404_mapped_return_customized_response() throws Exception { response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); doReturn(response).when(e).getMessage(); handler.messageReceived(ctx, e); assertEquals(new String(response.getContent().array()), "{\"error\":\"resource not found\"}"); }
@Test public void when_status_error_500_mapped_return_customized_response() throws Exception { response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR); doReturn(response).when(e).getMessage(); handler.messageReceived(ctx, e); assertEquals(new String(response.getContent().array()), "{\"error\":\"ops...something wrong\"}"); }
@Test public void when_status_error_300_mapped_return_customized_response() throws Exception { response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.MULTIPLE_CHOICES); doReturn(response).when(e).getMessage(); handler.messageReceived(ctx, e); assertEquals(new String(response.getContent().array()), "{\"error\":\"further actions required\"}"); }
@Test public void when_status_201_mapped_do_not_return_customized_response() throws Exception { response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CREATED); doReturn(response).when(e).getMessage(); ConfigLoader.loadMappingsConfig(false); handler.messageReceived(ctx, e); assertNotEquals(new String(response.getContent().array()), "{\"message\":\"resource created\"}"); }