Skip to content

Commit 34f7d1a

Browse files
Fix unit test
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 396abbe commit 34f7d1a

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -182,8 +182,10 @@ void cloudFoundryPathsIgnoredBySpringSecurity() {
182182
testCloudFoundrySecurity(request, BASE_PATH + "/test", chain);
183183
testCloudFoundrySecurity(request, BASE_PATH + "/test/a", chain);
184184
request.setServletPath(BASE_PATH + "/other-path");
185+
request.setRequestURI(BASE_PATH + "/other-path");
185186
assertThat(chain.matches(request)).isFalse();
186187
request.setServletPath("/some-other-path");
188+
request.setRequestURI("/some-other-path");
187189
assertThat(chain.matches(request)).isFalse();
188190
});
189191
}
@@ -211,7 +213,7 @@ private FilterChainProxy getFilterChainProxy(Filter filter) {
211213

212214
private static void testCloudFoundrySecurity(MockHttpServletRequest request, String servletPath,
213215
SecurityFilterChain chain) {
214-
request.setServletPath(servletPath);
216+
request.setRequestURI(servletPath);
215217
assertThat(chain.matches(request)).isTrue();
216218
}
217219

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ private MockHttpServletRequest mockRequest(HttpMethod httpMethod, String servlet
430430
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
431431
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
432432
if (servletPath != null) {
433-
request.setServletPath(servletPath);
433+
request.setRequestURI(servletPath);
434434
}
435435
if (httpMethod != null) {
436436
request.setMethod(httpMethod.name());

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/ManagementWebSecurityAutoConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private HttpStatus getResponseStatus(AssertableWebApplicationContext context, St
206206
MockHttpServletResponse response = new MockHttpServletResponse();
207207
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
208208
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
209-
request.setServletPath(path);
209+
request.setRequestURI(path);
210210
request.setMethod("GET");
211211
filterChainProxy.doFilter(request, response, new MockFilterChain());
212212
return HttpStatus.valueOf(response.getStatus());

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfigurationTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.context.annotation.Bean;
2828
import org.springframework.context.annotation.Configuration;
2929
import org.springframework.security.web.util.matcher.RequestMatcher;
30+
import org.springframework.web.util.pattern.PathPatternParser;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
3233

@@ -60,7 +61,8 @@ void registersRequestMatcherProviderIfMvcPresent() {
6061
this.contextRunner.withUserConfiguration(TestMvcConfiguration.class).run((context) -> {
6162
AntPathRequestMatcherProvider matcherProvider = context.getBean(AntPathRequestMatcherProvider.class);
6263
RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null);
63-
assertThat(requestMatcher).extracting("pattern").isEqualTo("/custom/example");
64+
assertThat(requestMatcher).extracting("pattern")
65+
.isEqualTo(PathPatternParser.defaultInstance.parse("/custom/example"));
6466
});
6567
}
6668

@@ -71,7 +73,8 @@ void registersRequestMatcherForJerseyProviderIfJerseyPresentAndMvcAbsent() {
7173
.run((context) -> {
7274
AntPathRequestMatcherProvider matcherProvider = context.getBean(AntPathRequestMatcherProvider.class);
7375
RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null);
74-
assertThat(requestMatcher).extracting("pattern").isEqualTo("/admin/example");
76+
assertThat(requestMatcher).extracting("pattern")
77+
.isEqualTo(PathPatternParser.defaultInstance.parse("/admin/example"));
7578
});
7679
}
7780

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/PathRequestTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.mock.web.MockHttpServletRequest;
2626
import org.springframework.mock.web.MockServletContext;
2727
import org.springframework.security.web.util.matcher.RequestMatcher;
28+
import org.springframework.util.StringUtils;
2829
import org.springframework.web.context.WebApplicationContext;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
@@ -99,14 +100,14 @@ private MockHttpServletRequest mockRequest(String path) {
99100
MockServletContext servletContext = new MockServletContext();
100101
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
101102
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
102-
request.setPathInfo(path);
103+
request.setRequestURI(path);
103104
return request;
104105
}
105106

106107
private String getRequestPath(HttpServletRequest request) {
107108
String url = request.getServletPath();
108-
if (request.getPathInfo() != null) {
109-
url += request.getPathInfo();
109+
if (StringUtils.hasText(request.getRequestURI())) {
110+
url += request.getRequestURI();
110111
}
111112
return url;
112113
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/StaticResourceRequestTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.mock.web.MockHttpServletRequest;
2626
import org.springframework.mock.web.MockServletContext;
2727
import org.springframework.security.web.util.matcher.RequestMatcher;
28+
import org.springframework.util.StringUtils;
2829
import org.springframework.web.context.WebApplicationContext;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
@@ -156,15 +157,18 @@ private MockHttpServletRequest mockRequest(String servletPath, String path) {
156157
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
157158
if (servletPath != null) {
158159
request.setServletPath(servletPath);
160+
request.setRequestURI(servletPath + path);
161+
}
162+
else {
163+
request.setRequestURI(path);
159164
}
160-
request.setPathInfo(path);
161165
return request;
162166
}
163167

164168
private String getRequestPath(HttpServletRequest request) {
165169
String url = request.getServletPath();
166-
if (request.getPathInfo() != null) {
167-
url += request.getPathInfo();
170+
if (StringUtils.hasText(request.getRequestURI())) {
171+
url += request.getRequestURI();
168172
}
169173
return url;
170174
}

0 commit comments

Comments
 (0)