Skip to content

Commit a2793f3

Browse files
authored
Merge Add XML Based shouldWriteHeadersEagerly tests
Add XML Based shouldWriteHeadersEagerly tests
2 parents 08fca57 + 679a47a commit a2793f3

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

config/src/test/java/org/springframework/security/config/http/HttpHeadersConfigTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@
2828
import org.junit.jupiter.api.Test;
2929
import org.junit.jupiter.api.extension.ExtendWith;
3030

31+
import org.springframework.beans.BeansException;
3132
import org.springframework.beans.factory.BeanCreationException;
3233
import org.springframework.beans.factory.annotation.Autowired;
34+
import org.springframework.beans.factory.config.BeanPostProcessor;
3335
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
3436
import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException;
3537
import org.springframework.security.config.test.SpringTestContext;
3638
import org.springframework.security.config.test.SpringTestContextExtension;
3739
import org.springframework.security.core.Authentication;
3840
import org.springframework.security.web.authentication.session.SessionLimit;
41+
import org.springframework.security.web.header.HeaderWriterFilter;
3942
import org.springframework.test.web.servlet.MockMvc;
4043
import org.springframework.test.web.servlet.ResultMatcher;
4144
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
@@ -150,6 +153,16 @@ public void requestWhenHeadersElementUsedThenResponseContainsAllSecureHeaders()
150153
// @formatter:on
151154
}
152155

156+
@Test
157+
public void requestWhenHeadersEagerlyConfiguredThenHeadersAreWritten() throws Exception {
158+
this.spring.configLocations(this.xml("HeadersEagerlyConfigured")).autowire();
159+
// @formatter:off
160+
this.mvc.perform(get("/").secure(true))
161+
.andExpect(status().isOk())
162+
.andExpect(includesDefaults());
163+
// @formatter:on
164+
}
165+
153166
@Test
154167
public void requestWhenFrameOptionsConfiguredThenIncludesHeader() throws Exception {
155168
Map<String, String> headers = new HashMap(defaultHeaders);
@@ -955,6 +968,18 @@ public String ok() {
955968

956969
}
957970

971+
public static class EagerHeadersBeanPostProcessor implements BeanPostProcessor {
972+
973+
@Override
974+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
975+
if (bean instanceof HeaderWriterFilter headerWriterFilter) {
976+
headerWriterFilter.setShouldWriteHeadersEagerly(true);
977+
}
978+
return bean;
979+
}
980+
981+
}
982+
958983
public static class CustomSessionLimit implements SessionLimit {
959984

960985
@Override
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2004-present the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://www.springframework.org/schema/security"
21+
xsi:schemaLocation="
22+
http://www.springframework.org/schema/security
23+
https://www.springframework.org/schema/security/spring-security.xsd
24+
http://www.springframework.org/schema/beans
25+
https://www.springframework.org/schema/beans/spring-beans.xsd">
26+
27+
<http auto-config="true">
28+
<headers/>
29+
<intercept-url pattern="/**" access="permitAll"/>
30+
</http>
31+
32+
<b:bean class="org.springframework.security.config.http.HttpHeadersConfigTests.EagerHeadersBeanPostProcessor"/>
33+
34+
<b:bean name="simple" class="org.springframework.security.config.http.HttpHeadersConfigTests.SimpleController"/>
35+
36+
<b:import resource="userservice.xml"/>
37+
</b:beans>

0 commit comments

Comments
 (0)