Skip to content

Commit d0e8d29

Browse files
author
Adam Ostrožlík
committed
RequestRejectedHandler tests
1 parent 61ca3bd commit d0e8d29

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/builders/WebSecurityTests.java

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.config.annotation.web.builders;
1818

19+
import jakarta.servlet.ServletException;
1920
import jakarta.servlet.http.HttpServletResponse;
2021

2122
import org.junit.jupiter.api.AfterEach;
@@ -24,6 +25,7 @@
2425

2526
import org.springframework.beans.factory.annotation.Autowired;
2627
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.http.HttpStatus;
2729
import org.springframework.mock.web.MockFilterChain;
2830
import org.springframework.mock.web.MockHttpServletRequest;
2931
import org.springframework.mock.web.MockHttpServletResponse;
@@ -32,13 +34,16 @@
3234
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3335
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
3436
import org.springframework.security.web.FilterChainProxy;
37+
import org.springframework.security.web.firewall.HttpStatusRequestRejectedHandler;
3538
import org.springframework.web.bind.annotation.RequestMapping;
3639
import org.springframework.web.bind.annotation.RestController;
3740
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3841
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
3942
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
4043
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
4144

45+
import java.io.IOException;
46+
4247
import static org.assertj.core.api.Assertions.assertThat;
4348

4449
/**
@@ -92,6 +97,15 @@ public void ignoringMvcMatcher() throws Exception {
9297
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
9398
}
9499

100+
@Test
101+
public void requestRejectedHandlerInvoked() throws ServletException, IOException {
102+
loadConfig(RequestRejectedHandlerConfig.class);
103+
this.request.setServletPath("/spring");
104+
this.request.setRequestURI("/spring/\u0019path");
105+
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
106+
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST);
107+
}
108+
95109
@Test
96110
public void ignoringMvcMatcherServletPath() throws Exception {
97111
loadConfig(MvcMatcherServletPathConfig.class, LegacyMvcMatchingConfig.class);
@@ -223,4 +237,14 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
223237

224238
}
225239

240+
@EnableWebSecurity
241+
static class RequestRejectedHandlerConfig extends WebSecurityConfigurerAdapter {
242+
243+
@Override
244+
public void configure(WebSecurity web) throws Exception {
245+
web.requestRejectedHandler(new HttpStatusRequestRejectedHandler(HttpStatus.BAD_REQUEST.value()));
246+
}
247+
248+
}
249+
226250
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.firewall;
18+
19+
import jakarta.servlet.http.HttpServletRequest;
20+
import jakarta.servlet.http.HttpServletResponse;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
24+
import static org.mockito.Mockito.mock;
25+
26+
public class CompositeRequestRejectedHandlerTests {
27+
28+
@Test
29+
void compositeRequestRejectedHandlerRethrowsTheException() {
30+
RequestRejectedException requestRejectedException = new RequestRejectedException("rejected");
31+
DefaultRequestRejectedHandler sut = new DefaultRequestRejectedHandler();
32+
CompositeRequestRejectedHandler crrh = new CompositeRequestRejectedHandler(sut);
33+
assertThatExceptionOfType(RequestRejectedException.class).isThrownBy(() -> crrh
34+
.handle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), requestRejectedException))
35+
.withMessage("rejected");
36+
}
37+
38+
@Test
39+
void compositeRequestRejectedHandlerForbidsEmptyHandlers() {
40+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(CompositeRequestRejectedHandler::new);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)