@Test(expected = PathNotFound404Exception.class) public void findSingleEndpointForExecution_throws_PathNotFound404Exception_if_no_matching_path() { // given doReturn(Optional.empty()).when(matcherMock).matchesPath(any(RequestInfo.class)); // expect handlerSpy.findSingleEndpointForExecution(requestInfoMock); }
@Test(expected = MethodNotAllowed405Exception.class) public void findSingleEndpointForExecution_throws_MethodNotAllowed405Exception_if_path_matches_but_method_does_not() { // given doReturn(Optional.of(defaultPath)).when(matcherMock).matchesPath(any(RequestInfo.class)); doReturn(false).when(matcherMock).matchesMethod(any(RequestInfo.class)); // expect handlerSpy.findSingleEndpointForExecution(requestInfoMock); }
@Test(expected = MultipleMatchingEndpointsException.class) public void findSingleEndpointForExecution_throws_MultipleMatchingEndpointsException_if_multiple_endpoints_fully_match() { // given doReturn(Optional.of(defaultPath)).when(matcherMock).matchesPath(any(RequestInfo.class)); doReturn(true).when(matcherMock).matchesMethod(any(RequestInfo.class)); Endpoint<?> alsoMatchingEndpointMock = mock(Endpoint.class); doReturn(matcherMock).when(alsoMatchingEndpointMock).requestMatcher(); endpoints.add(alsoMatchingEndpointMock); // when handlerSpy.findSingleEndpointForExecution(requestInfoMock); }
@Test public void doChannelRead_calls_findSingleEndpointForExecution_then_sets_path_params_and_endpoint_on_state_then_returns_CONTINUE_if_msg_is_HttpRequest() { // given doReturn(Arrays.asList(defaultPath)).when(matcherMock).matchingPathTemplates(); HttpRequest msg = mock(HttpRequest.class); // when PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg); // then verify(handlerSpy).findSingleEndpointForExecution(requestInfoMock); verify(requestInfoMock).setPathParamsBasedOnPathTemplate(defaultPath); verify(stateMock).setEndpointForExecution(endpointMock, defaultPath); assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); }
@Test public void findSingleEndpointForExecution_returns_matching_endpoint() { // given doReturn(Optional.of(defaultPath)).when(matcherMock).matchesPath(any(RequestInfo.class)); doReturn(true).when(matcherMock).matchesMethod(any(RequestInfo.class)); // when Pair<Endpoint<?>, String> result = handlerSpy.findSingleEndpointForExecution(requestInfoMock); // then assertThat(result.getKey()).isSameAs(endpointMock); assertThat(result.getValue()).isSameAs(defaultPath); }
@Override public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); RequestInfo request = handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest)msg, state ); // If the Netty HttpRequest is invalid, we shouldn't do any endpoint routing. handlerUtils.throwExceptionIfNotSuccessfullyDecoded((HttpRequest) msg); // The HttpRequest is valid, so continue with the endpoint routing. Pair<Endpoint<?>, String> endpointForExecution = findSingleEndpointForExecution(request); request.setPathParamsBasedOnPathTemplate(endpointForExecution.getRight()); state.setEndpointForExecution(endpointForExecution.getLeft(), endpointForExecution.getRight()); throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize( (HttpRequest) msg, endpointForExecution.getLeft() ); } return PipelineContinuationBehavior.CONTINUE; }
@Override public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); RequestInfo request = handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest)msg, state ); // If the Netty HttpRequest is invalid, we shouldn't do any endpoint routing. handlerUtils.throwExceptionIfNotSuccessfullyDecoded((HttpRequest) msg); // The HttpRequest is valid, so continue with the endpoint routing. Pair<Endpoint<?>, String> endpointForExecution = findSingleEndpointForExecution(request); request.setPathParamsBasedOnPathTemplate(endpointForExecution.getRight()); state.setEndpointForExecution(endpointForExecution.getLeft(), endpointForExecution.getRight()); throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize( (HttpRequest) msg, endpointForExecution.getLeft() ); } return PipelineContinuationBehavior.CONTINUE; }