Skip to content

Commit 385defd

Browse files
committed
added tests
1 parent 62b4c65 commit 385defd

12 files changed

Lines changed: 1083 additions & 0 deletions

security-header/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
<groupId>com.google.code.gson</groupId>
2727
<artifactId>gson</artifactId>
2828
</dependency>
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.mockito</groupId>
36+
<artifactId>mockito-core</artifactId>
37+
<scope>test</scope>
38+
</dependency>
2939
</dependencies>
3040

3141
<build>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.header.filter;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.Mock;
23+
import org.mockito.MockitoAnnotations;
24+
25+
import javax.servlet.FilterChain;
26+
import javax.servlet.FilterConfig;
27+
import javax.servlet.ServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
import static org.mockito.Mockito.*;
31+
32+
/**
33+
* Tests for the CSP2Filter class.
34+
*
35+
* @author Dominik Schadow
36+
*/
37+
class CSP2FilterTest {
38+
private CSP2Filter csp2Filter;
39+
40+
@Mock
41+
private ServletRequest request;
42+
43+
@Mock
44+
private HttpServletResponse response;
45+
46+
@Mock
47+
private FilterChain filterChain;
48+
49+
@Mock
50+
private FilterConfig filterConfig;
51+
52+
@BeforeEach
53+
void setUp() {
54+
MockitoAnnotations.openMocks(this);
55+
csp2Filter = new CSP2Filter();
56+
}
57+
58+
@Test
59+
void doFilter_setsContentSecurityPolicyHeader() throws Exception {
60+
csp2Filter.doFilter(request, response, filterChain);
61+
62+
verify(response).setHeader("Content-Security-Policy", "default-src 'self'; frame-ancestors 'none'; reflected-xss block");
63+
}
64+
65+
@Test
66+
void doFilter_callsFilterChain() throws Exception {
67+
csp2Filter.doFilter(request, response, filterChain);
68+
69+
verify(filterChain).doFilter(request, response);
70+
}
71+
72+
@Test
73+
void doFilter_setsHeaderAndContinuesChain() throws Exception {
74+
csp2Filter.doFilter(request, response, filterChain);
75+
76+
verify(response).setHeader("Content-Security-Policy", "default-src 'self'; frame-ancestors 'none'; reflected-xss block");
77+
verify(filterChain).doFilter(request, response);
78+
}
79+
80+
@Test
81+
void init_doesNotThrowException() {
82+
csp2Filter.init(filterConfig);
83+
84+
verifyNoInteractions(filterConfig);
85+
}
86+
87+
@Test
88+
void destroy_doesNotThrowException() {
89+
csp2Filter.destroy();
90+
}
91+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.header.filter;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.Mock;
23+
import org.mockito.MockitoAnnotations;
24+
25+
import javax.servlet.FilterChain;
26+
import javax.servlet.FilterConfig;
27+
import javax.servlet.ServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
import static org.mockito.Mockito.*;
31+
32+
/**
33+
* Tests for the CSPFilter class.
34+
*
35+
* @author Dominik Schadow
36+
*/
37+
class CSPFilterTest {
38+
private CSPFilter cspFilter;
39+
40+
@Mock
41+
private ServletRequest request;
42+
43+
@Mock
44+
private HttpServletResponse response;
45+
46+
@Mock
47+
private FilterChain filterChain;
48+
49+
@Mock
50+
private FilterConfig filterConfig;
51+
52+
@BeforeEach
53+
void setUp() {
54+
MockitoAnnotations.openMocks(this);
55+
cspFilter = new CSPFilter();
56+
}
57+
58+
@Test
59+
void doFilter_setsContentSecurityPolicyHeader() throws Exception {
60+
cspFilter.doFilter(request, response, filterChain);
61+
62+
verify(response).setHeader("Content-Security-Policy", "default-src 'self'; report-uri CSPReporting");
63+
}
64+
65+
@Test
66+
void doFilter_callsFilterChain() throws Exception {
67+
cspFilter.doFilter(request, response, filterChain);
68+
69+
verify(filterChain).doFilter(request, response);
70+
}
71+
72+
@Test
73+
void doFilter_setsHeaderAndContinuesChain() throws Exception {
74+
cspFilter.doFilter(request, response, filterChain);
75+
76+
verify(response).setHeader("Content-Security-Policy", "default-src 'self'; report-uri CSPReporting");
77+
verify(filterChain).doFilter(request, response);
78+
}
79+
80+
@Test
81+
void init_doesNotThrowException() {
82+
cspFilter.init(filterConfig);
83+
84+
verifyNoInteractions(filterConfig);
85+
}
86+
87+
@Test
88+
void destroy_doesNotThrowException() {
89+
cspFilter.destroy();
90+
}
91+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.header.filter;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.Mock;
23+
import org.mockito.MockitoAnnotations;
24+
25+
import javax.servlet.FilterChain;
26+
import javax.servlet.FilterConfig;
27+
import javax.servlet.ServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
import static org.mockito.Mockito.*;
31+
32+
/**
33+
* Tests for the CSPReportingFilter class.
34+
*
35+
* @author Dominik Schadow
36+
*/
37+
class CSPReportingFilterTest {
38+
private CSPReportingFilter cspReportingFilter;
39+
40+
@Mock
41+
private ServletRequest request;
42+
43+
@Mock
44+
private HttpServletResponse response;
45+
46+
@Mock
47+
private FilterChain filterChain;
48+
49+
@Mock
50+
private FilterConfig filterConfig;
51+
52+
@BeforeEach
53+
void setUp() {
54+
MockitoAnnotations.openMocks(this);
55+
cspReportingFilter = new CSPReportingFilter();
56+
}
57+
58+
@Test
59+
void doFilter_setsContentSecurityPolicyReportOnlyHeader() throws Exception {
60+
cspReportingFilter.doFilter(request, response, filterChain);
61+
62+
verify(response).setHeader("Content-Security-Policy-Report-Only", "default-src 'self'; report-uri CSPReporting");
63+
}
64+
65+
@Test
66+
void doFilter_callsFilterChain() throws Exception {
67+
cspReportingFilter.doFilter(request, response, filterChain);
68+
69+
verify(filterChain).doFilter(request, response);
70+
}
71+
72+
@Test
73+
void doFilter_setsHeaderAndContinuesChain() throws Exception {
74+
cspReportingFilter.doFilter(request, response, filterChain);
75+
76+
verify(response).setHeader("Content-Security-Policy-Report-Only", "default-src 'self'; report-uri CSPReporting");
77+
verify(filterChain).doFilter(request, response);
78+
}
79+
80+
@Test
81+
void init_doesNotThrowException() {
82+
cspReportingFilter.init(filterConfig);
83+
84+
verifyNoInteractions(filterConfig);
85+
}
86+
87+
@Test
88+
void destroy_doesNotThrowException() {
89+
cspReportingFilter.destroy();
90+
}
91+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.header.filter;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.Mock;
23+
import org.mockito.MockitoAnnotations;
24+
25+
import javax.servlet.FilterChain;
26+
import javax.servlet.FilterConfig;
27+
import javax.servlet.ServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
import static org.mockito.Mockito.*;
31+
32+
/**
33+
* Tests for the CacheControlFilter class.
34+
*
35+
* @author Dominik Schadow
36+
*/
37+
class CacheControlFilterTest {
38+
private CacheControlFilter cacheControlFilter;
39+
40+
@Mock
41+
private ServletRequest request;
42+
43+
@Mock
44+
private HttpServletResponse response;
45+
46+
@Mock
47+
private FilterChain filterChain;
48+
49+
@Mock
50+
private FilterConfig filterConfig;
51+
52+
@BeforeEach
53+
void setUp() {
54+
MockitoAnnotations.openMocks(this);
55+
cacheControlFilter = new CacheControlFilter();
56+
}
57+
58+
@Test
59+
void doFilter_setsCacheControlHeader() throws Exception {
60+
cacheControlFilter.doFilter(request, response, filterChain);
61+
62+
verify(response).addHeader("Cache-Control", "no-cache, must-revalidate, max-age=0, no-store");
63+
}
64+
65+
@Test
66+
void doFilter_setsExpiresHeader() throws Exception {
67+
cacheControlFilter.doFilter(request, response, filterChain);
68+
69+
verify(response).addDateHeader("Expires", -1);
70+
}
71+
72+
@Test
73+
void doFilter_callsFilterChain() throws Exception {
74+
cacheControlFilter.doFilter(request, response, filterChain);
75+
76+
verify(filterChain).doFilter(request, response);
77+
}
78+
79+
@Test
80+
void doFilter_setsAllHeadersAndContinuesChain() throws Exception {
81+
cacheControlFilter.doFilter(request, response, filterChain);
82+
83+
verify(response).addHeader("Cache-Control", "no-cache, must-revalidate, max-age=0, no-store");
84+
verify(response).addDateHeader("Expires", -1);
85+
verify(filterChain).doFilter(request, response);
86+
}
87+
88+
@Test
89+
void init_doesNotThrowException() {
90+
cacheControlFilter.init(filterConfig);
91+
92+
verifyNoInteractions(filterConfig);
93+
}
94+
95+
@Test
96+
void destroy_doesNotThrowException() {
97+
cacheControlFilter.destroy();
98+
}
99+
}

0 commit comments

Comments
 (0)