ClickRequestReceiver extends RequestReceiver<ClickController> { @Override public void receive(HttpReceiverContext ctx) { boolean unhandledException = true; try { Timer.Context timerContext = requestTimer().time(); ClickRequest request = newRequest(ctx.httpRequest()).build(); ClickResponse response = newResponse(ctx.httpResponse()).build(); controller().onRequest(request, response); if (!(HttpStatusType.REDIRECT.contains(ctx.httpResponse().getStatusCode()) && ctx.httpResponse().hasRedirectUri())) { noRedirectMeter.mark(); logger.debug("No redirect location: {}", response); ctx.httpResponse().setStatusOk(); } else { if (logger.isDebugEnabled()) { logger.debug("Redirect: {}", response.getRedirectLocation()); } ctx.httpResponse().setStatusCode(HttpStatus.SC_MOVED_TEMPORARILY); ctx.httpResponse().setRedirectUri(response.getRedirectLocation()); } successResponseMeter().mark(); timerContext.close(); unhandledException = false; } catch (InterceptorAbortException e) { unhandledException = false; ctx.httpResponse().setStatusOk(); logger.error("InterceptorAbortException thrown", e); interceptorAbortMeter().mark(); } finally { if (unhandledException) { interceptorOtherMeter().mark(); } } } @Inject  ClickRequestReceiver(
      Exchange exchange, MetricRegistry metricRegistry, ClickController controller); @Override void receive(HttpReceiverContext ctx);  }
@Test public void testInterceptorAbort() { HttpRequest httpRequest = newHttpClickRequest(AD_URL); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ClickInterceptor() { @Override public void execute(InterceptorChain<ClickRequest, ClickResponse> 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 = newHttpClickRequest(AD_URL); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ClickInterceptor() { @Override public void execute(InterceptorChain<ClickRequest, ClickResponse> chain) { throw new NullPointerException(); }}).receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); }
@Test public void testRequestResponse() { HttpRequest httpRequest = newHttpClickRequest(AD_URL); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ClickRedirectInterceptor()).receive( new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, httpResponse.getStatusCode()); assertEquals(AD_URL, String.valueOf(httpResponse.getRedirectUri())); }
@Test public void testRequestResponseNoLocation() { HttpRequest httpRequest = newHttpClickRequest(null); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver().receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); assertEquals(HttpStatus.SC_OK, httpResponse.getStatusCode()); assertNull(httpResponse.getRedirectUri()); }
@Test public void testInterceptorAbort() { HttpRequest httpRequest = newHttpClickRequest(AD_URL); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ClickInterceptor() { @Override public void execute(InterceptorChain<ClickRequest, ClickResponse> 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 = newHttpClickRequest(AD_URL); HttpResponse.Builder httpResponse = StandardHttpResponse.newBuilder(); newReceiver(new ClickInterceptor() { @Override public void execute(InterceptorChain<ClickRequest, ClickResponse> chain) { throw new NullPointerException(); } }).receive(new DefaultHttpReceiverContext(httpRequest, httpResponse)); }