You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RootFilterTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform.web;
  21. import java.util.List;
  22. import javax.servlet.FilterChain;
  23. import javax.servlet.FilterConfig;
  24. import javax.servlet.ServletContext;
  25. import javax.servlet.ServletRequest;
  26. import javax.servlet.ServletResponse;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import org.junit.Before;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.ExpectedException;
  33. import org.mockito.ArgumentCaptor;
  34. import org.sonar.api.utils.log.LogTester;
  35. import org.sonar.api.utils.log.LoggerLevel;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.mockito.ArgumentMatchers.any;
  38. import static org.mockito.Mockito.doThrow;
  39. import static org.mockito.Mockito.mock;
  40. import static org.mockito.Mockito.never;
  41. import static org.mockito.Mockito.verify;
  42. import static org.mockito.Mockito.when;
  43. public class RootFilterTest {
  44. @Rule
  45. public ExpectedException expectedException = ExpectedException.none();
  46. @Rule
  47. public LogTester logTester = new LogTester();
  48. private FilterChain chain = mock(FilterChain.class);
  49. private RootFilter underTest;
  50. @Before
  51. public void initialize() {
  52. FilterConfig filterConfig = mock(FilterConfig.class);
  53. ServletContext context = mock(ServletContext.class);
  54. when(context.getContextPath()).thenReturn("/context");
  55. when(filterConfig.getServletContext()).thenReturn(context);
  56. underTest = new RootFilter();
  57. underTest.init(filterConfig);
  58. }
  59. @Test
  60. public void throwable_in_doFilter_is_caught_and_500_error_returned_if_response_is_not_committed() throws Exception {
  61. doThrow(new RuntimeException()).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
  62. HttpServletResponse response = mockHttpResponse(false);
  63. underTest.doFilter(request("POST", "/context/service/call", "param=value"), response, chain);
  64. verify(response).sendError(500);
  65. }
  66. @Test
  67. public void throwable_in_doFilter_is_caught_but_no_500_response_is_sent_if_response_already_committed() throws Exception {
  68. doThrow(new RuntimeException()).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
  69. HttpServletResponse response = mockHttpResponse(true);
  70. underTest.doFilter(request("POST", "/context/service/call", "param=value"), response, chain);
  71. verify(response, never()).sendError(500);
  72. }
  73. @Test
  74. public void throwable_in_doFilter_is_logged_in_debug_if_response_is_already_committed() throws Exception {
  75. doThrow(new RuntimeException()).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
  76. HttpServletResponse response = mockHttpResponse(true);
  77. underTest.doFilter(request("POST", "/context/service/call", "param=value"), response, chain);
  78. List<String> debugLogs = logTester.logs(LoggerLevel.DEBUG);
  79. assertThat(debugLogs.size()).isEqualTo(1);
  80. assertThat(debugLogs.get(0)).contains("Processing of request", "failed");
  81. }
  82. @Test
  83. public void request_used_in_chain_do_filter_is_a_servlet_wrapper_when_static_resource() throws Exception {
  84. underTest.doFilter(request("GET", "/context/static/image.png", null), mock(HttpServletResponse.class), chain);
  85. ArgumentCaptor<ServletRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ServletRequest.class);
  86. verify(chain).doFilter(requestArgumentCaptor.capture(), any(HttpServletResponse.class));
  87. assertThat(requestArgumentCaptor.getValue()).isInstanceOf(RootFilter.ServletRequestWrapper.class);
  88. }
  89. @Test
  90. public void request_used_in_chain_do_filter_is_a_servlet_wrapper_when_service_call() throws Exception {
  91. underTest.doFilter(request("POST", "/context/service/call", "param=value"), mock(HttpServletResponse.class), chain);
  92. ArgumentCaptor<ServletRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ServletRequest.class);
  93. verify(chain).doFilter(requestArgumentCaptor.capture(), any(HttpServletResponse.class));
  94. assertThat(requestArgumentCaptor.getValue()).isInstanceOf(RootFilter.ServletRequestWrapper.class);
  95. }
  96. @Test
  97. public void fail_to_get_session_from_request() throws Exception {
  98. underTest.doFilter(request("GET", "/context/static/image.png", null), mock(HttpServletResponse.class), chain);
  99. ArgumentCaptor<ServletRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ServletRequest.class);
  100. verify(chain).doFilter(requestArgumentCaptor.capture(), any(ServletResponse.class));
  101. expectedException.expect(UnsupportedOperationException.class);
  102. ((HttpServletRequest) requestArgumentCaptor.getValue()).getSession();
  103. }
  104. @Test
  105. public void fail_to_get_session_with_create_from_request() throws Exception {
  106. underTest.doFilter(request("GET", "/context/static/image.png", null), mock(HttpServletResponse.class), chain);
  107. ArgumentCaptor<ServletRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ServletRequest.class);
  108. verify(chain).doFilter(requestArgumentCaptor.capture(), any(ServletResponse.class));
  109. expectedException.expect(UnsupportedOperationException.class);
  110. ((HttpServletRequest) requestArgumentCaptor.getValue()).getSession(true);
  111. }
  112. private HttpServletRequest request(String method, String path, String query) {
  113. HttpServletRequest request = mock(HttpServletRequest.class);
  114. when(request.getMethod()).thenReturn(method);
  115. when(request.getRequestURI()).thenReturn(path);
  116. when(request.getQueryString()).thenReturn(query);
  117. return request;
  118. }
  119. private static HttpServletResponse mockHttpResponse(boolean committed) {
  120. HttpServletResponse response = mock(HttpServletResponse.class);
  121. when(response.isCommitted()).thenReturn(committed);
  122. return response;
  123. }
  124. }