@Test(expected = IllegalArgumentException.class) public void constructor_throws_IllegalArgumentException_if_arg_is_null() { // expect new RoutingHandler(null, maxRequestSizeInBytes); }
@Test(expected = IllegalArgumentException.class) public void constructor_throws_IllegalArgumentException_if_arg_is_empty() { // expect new RoutingHandler(Collections.emptyList(), maxRequestSizeInBytes); }
@Test public void doChannelRead_HttpRequest_under_max_global_request_size_processed_successfully() { // given doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride(); maxRequestSizeInBytes = 101; httpHeaders.set(CONTENT_LENGTH, 100); handlerSpy = spy(new RoutingHandler(endpoints, maxRequestSizeInBytes)); // when PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg); // then assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); }
@Test public void doChannelRead_HttpRequest_throws_exception_when_content_length_header_greater_than_configured_global_request_limit() { // given doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride(); maxRequestSizeInBytes = 10; httpHeaders.set(CONTENT_LENGTH, 100); handlerSpy = spy(new RoutingHandler(endpoints, maxRequestSizeInBytes)); // when Throwable thrownException = Assertions.catchThrowable(() -> handlerSpy.doChannelRead(ctxMock, msg)); // then assertThat(thrownException).isExactlyInstanceOf(RequestTooBigException.class); assertThat(thrownException.getMessage()).isEqualTo("Content-Length header value exceeded configured max request size of 10"); }
@Before public void beforeMethod() { stateMock = mock(HttpProcessingState.class); ctxMock = mock(ChannelHandlerContext.class); channelMock = mock(Channel.class); stateAttrMock = mock(Attribute.class); requestInfoMock = mock(RequestInfo.class); endpointMock = mock(StandardEndpoint.class); matcherMock = mock(Matcher.class); endpoints = new ArrayList<>(Collections.singleton(endpointMock)); httpHeaders = new DefaultHttpHeaders(); maxRequestSizeInBytes = 10; msg = mock(HttpRequest.class); doReturn(channelMock).when(ctxMock).channel(); doReturn(stateAttrMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY); doReturn(stateMock).when(stateAttrMock).get(); doReturn(endpointMock).when(stateMock).getEndpointForExecution(); doReturn(matcherMock).when(endpointMock).requestMatcher(); doReturn(Optional.of(defaultPath)).when(matcherMock).matchesPath(any(RequestInfo.class)); doReturn(true).when(matcherMock).matchesMethod(any(RequestInfo.class)); doReturn(requestInfoMock).when(stateMock).getRequestInfo(); doReturn(httpHeaders).when(msg).headers(); handlerSpy = spy(new RoutingHandler(endpoints, maxRequestSizeInBytes)); }
@Test public void constructor_sets_fields_based_on_incoming_args() { // when RoutingHandler theHandler = new RoutingHandler(endpoints, maxRequestSizeInBytes); // then Collection<Endpoint<?>> actualEndpoints = (Collection<Endpoint<?>>) Whitebox.getInternalState(theHandler, "endpoints"); assertThat(actualEndpoints).isSameAs(endpoints); }
@DataProvider(value = { "99", "100" }) @Test public void doChannelRead_HttpRequest_does_not_throw_TooLongFrameException_if_content_length_is_less_than_endpoint_overridden_value( int maxRequestSize ) { // given doReturn(100).when(endpointMock).maxRequestSizeInBytesOverride(); httpHeaders.set(CONTENT_LENGTH, 100); handlerSpy = spy(new RoutingHandler(endpoints, maxRequestSize)); // when PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg); // then assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); }
@DataProvider(value = { "99", "101" }) @Test public void doChannelRead_HttpRequest_endpoint_overridden_max_request_size_throws_exception(int maxRequestSize) { // given doReturn(100).when(endpointMock).maxRequestSizeInBytesOverride(); httpHeaders.set(CONTENT_LENGTH, 101); handlerSpy = spy(new RoutingHandler(endpoints, maxRequestSize)); // when Throwable thrownException = Assertions.catchThrowable(() -> handlerSpy.doChannelRead(ctxMock, msg)); // then assertThat(thrownException).isExactlyInstanceOf(RequestTooBigException.class); assertThat(thrownException.getMessage()).isEqualTo("Content-Length header value exceeded configured max request size of 100"); }
@DataProvider(value = { "true", "false" }) @Test public void doChannelRead_HttpRequest_does_not_throw_TooLongFrameException_if_content_length_header_is_missing( boolean isChunkedTransferEncoding ) { // given if (isChunkedTransferEncoding) { httpHeaders.set(TRANSFER_ENCODING, CHUNKED); } doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride(); maxRequestSizeInBytes = 101; handlerSpy = spy(new RoutingHandler(endpoints, maxRequestSizeInBytes)); // when PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg); // then assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); }
p.addLast(ROUTING_HANDLER_NAME, new RoutingHandler(endpoints, maxRequestSizeInBytes));
p.addLast(ROUTING_HANDLER_NAME, new RoutingHandler(endpoints, maxRequestSizeInBytes));