Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

WebPagesFilterTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 javax.servlet.FilterChain;
  22. import javax.servlet.ServletContext;
  23. import javax.servlet.ServletOutputStream;
  24. import javax.servlet.WriteListener;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27. import org.junit.Before;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.ExpectedException;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.mockito.Mockito.RETURNS_MOCKS;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.verify;
  35. import static org.mockito.Mockito.verifyZeroInteractions;
  36. import static org.mockito.Mockito.when;
  37. public class WebPagesFilterTest {
  38. private static final String TEST_CONTEXT = "/sonarqube";
  39. @Rule
  40. public ExpectedException expectedException = ExpectedException.none();
  41. private ServletContext servletContext = mock(ServletContext.class, RETURNS_MOCKS);
  42. private WebPagesCache webPagesCache = mock(WebPagesCache.class);
  43. private HttpServletRequest request = mock(HttpServletRequest.class);
  44. private HttpServletResponse response = mock(HttpServletResponse.class);
  45. private FilterChain chain = mock(FilterChain.class);
  46. private WebPagesFilter underTest = new WebPagesFilter(webPagesCache);
  47. @Before
  48. public void setUp() throws Exception {
  49. when(servletContext.getContextPath()).thenReturn(TEST_CONTEXT);
  50. }
  51. @Test
  52. public void return_web_page_content() throws Exception {
  53. String path = "/index.html";
  54. when(webPagesCache.getContent(path)).thenReturn("test");
  55. when(request.getRequestURI()).thenReturn(path);
  56. when(request.getContextPath()).thenReturn(TEST_CONTEXT);
  57. StringOutputStream outputStream = new StringOutputStream();
  58. when(response.getOutputStream()).thenReturn(outputStream);
  59. underTest.doFilter(request, response, chain);
  60. verify(response).setContentType("text/html");
  61. verify(response).setCharacterEncoding("utf-8");
  62. verify(response).setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  63. assertThat(outputStream.toString()).isEqualTo("test");
  64. }
  65. @Test
  66. public void does_nothing_when_static_resource() throws Exception{
  67. when(request.getRequestURI()).thenReturn("/static");
  68. when(request.getContextPath()).thenReturn(TEST_CONTEXT);
  69. underTest.doFilter(request, response, chain);
  70. verify(chain).doFilter(request, response);
  71. verifyZeroInteractions(webPagesCache);
  72. }
  73. class StringOutputStream extends ServletOutputStream {
  74. private final StringBuilder buf = new StringBuilder();
  75. StringOutputStream() {
  76. }
  77. @Override
  78. public boolean isReady() {
  79. return false;
  80. }
  81. @Override
  82. public void setWriteListener(WriteListener listener) {
  83. }
  84. public void write(byte[] b) {
  85. this.buf.append(new String(b));
  86. }
  87. public void write(byte[] b, int off, int len) {
  88. this.buf.append(new String(b, off, len));
  89. }
  90. public void write(int b) {
  91. byte[] bytes = new byte[] {(byte) b};
  92. this.buf.append(new String(bytes));
  93. }
  94. public String toString() {
  95. return this.buf.toString();
  96. }
  97. }
  98. }