diff options
author | Aurelien Poscia <aurelien.poscia@sonarsource.com> | 2022-09-16 15:20:09 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-09-19 20:03:08 +0000 |
commit | 1f9bc827e81575d6515061cace65526ca3edf18b (patch) | |
tree | f16fd9d7ffb341854f679ecbcc7860183f9425b8 /server/sonar-webserver | |
parent | 1e1e26d5dc136036fc4cbfde759e4879e098f519 (diff) | |
download | sonarqube-1f9bc827e81575d6515061cace65526ca3edf18b.tar.gz sonarqube-1f9bc827e81575d6515061cace65526ca3edf18b.zip |
SONAR-17271 Request body can be read multiple time & implement support of comments coming from GitHub webhooks
Diffstat (limited to 'server/sonar-webserver')
-rw-r--r-- | server/sonar-webserver/src/test/java/org/sonar/server/platform/web/RootFilterTest.java | 148 |
1 files changed, 0 insertions, 148 deletions
diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/RootFilterTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/RootFilterTest.java deleted file mode 100644 index 0d1d3a3cf78..00000000000 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/RootFilterTest.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.platform.web; - -import java.util.List; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class RootFilterTest { - - @Rule - public LogTester logTester = new LogTester(); - - private FilterChain chain = mock(FilterChain.class); - private RootFilter underTest; - - @Before - public void initialize() { - FilterConfig filterConfig = mock(FilterConfig.class); - ServletContext context = mock(ServletContext.class); - when(context.getContextPath()).thenReturn("/context"); - when(filterConfig.getServletContext()).thenReturn(context); - underTest = new RootFilter(); - underTest.init(filterConfig); - } - - @Test - public void throwable_in_doFilter_is_caught_and_500_error_returned_if_response_is_not_committed() throws Exception { - doThrow(new RuntimeException()).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); - HttpServletResponse response = mockHttpResponse(false); - underTest.doFilter(request("POST", "/context/service/call", "param=value"), response, chain); - - verify(response).sendError(500); - } - - @Test - public void throwable_in_doFilter_is_caught_but_no_500_response_is_sent_if_response_already_committed() throws Exception { - doThrow(new RuntimeException()).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); - HttpServletResponse response = mockHttpResponse(true); - underTest.doFilter(request("POST", "/context/service/call", "param=value"), response, chain); - - verify(response, never()).sendError(500); - } - - @Test - public void throwable_in_doFilter_is_logged_in_debug_if_response_is_already_committed() throws Exception { - doThrow(new RuntimeException()).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); - HttpServletResponse response = mockHttpResponse(true); - underTest.doFilter(request("POST", "/context/service/call", "param=value"), response, chain); - - List<String> debugLogs = logTester.logs(LoggerLevel.DEBUG); - assertThat(debugLogs.size()).isOne(); - assertThat(debugLogs.get(0)).contains("Processing of request", "failed"); - } - - - @Test - public void request_used_in_chain_do_filter_is_a_servlet_wrapper_when_static_resource() throws Exception { - underTest.doFilter(request("GET", "/context/static/image.png", null), mock(HttpServletResponse.class), chain); - ArgumentCaptor<ServletRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ServletRequest.class); - - verify(chain).doFilter(requestArgumentCaptor.capture(), any(HttpServletResponse.class)); - - assertThat(requestArgumentCaptor.getValue()).isInstanceOf(RootFilter.ServletRequestWrapper.class); - } - - @Test - public void request_used_in_chain_do_filter_is_a_servlet_wrapper_when_service_call() throws Exception { - underTest.doFilter(request("POST", "/context/service/call", "param=value"), mock(HttpServletResponse.class), chain); - ArgumentCaptor<ServletRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ServletRequest.class); - - verify(chain).doFilter(requestArgumentCaptor.capture(), any(HttpServletResponse.class)); - - assertThat(requestArgumentCaptor.getValue()).isInstanceOf(RootFilter.ServletRequestWrapper.class); - } - - @Test - public void fail_to_get_session_from_request() throws Exception { - underTest.doFilter(request("GET", "/context/static/image.png", null), mock(HttpServletResponse.class), chain); - ArgumentCaptor<ServletRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ServletRequest.class); - verify(chain).doFilter(requestArgumentCaptor.capture(), any(ServletResponse.class)); - - - assertThatThrownBy(() -> ((HttpServletRequest) requestArgumentCaptor.getValue()).getSession()) - .isInstanceOf(UnsupportedOperationException.class); - } - - @Test - public void fail_to_get_session_with_create_from_request() throws Exception { - underTest.doFilter(request("GET", "/context/static/image.png", null), mock(HttpServletResponse.class), chain); - ArgumentCaptor<ServletRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ServletRequest.class); - verify(chain).doFilter(requestArgumentCaptor.capture(), any(ServletResponse.class)); - - assertThatThrownBy(() -> ((HttpServletRequest) requestArgumentCaptor.getValue()).getSession(true)) - .isInstanceOf(UnsupportedOperationException.class); - } - - private HttpServletRequest request(String method, String path, String query) { - HttpServletRequest request = mock(HttpServletRequest.class); - when(request.getMethod()).thenReturn(method); - when(request.getRequestURI()).thenReturn(path); - when(request.getQueryString()).thenReturn(query); - return request; - } - - private static HttpServletResponse mockHttpResponse(boolean committed) { - HttpServletResponse response = mock(HttpServletResponse.class); - when(response.isCommitted()).thenReturn(committed); - return response; - } -} |