diff --git a/plugin-api/src/main/java/org/sonar/api/server/http/HttpRequest.java b/plugin-api/src/main/java/org/sonar/api/server/http/HttpRequest.java index 6cedbcab..30497300 100644 --- a/plugin-api/src/main/java/org/sonar/api/server/http/HttpRequest.java +++ b/plugin-api/src/main/java/org/sonar/api/server/http/HttpRequest.java @@ -28,7 +28,6 @@ */ public interface HttpRequest { - /** * Returns the port number to which the request was sent. */ diff --git a/plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java b/plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java index c5eac494..8bb02ec7 100644 --- a/plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java +++ b/plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java @@ -19,25 +19,12 @@ */ package org.sonar.api.web; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; -import java.util.function.Predicate; -import java.util.stream.Collectors; import org.sonar.api.ExtensionPoint; import org.sonar.api.server.ServerSide; -import static java.util.Arrays.asList; -import static java.util.Collections.unmodifiableList; -import static org.apache.commons.lang.StringUtils.substringBeforeLast; -import static org.sonar.api.utils.Preconditions.checkArgument; - /** * {@code @deprecated} since 9.16. Use {@link org.sonar.api.web.HttpFilter} instead. + * * @since 3.1 */ @ServerSide @@ -51,166 +38,4 @@ public abstract class ServletFilter implements javax.servlet.Filter { public UrlPattern doGetPattern() { return UrlPattern.builder().build(); } - - public static final class UrlPattern { - - private static final String MATCH_ALL = "/*"; - - private final List inclusions; - private final List exclusions; - private final Predicate[] inclusionPredicates; - private final Predicate[] exclusionPredicates; - - private UrlPattern(Builder builder) { - this.inclusions = unmodifiableList(new ArrayList<>(builder.inclusions)); - this.exclusions = unmodifiableList(new ArrayList<>(builder.exclusions)); - if (builder.inclusionPredicates.isEmpty()) { - // because Stream#anyMatch() returns false if stream is empty - this.inclusionPredicates = new Predicate[]{s -> true}; - } else { - this.inclusionPredicates = builder.inclusionPredicates.stream().toArray(Predicate[]::new); - } - this.exclusionPredicates = builder.exclusionPredicates.stream().toArray(Predicate[]::new); - } - - public boolean matches(String path) { - return !Arrays.stream(exclusionPredicates).anyMatch(pattern -> pattern.test(path)) && - Arrays.stream(inclusionPredicates).anyMatch(pattern -> pattern.test(path)); - } - - /** - * @since 6.0 - */ - public Collection getInclusions() { - return inclusions; - } - - /** - * @since 6.0 - */ - public Collection getExclusions() { - return exclusions; - } - - public String label() { - return "UrlPattern{" + - "inclusions=[" + convertPatternsToString(inclusions) + "]" + - ", exclusions=[" + convertPatternsToString(exclusions) + "]" + - '}'; - } - - private static String convertPatternsToString(List input) { - StringBuilder output = new StringBuilder(); - if (input.isEmpty()) { - return ""; - } - if (input.size() == 1) { - return output.append(input.get(0)).toString(); - } - return output.append(input.get(0)).append(", ...").toString(); - } - - /** - * Defines only a single inclusion pattern. This is a shortcut for {@code builder().includes(inclusionPattern).build()}. - */ - public static UrlPattern create(String inclusionPattern) { - return builder().includes(inclusionPattern).build(); - } - - /** - * @since 6.0 - */ - public static Builder builder() { - return new Builder(); - } - - /** - * @since 6.0 - */ - public static class Builder { - private static final String WILDCARD_CHAR = "*"; - private static final Collection STATIC_RESOURCES = unmodifiableList(asList( - "*.css", "*.css.map", "*.ico", "*.png", "*.jpg", "*.jpeg", "*.gif", "*.svg", "*.js", "*.js.map", "*.pdf", "/json/*", "*.woff2", - "/static/*", "/robots.txt", "/favicon.ico", "/apple-touch-icon*", "/mstile*")); - - private final Set inclusions = new LinkedHashSet<>(); - private final Set exclusions = new LinkedHashSet<>(); - private final Set> inclusionPredicates = new HashSet<>(); - private final Set> exclusionPredicates = new HashSet<>(); - - private Builder() { - } - - public static Collection staticResourcePatterns() { - return STATIC_RESOURCES; - } - - /** - * Add inclusion patterns. Supported formats are: - *
    - *
  • path prefixed by / and ended by * or /*, for example "/api/foo/*", to match all paths "/api/foo" and "api/api/foo/something/else"
  • - *
  • path prefixed by / and ended by .*, for example "/api/foo.*", to match exact path "/api/foo" with any suffix like "/api/foo.protobuf"
  • - *
  • path prefixed by *, for example "*\/foo", to match all paths "/api/foo" and "something/else/foo"
  • - *
  • path with leading slash and no wildcard, for example "/api/foo", to match exact path "/api/foo"
  • - *
- */ - public Builder includes(String... includePatterns) { - return includes(asList(includePatterns)); - } - - /** - * Add exclusion patterns. See format described in {@link #includes(String...)} - */ - public Builder includes(Collection includePatterns) { - this.inclusions.addAll(includePatterns); - this.inclusionPredicates.addAll(includePatterns.stream() - .filter(pattern -> !MATCH_ALL.equals(pattern)) - .map(Builder::compile) - .collect(Collectors.toList())); - return this; - } - - public Builder excludes(String... excludePatterns) { - return excludes(asList(excludePatterns)); - } - - public Builder excludes(Collection excludePatterns) { - this.exclusions.addAll(excludePatterns); - this.exclusionPredicates.addAll(excludePatterns.stream() - .map(Builder::compile) - .collect(Collectors.toList())); - return this; - } - - public UrlPattern build() { - return new UrlPattern(this); - } - - private static Predicate compile(String pattern) { - int countStars = pattern.length() - pattern.replace(WILDCARD_CHAR, "").length(); - if (countStars == 0) { - checkArgument(pattern.startsWith("/"), "URL pattern must start with slash '/': %s", pattern); - return url -> url.equals(pattern); - } - checkArgument(countStars == 1, "URL pattern accepts only zero or one wildcard character '*': %s", pattern); - if (pattern.charAt(0) == '/') { - checkArgument(pattern.endsWith(WILDCARD_CHAR), "URL pattern must end with wildcard character '*': %s", pattern); - if (pattern.endsWith("/*")) { - String path = pattern.substring(0, pattern.length() - "/*".length()); - return url -> url.startsWith(path); - } - if (pattern.endsWith(".*")) { - String path = pattern.substring(0, pattern.length() - ".*".length()); - return url -> substringBeforeLast(url, ".").equals(path); - } - String path = pattern.substring(0, pattern.length() - "*".length()); - return url -> url.startsWith(path); - } - checkArgument(pattern.startsWith(WILDCARD_CHAR), "URL pattern must start with wildcard character '*': %s", pattern); - // remove the leading * - String path = pattern.substring(1); - return url -> url.endsWith(path); - } - } - } } diff --git a/plugin-api/src/test/java/org/sonar/api/server/http/JakartaHttpRequestTest.java b/plugin-api/src/test/java/org/sonar/api/server/http/JakartaHttpRequestTest.java new file mode 100644 index 00000000..1b7339e7 --- /dev/null +++ b/plugin-api/src/test/java/org/sonar/api/server/http/JakartaHttpRequestTest.java @@ -0,0 +1,67 @@ +/* + * Sonar Plugin API + * Copyright (C) 2009-2023 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.api.server.http; + +import jakarta.servlet.http.HttpServletRequest; +import java.util.Enumeration; +import org.junit.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class JakartaHttpRequestTest { + + @Test + public void initRequest() { + HttpServletRequest requestMock = mock(HttpServletRequest.class); + when(requestMock.getServerPort()).thenReturn(80); + when(requestMock.isSecure()).thenReturn(true); + when(requestMock.getScheme()).thenReturn("https"); + when(requestMock.getServerName()).thenReturn("hostname"); + when(requestMock.getRequestURL()).thenReturn(new StringBuffer("https://hostname:80/path")); + when(requestMock.getRequestURI()).thenReturn("/path"); + when(requestMock.getQueryString()).thenReturn("param1=value1"); + when(requestMock.getContextPath()).thenReturn("/path"); + when(requestMock.getMethod()).thenReturn("POST"); + when(requestMock.getParameter("param1")).thenReturn("value1"); + when(requestMock.getParameterValues("param1")).thenReturn(new String[]{"value1"}); + when(requestMock.getHeader("header1")).thenReturn("hvalue1"); + Enumeration headers = mock(Enumeration.class); + when(requestMock.getHeaders("header1")).thenReturn(headers); + + JakartaHttpRequest request = new JakartaHttpRequest(requestMock); + + assertThat(request.getRawRequest()).isSameAs(requestMock); + assertThat(request.getServerPort()).isEqualTo(80); + assertThat(request.isSecure()).isTrue(); + assertThat(request.getScheme()).isEqualTo("https"); + assertThat(request.getServerName()).isEqualTo("hostname"); + assertThat(request.getRequestURL()).isEqualTo("https://hostname:80/path"); + assertThat(request.getRequestURI()).isEqualTo("/path"); + assertThat(request.getQueryString()).isEqualTo("param1=value1"); + assertThat(request.getContextPath()).isEqualTo("/path"); + assertThat(request.getMethod()).isEqualTo("POST"); + assertThat(request.getParameter("param1")).isEqualTo("value1"); + assertThat(request.getParameterValues("param1")).containsExactly("value1"); + assertThat(request.getHeader("header1")).isEqualTo("hvalue1"); + assertThat(request.getHeaders("header1")).isEqualTo(headers); + } +} diff --git a/plugin-api/src/test/java/org/sonar/api/server/http/JakartaHttpResponseTest.java b/plugin-api/src/test/java/org/sonar/api/server/http/JakartaHttpResponseTest.java new file mode 100644 index 00000000..df0755d4 --- /dev/null +++ b/plugin-api/src/test/java/org/sonar/api/server/http/JakartaHttpResponseTest.java @@ -0,0 +1,61 @@ +/* + * Sonar Plugin API + * Copyright (C) 2009-2023 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.api.server.http; + +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; +import org.junit.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class JakartaHttpResponseTest { + + @Test + public void initResponse() throws IOException { + HttpServletResponse responseMock = mock(HttpServletResponse.class); + when(responseMock.getHeader("h1")).thenReturn("hvalue1"); + when(responseMock.getHeaders("h1")).thenReturn(List.of("hvalue1")); + when(responseMock.getStatus()).thenReturn(200); + PrintWriter writer = mock(PrintWriter.class); + when(responseMock.getWriter()).thenReturn(writer); + + JakartaHttpResponse response = new JakartaHttpResponse(responseMock); + + assertThat(response.getRawResponse()).isSameAs(responseMock); + assertThat(response.getHeader("h1")).isEqualTo("hvalue1"); + assertThat(response.getHeaders("h1")).asList().containsExactly("hvalue1"); + assertThat(response.getStatus()).isEqualTo(200); + assertThat(response.getWriter()).isEqualTo(writer); + + response.addHeader("h2", "hvalue2"); + response.setStatus(201); + response.setContentType("text/plain"); + response.sendRedirect("http://redirect"); + verify(responseMock).addHeader("h2", "hvalue2"); + verify(responseMock).setStatus(201); + verify(responseMock).setContentType("text/plain"); + verify(responseMock).sendRedirect("http://redirect"); + } +} diff --git a/plugin-api/src/test/java/org/sonar/api/server/http/JavaxHttpRequestTest.java b/plugin-api/src/test/java/org/sonar/api/server/http/JavaxHttpRequestTest.java new file mode 100644 index 00000000..15881771 --- /dev/null +++ b/plugin-api/src/test/java/org/sonar/api/server/http/JavaxHttpRequestTest.java @@ -0,0 +1,67 @@ +/* + * Sonar Plugin API + * Copyright (C) 2009-2023 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.api.server.http; + +import java.util.Enumeration; +import javax.servlet.http.HttpServletRequest; +import org.junit.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class JavaxHttpRequestTest { + + @Test + public void initRequest() { + HttpServletRequest requestMock = mock(HttpServletRequest.class); + when(requestMock.getServerPort()).thenReturn(80); + when(requestMock.isSecure()).thenReturn(true); + when(requestMock.getScheme()).thenReturn("https"); + when(requestMock.getServerName()).thenReturn("hostname"); + when(requestMock.getRequestURL()).thenReturn(new StringBuffer("https://hostname:80/path")); + when(requestMock.getRequestURI()).thenReturn("/path"); + when(requestMock.getQueryString()).thenReturn("param1=value1"); + when(requestMock.getContextPath()).thenReturn("/path"); + when(requestMock.getMethod()).thenReturn("POST"); + when(requestMock.getParameter("param1")).thenReturn("value1"); + when(requestMock.getParameterValues("param1")).thenReturn(new String[]{"value1"}); + when(requestMock.getHeader("header1")).thenReturn("hvalue1"); + Enumeration headers = mock(Enumeration.class); + when(requestMock.getHeaders("header1")).thenReturn(headers); + + JavaxHttpRequest request = new JavaxHttpRequest(requestMock); + + assertThat(request.getRawRequest()).isSameAs(requestMock); + assertThat(request.getServerPort()).isEqualTo(80); + assertThat(request.isSecure()).isTrue(); + assertThat(request.getScheme()).isEqualTo("https"); + assertThat(request.getServerName()).isEqualTo("hostname"); + assertThat(request.getRequestURL()).isEqualTo("https://hostname:80/path"); + assertThat(request.getRequestURI()).isEqualTo("/path"); + assertThat(request.getQueryString()).isEqualTo("param1=value1"); + assertThat(request.getContextPath()).isEqualTo("/path"); + assertThat(request.getMethod()).isEqualTo("POST"); + assertThat(request.getParameter("param1")).isEqualTo("value1"); + assertThat(request.getParameterValues("param1")).containsExactly("value1"); + assertThat(request.getHeader("header1")).isEqualTo("hvalue1"); + assertThat(request.getHeaders("header1")).isEqualTo(headers); + } +} diff --git a/plugin-api/src/test/java/org/sonar/api/server/http/JavaxHttpResponseTest.java b/plugin-api/src/test/java/org/sonar/api/server/http/JavaxHttpResponseTest.java new file mode 100644 index 00000000..0a665774 --- /dev/null +++ b/plugin-api/src/test/java/org/sonar/api/server/http/JavaxHttpResponseTest.java @@ -0,0 +1,61 @@ +/* + * Sonar Plugin API + * Copyright (C) 2009-2023 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.api.server.http; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; +import javax.servlet.http.HttpServletResponse; +import org.junit.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class JavaxHttpResponseTest { + + @Test + public void initResponse() throws IOException { + HttpServletResponse responseMock = mock(HttpServletResponse.class); + when(responseMock.getHeader("h1")).thenReturn("hvalue1"); + when(responseMock.getHeaders("h1")).thenReturn(List.of("hvalue1")); + when(responseMock.getStatus()).thenReturn(200); + PrintWriter writer = mock(PrintWriter.class); + when(responseMock.getWriter()).thenReturn(writer); + + JavaxHttpResponse response = new JavaxHttpResponse(responseMock); + + assertThat(response.getRawResponse()).isSameAs(responseMock); + assertThat(response.getHeader("h1")).isEqualTo("hvalue1"); + assertThat(response.getHeaders("h1")).asList().containsExactly("hvalue1"); + assertThat(response.getStatus()).isEqualTo(200); + assertThat(response.getWriter()).isEqualTo(writer); + + response.addHeader("h2", "hvalue2"); + response.setStatus(201); + response.setContentType("text/plain"); + response.sendRedirect("http://redirect"); + verify(responseMock).addHeader("h2", "hvalue2"); + verify(responseMock).setStatus(201); + verify(responseMock).setContentType("text/plain"); + verify(responseMock).sendRedirect("http://redirect"); + } +} diff --git a/plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java b/plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java index 10d88dd0..25740e5f 100644 --- a/plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java +++ b/plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java @@ -26,170 +26,9 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; public class ServletFilterTest { - @Test - public void include_all() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.create("/*"); - assertThat(pattern.matches("/")).isTrue(); - assertThat(pattern.matches("/foo/ooo")).isTrue(); - - assertThat(pattern.getInclusions()).containsOnly("/*"); - assertThat(pattern.getExclusions()).isEmpty(); - } - - @Test - public void include_end_of_url() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.create("*foo"); - assertThat(pattern.matches("/")).isFalse(); - assertThat(pattern.matches("/hello/foo")).isTrue(); - assertThat(pattern.matches("/hello/bar")).isFalse(); - assertThat(pattern.matches("/foo")).isTrue(); - assertThat(pattern.matches("/foo2")).isFalse(); - } - - @Test - public void include_beginning_of_url() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.create("/foo/*"); - assertThat(pattern.matches("/")).isFalse(); - assertThat(pattern.matches("/foo")).isTrue(); - assertThat(pattern.matches("/foo/bar")).isTrue(); - assertThat(pattern.matches("/bar")).isFalse(); - } - - @Test - public void include_exact_url() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.create("/foo"); - assertThat(pattern.matches("/")).isFalse(); - assertThat(pattern.matches("/foo")).isTrue(); - assertThat(pattern.matches("/foo/")).isFalse(); - assertThat(pattern.matches("/bar")).isFalse(); - } - - @Test - public void exclude_all() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .excludes("/*") - .build(); - assertThat(pattern.matches("/")).isFalse(); - assertThat(pattern.matches("/foo/ooo")).isFalse(); - } - - @Test - public void exclude_end_of_url() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .excludes("*foo") - .build(); - - assertThat(pattern.matches("/")).isTrue(); - assertThat(pattern.matches("/hello/foo")).isFalse(); - assertThat(pattern.matches("/hello/bar")).isTrue(); - assertThat(pattern.matches("/foo")).isFalse(); - assertThat(pattern.matches("/foo2")).isTrue(); - } - - @Test - public void exclude_beginning_of_url() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .excludes("/foo/*") - .build(); - - assertThat(pattern.matches("/")).isTrue(); - assertThat(pattern.matches("/foo")).isFalse(); - assertThat(pattern.matches("/foo/bar")).isFalse(); - assertThat(pattern.matches("/bar")).isTrue(); - } - - @Test - public void exclude_exact_url() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .excludes("/foo") - .build(); - - assertThat(pattern.matches("/")).isTrue(); - assertThat(pattern.matches("/foo")).isFalse(); - assertThat(pattern.matches("/foo/")).isTrue(); - assertThat(pattern.matches("/bar")).isTrue(); - } - - @Test - public void use_multiple_include_patterns() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .includes("/foo", "/foo2") - .build(); - assertThat(pattern.matches("/")).isFalse(); - assertThat(pattern.matches("/foo")).isTrue(); - assertThat(pattern.matches("/foo2")).isTrue(); - assertThat(pattern.matches("/foo/")).isFalse(); - assertThat(pattern.matches("/bar")).isFalse(); - } - - @Test - public void use_multiple_exclude_patterns() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .excludes("/foo", "/foo2") - .build(); - assertThat(pattern.matches("/")).isTrue(); - assertThat(pattern.matches("/foo")).isFalse(); - assertThat(pattern.matches("/foo2")).isFalse(); - assertThat(pattern.matches("/foo/")).isTrue(); - assertThat(pattern.matches("/bar")).isTrue(); - } - - @Test - public void use_include_and_exclude_patterns() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .includes("/foo/*", "/foo/lo*") - .excludes("/foo/login", "/foo/logout", "/foo/list") - .build(); - assertThat(pattern.matches("/")).isFalse(); - assertThat(pattern.matches("/foo")).isTrue(); - assertThat(pattern.matches("/foo/login")).isFalse(); - assertThat(pattern.matches("/foo/logout")).isFalse(); - assertThat(pattern.matches("/foo/list")).isFalse(); - assertThat(pattern.matches("/foo/locale")).isTrue(); - assertThat(pattern.matches("/foo/index")).isTrue(); - } - - @Test - public void use_include_and_exclude_prefix() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .includes("/foo_2") - .excludes("/foo") - .build(); - assertThat(pattern.matches("/")).isFalse(); - assertThat(pattern.matches("/foo_2")).isTrue(); - assertThat(pattern.matches("/foo")).isFalse(); - } - - @Test - public void exclude_pattern_has_higher_priority_than_include_pattern() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .includes("/foo") - .excludes("/foo") - .build(); - assertThat(pattern.matches("/foo")).isFalse(); - } - - @Test - public void accept_empty_patterns() { - ServletFilter.UrlPattern pattern = ServletFilter.UrlPattern.builder() - .excludes() - .includes() - .build(); - assertThat(pattern.matches("/")).isTrue(); - assertThat(pattern.matches("/foo/bar")).isTrue(); - } - - @Test - public void create_throws_IAE_if_empty_url() { - assertThatThrownBy(() -> ServletFilter.UrlPattern.create("")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("URL pattern must start with slash '/': "); - } - @Test public void filter_should_return_url_pattern() { ServletFilter filter = new FakeFilter(); @@ -203,48 +42,6 @@ public void filter_should_apply_to_all_urls_by_default() { assertThat(filter.doGetPattern().matches("/foo/bar")).isTrue(); } - @Test - public void getUrl_returns_single_inclusion() { - assertThat(ServletFilter.UrlPattern.create("/*").getInclusions()).containsOnly("/*"); - assertThat(ServletFilter.UrlPattern.create("/foo/bar").getInclusions()).containsOnly("/foo/bar"); - } - - @Test - public void test_staticResourcePatterns() { - assertThat(ServletFilter.UrlPattern.Builder.staticResourcePatterns()).containsOnly( - "*.css", - "*.css.map", - "*.ico", - "*.png", - "*.jpg", - "*.jpeg", - "*.gif", - "*.svg", - "*.js", - "*.js.map", - "*.pdf", - "*.woff2", - "/json/*", - "/static/*", - "/robots.txt", - "/favicon.ico", - "/apple-touch-icon*", - "/mstile*"); - } - - @Test - public void test_label() { - assertThat(ServletFilter.UrlPattern.builder().build().label()).isEqualTo("UrlPattern{inclusions=[], exclusions=[]}"); - assertThat(ServletFilter.UrlPattern.builder() - .includes("/foo/*") - .excludes("/foo/login") - .build().label()).isEqualTo("UrlPattern{inclusions=[/foo/*], exclusions=[/foo/login]}"); - assertThat(ServletFilter.UrlPattern.builder() - .includes("/foo/*", "/foo/lo*") - .excludes("/foo/login", "/foo/logout", "/foo/list") - .build().label()).isEqualTo("UrlPattern{inclusions=[/foo/*, ...], exclusions=[/foo/login, ...]}"); - } - private static class FakeFilter extends ServletFilter { @Override public UrlPattern doGetPattern() {