Skip to content

Commit 2b960b0

Browse files
committed
Polish Eager Header Config Tests
In the Java config tests, there is a simplified way to configure Spring, and that is with SpringTestRule. Also, test names typically follow the when-then convention. Issue: gh-6501
1 parent ac13b55 commit 2b960b0

File tree

2 files changed

+78
-131
lines changed

2 files changed

+78
-131
lines changed

config/src/test/java/org/springframework/security/config/annotation/authentication/configurers/HeadersConfigurerJavaTests.java

Lines changed: 0 additions & 131 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2002-2019 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+
* http://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.config.annotation.web.configurers;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.security.config.annotation.ObjectPostProcessor;
23+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
24+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
25+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
26+
import org.springframework.security.config.test.SpringTestRule;
27+
import org.springframework.security.web.header.HeaderWriterFilter;
28+
import org.springframework.test.web.servlet.MockMvc;
29+
30+
import static org.springframework.http.HttpHeaders.*;
31+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
32+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
33+
34+
/**
35+
* Tests for {@link HeadersConfigurer}.
36+
*
37+
* @author Ankur Pathak
38+
*/
39+
public class HeadersConfigurerEagerHeadersTests {
40+
41+
@Rule
42+
public final SpringTestRule spring = new SpringTestRule();
43+
44+
@Autowired
45+
MockMvc mvc;
46+
47+
@EnableWebSecurity
48+
public static class HeadersAtTheBeginningOfRequestConfig extends WebSecurityConfigurerAdapter {
49+
@Override
50+
protected void configure(HttpSecurity http) throws Exception {
51+
//@ formatter:off
52+
http
53+
.headers()
54+
.addObjectPostProcessor(new ObjectPostProcessor<HeaderWriterFilter>() {
55+
@Override
56+
public HeaderWriterFilter postProcess(HeaderWriterFilter filter) {
57+
filter.setShouldWriteHeadersEagerly(true);
58+
return filter;
59+
}
60+
});
61+
//@ formatter:on
62+
}
63+
}
64+
65+
@Test
66+
public void requestWhenHeadersEagerlyConfiguredThenHeadersAreWritten() throws Exception {
67+
this.spring.register(HeadersAtTheBeginningOfRequestConfig.class).autowire();
68+
69+
this.mvc.perform(get("/").secure(true))
70+
.andExpect(header().string("X-Content-Type-Options", "nosniff"))
71+
.andExpect(header().string("X-Frame-Options", "DENY"))
72+
.andExpect(header().string("Strict-Transport-Security", "max-age=31536000 ; includeSubDomains"))
73+
.andExpect(header().string(CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate"))
74+
.andExpect(header().string(EXPIRES, "0"))
75+
.andExpect(header().string(PRAGMA, "no-cache"))
76+
.andExpect(header().string("X-XSS-Protection", "1; mode=block"));
77+
}
78+
}

0 commit comments

Comments
 (0)