@Before public void beforeMethod() { stateMock = mock(HttpProcessingState.class); ctxMock = mock(ChannelHandlerContext.class); channelMock = mock(Channel.class); stateAttrMock = mock(Attribute.class); endpointMock = mock(Endpoint.class); maxRequestSizeInBytes = 10; httpContentMock = mock(HttpContent.class); byteBufMock = mock(ByteBuf.class); requestInfo = mock(RequestInfo.class); handler = new RequestInfoSetterHandler(maxRequestSizeInBytes); 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(byteBufMock).when(httpContentMock).content(); doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride(); doReturn(requestInfo).when(stateMock).getRequestInfo(); }
@Test public void doChannelRead_does_not_throw_exception_when_exceeding_global_max_size_when_request_validation_is_turned_off() { // given maxRequestSizeInBytes = 10; handler = new RequestInfoSetterHandler(maxRequestSizeInBytes); doReturn(0).when(endpointMock).maxRequestSizeInBytesOverride(); doReturn(100).when(requestInfo).addContentChunk(anyObject()); // when PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, httpContentMock); // then assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); verify(httpContentMock).release(); }
@Test public void doChannelRead_allows_endpoints_to_override_max_request_size_setting_higher() { // given maxRequestSizeInBytes = 10; handler = new RequestInfoSetterHandler(maxRequestSizeInBytes); doReturn(100).when(endpointMock).maxRequestSizeInBytesOverride(); doReturn(99).when(requestInfo).addContentChunk(anyObject()); // when PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, httpContentMock); // then assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); verify(httpContentMock).release(); }
p.addLast(REQUEST_INFO_SETTER_HANDLER_NAME, new RequestInfoSetterHandler(maxRequestSizeInBytes));
p.addLast(REQUEST_INFO_SETTER_HANDLER_NAME, new RequestInfoSetterHandler(maxRequestSizeInBytes));
@Test public void doChannelRead_allows_endpoints_to_override_max_request_size_setting_lower() { // given maxRequestSizeInBytes = 10; handler = new RequestInfoSetterHandler(maxRequestSizeInBytes); doReturn(1).when(endpointMock).maxRequestSizeInBytesOverride(); doReturn(2).when(requestInfo).addContentChunk(anyObject()); // when Throwable thrownException = Assertions.catchThrowable(() -> handler.doChannelRead(ctxMock, httpContentMock)); // then assertThat(thrownException).isExactlyInstanceOf(RequestTooBigException.class); verify(httpContentMock).release(); }
@DataProvider(value = { "true | false", "false | true" }, splitBy = "\\|") @Test public void doChannelRead_override_uses_global_configured_max_request_size_when_necessary_no_error( boolean endpointIsNull, boolean endpointReturnsNullOverride ) { // given if (endpointIsNull) doReturn(null).when(stateMock).getEndpointForExecution(); if (endpointReturnsNullOverride) doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride(); maxRequestSizeInBytes = 10; handler = new RequestInfoSetterHandler(maxRequestSizeInBytes); doReturn(5).when(requestInfo).addContentChunk(anyObject()); // when PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, httpContentMock); // then assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); verify(httpContentMock).release(); }
@DataProvider(value = { "true | false", "false | true" }, splitBy = "\\|") @Test public void doChannelRead_no_endpoint_override_uses_global_configured_max_request_size_when_necessary_with_error( boolean endpointIsNull, boolean endpointReturnsNullOverride ) { // given // given if (endpointIsNull) doReturn(null).when(stateMock).getEndpointForExecution(); if (endpointReturnsNullOverride) doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride(); maxRequestSizeInBytes = 10; handler = new RequestInfoSetterHandler(maxRequestSizeInBytes); doReturn(11).when(requestInfo).addContentChunk(anyObject()); // when Throwable thrownException = Assertions.catchThrowable(() -> handler.doChannelRead(ctxMock, httpContentMock)); // then assertThat(thrownException).isExactlyInstanceOf(RequestTooBigException.class); verify(httpContentMock).release(); }