ImpressionRequestReceiver extends RequestReceiver<ImpressionController> { @Override public void receive(HttpReceiverContext ctx) { boolean unhandledException = true; try { Timer.Context timerContext = requestTimer().time(); ImpressionRequest request = newRequest(ctx.httpRequest()); ImpressionResponse response = newResponse(ctx.httpResponse()); controller().onRequest(request, response); successResponseMeter().mark(); timerContext.close(); unhandledException = false; } catch (InterceptorAbortException e) { unhandledException = false; logger.error("InterceptorAbortException thrown", e); interceptorAbortMeter().mark(); } finally { if (unhandledException) { interceptorOtherMeter().mark(); } } ctx.httpResponse().setMediaType(MediaType.GIF); try { PixelImage.write(ctx.httpResponse().content()); } catch (IOException e) { throw new IllegalStateException(e); } } @Inject  ImpressionRequestReceiver(
      Exchange exchange, MetricRegistry metricRegistry, ImpressionController controller); @Override void receive(HttpReceiverContext ctx);  }
@Test public void testInterceptorAbort() { HttpRequest httpRequest = newImpressionRequest("100"); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ImpressionInterceptor() { @Override public void execute(InterceptorChain<ImpressionRequest, ImpressionResponse> chain) { throw new InterceptorAbortException("Tracking ads is hard, let's go shopping"); }}).receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_OK, httpResponse.getStatusCode()); }
@Test(expected = NullPointerException.class) public void testInterceptorRuntimeException() { HttpRequest httpRequest = newImpressionRequest("GARBAGE"); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ImpressionInterceptor() { @Override public void execute(InterceptorChain<ImpressionRequest, ImpressionResponse> chain) { throw new NullPointerException(); }}).receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_OK, httpResponse.getStatusCode()); }
@Test public void testRequestResponse() { HttpRequest httpRequest = newImpressionRequest("100"); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver().receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_OK, httpResponse.getStatusCode()); }
@Test public void testBadRequest() { HttpRequest httpRequest = newImpressionRequest("GARBAGE"); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver().receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_OK, httpResponse.getStatusCode()); }
@Test public void testInterceptorAbort() { HttpRequest httpRequest = newImpressionRequest("100"); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ImpressionInterceptor() { @Override public void execute(InterceptorChain<ImpressionRequest, ImpressionResponse> chain) { throw new InterceptorAbortException("Tracking ads is hard, let's go shopping"); } }).receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_OK, httpResponse.getStatusCode()); }
@Test(expected = NullPointerException.class) public void testInterceptorRuntimeException() { HttpRequest httpRequest = newImpressionRequest("GARBAGE"); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ImpressionInterceptor() { @Override public void execute(InterceptorChain<ImpressionRequest, ImpressionResponse> chain) { throw new NullPointerException(); } }).receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_OK, httpResponse.getStatusCode()); }