RequestRunnableImpl implements RequestRunnable { @Override public void run(BiConsumer<OngoingRequest, RuleMatch<Endpoint>> matchContinuation) { try { matchAndRun(matchContinuation); } catch (Exception e) { LOG.error("Exception when handling request", e); ongoingRequest.reply(forStatus(INTERNAL_SERVER_ERROR)); } } RequestRunnableImpl(OngoingRequest ongoingRequest, ApplicationRouter<Endpoint> applicationRouter); @Override void run(BiConsumer<OngoingRequest, RuleMatch<Endpoint>> matchContinuation);  }
@Test public void testRunsMatchedEndpoint() { requestRunnable.run(matchContinuation); verify(matchContinuation, times(1)).accept(eq(ongoingRequest), eq(match)); }
@Test public void testMatchingFails() throws Exception { when(applicationRouter.match(any(Request.class))).thenReturn(Optional.empty()); when(applicationRouter.getMethodsForValidRules(any(Request.class))) .thenReturn(Collections.emptyList()); requestRunnable.run(matchContinuation); verify(ongoingRequest).reply(forStatus(NOT_FOUND)); }
@Test public void testWrongMethod() throws Exception { when(applicationRouter.match(any(Request.class))).thenReturn(Optional.empty()); when(applicationRouter.getMethodsForValidRules(any(Request.class))) .thenReturn(Collections.singleton("POST")); when(message.method()).thenReturn("GET"); requestRunnable.run(matchContinuation); verify(ongoingRequest).reply(responseArgumentCaptor.capture()); Response<ByteString> reply = responseArgumentCaptor.getValue(); assertEquals(reply.status(), Status.METHOD_NOT_ALLOWED); assertEquals(reply.headerEntries(), Collections.singletonList(new SimpleEntry<>("Allow", "OPTIONS, POST"))); }
@Test public void testWithMethodOptions() throws Exception { when(applicationRouter.match(any(Request.class))).thenReturn(Optional.empty()); when(applicationRouter.getMethodsForValidRules(any(Request.class))) .thenReturn(Collections.singleton("POST")); when(message.method()).thenReturn("OPTIONS"); requestRunnable.run(matchContinuation); verify(ongoingRequest).reply(responseArgumentCaptor.capture()); Response<ByteString> response = responseArgumentCaptor.getValue(); assertThat(response.status(), is(Status.NO_CONTENT)); assertThat(response.headerEntries(), is(Collections.singletonList(new SimpleEntry<>("Allow", "OPTIONS, POST")))); }
@Test public void shouldReply500IfApplicationRouterMatchThrows() throws Exception { when(applicationRouter.match(any(Request.class))).thenThrow(new RuntimeException("expected")); requestRunnable.run(matchContinuation); verify(ongoingRequest).reply(forStatus(INTERNAL_SERVER_ERROR)); }
@Test public void shouldReply500IfApplicationRouterValidMethodsThrows() throws Exception { when(applicationRouter.match(any(Request.class))).thenReturn(Optional.empty()); when(applicationRouter.getMethodsForValidRules(any(Request.class))) .thenThrow(new RuntimeException("expected")); requestRunnable.run(matchContinuation); verify(ongoingRequest).reply(forStatus(INTERNAL_SERVER_ERROR)); }