RequestHandlerImpl implements RequestHandler { @Override public void handle(OngoingRequest ongoingRequest) { try { rrf.create(ongoingRequest).run(this::handleEndpointMatch); } catch (Exception e) { LOGGER.error("Request matching/handling threw exception", e); try { ongoingRequest.reply(forStatus(INTERNAL_SERVER_ERROR)); } catch (Throwable t) { LOGGER.error("Caught throwable when replying with Internal Server Error", t); } } } RequestHandlerImpl(
      RequestRunnableFactory requestRunnableFactory,
      EndpointRunnableFactory endpointRunnableFactory,
      IncomingRequestAwareClient client); @Override void handle(OngoingRequest ongoingRequest);  }
@Test public void shouldRunRequestRunnable() throws Exception { requestHandler.handle(ongoingRequest); verify(requestRunnable).run(any()); }
@Test public void shouldRunEndpointRunnable() throws Exception { requestHandler.handle(ongoingRequest); verify(requestRunnable).run(continuationCaptor.capture()); continuationCaptor.getValue() .accept(ongoingRequest, match); verify(endpointFactory).create(eq(ongoingRequest), any(RequestContext.class), eq(endpoint)); verify(runnable).run(); }
@Test public void shouldReplySafelyForExceptions() throws Exception { doThrow(new NullPointerException("expected")).when(requestRunnable).run(any()); requestHandler.handle(ongoingRequest); verify(ongoingRequest).reply(forStatus(INTERNAL_SERVER_ERROR)); }
@Test public void shouldSetRequestContextMetadata() throws Exception { requestHandler.handle(ongoingRequest); verify(requestRunnable).run(continuationCaptor.capture()); continuationCaptor.getValue() .accept(ongoingRequest, match); final RequestContext requestContext = requestContextCaptor.getValue(); assertThat(requestContext.metadata(), is(requestMetadata)); }