Skip to content

Commit a53cbb8

Browse files
Polish
Issue gh-13155
1 parent 8287289 commit a53cbb8

File tree

3 files changed

+79
-41
lines changed

3 files changed

+79
-41
lines changed

web/src/main/java/org/springframework/security/web/server/header/XContentTypeOptionsServerHttpHeadersWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2023 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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2023 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.server.header;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.http.HttpHeaders;
22+
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
23+
import org.springframework.mock.web.server.MockServerWebExchange;
24+
import org.springframework.web.server.ServerWebExchange;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link ContentTypeOptionsServerHttpHeadersWriter}
30+
*
31+
* @author Marcus da Coregio
32+
*/
33+
class ContentTypeOptionsServerHttpHeadersWriterTests {
34+
35+
ContentTypeOptionsServerHttpHeadersWriter writer = new ContentTypeOptionsServerHttpHeadersWriter();
36+
37+
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/").build());
38+
39+
HttpHeaders headers = this.exchange.getResponse().getHeaders();
40+
41+
@Test
42+
void writeHeadersWhenNoHeadersThenWriteHeaders() {
43+
this.writer.writeHttpHeaders(this.exchange);
44+
assertThat(this.headers).hasSize(1);
45+
assertThat(this.headers.get(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS))
46+
.containsOnly(ContentTypeOptionsServerHttpHeadersWriter.NOSNIFF);
47+
}
48+
49+
@Test
50+
void writeHeadersWhenHeaderWrittenThenDoesNotOverride() {
51+
String headerValue = "value";
52+
this.headers.set(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS, headerValue);
53+
this.writer.writeHttpHeaders(this.exchange);
54+
assertThat(this.headers).hasSize(1);
55+
assertThat(this.headers.get(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS))
56+
.containsOnly(headerValue);
57+
}
58+
59+
@Test
60+
void constantsMatchExpectedHeaderAndValue() {
61+
assertThat(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS).isEqualTo("X-Content-Type-Options");
62+
assertThat(ContentTypeOptionsServerHttpHeadersWriter.NOSNIFF).isEqualTo("nosniff");
63+
}
64+
65+
}

web/src/test/java/org/springframework/security/web/server/header/XContentTypeOptionsServerHttpHeadersWriterTests.java

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -26,67 +26,40 @@
2626
import static org.assertj.core.api.Assertions.assertThat;
2727

2828
/**
29+
* Tests for {@link XContentTypeOptionsServerHttpHeadersWriter}
30+
*
2931
* @author Rob Winch
3032
* @since 5.0
3133
*/
3234
public class XContentTypeOptionsServerHttpHeadersWriterTests {
3335

34-
ContentTypeOptionsServerHttpHeadersWriter writer = new ContentTypeOptionsServerHttpHeadersWriter();
35-
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/").build());
36-
HttpHeaders headers = this.exchange.getResponse().getHeaders();
37-
36+
XContentTypeOptionsServerHttpHeadersWriter writer = new XContentTypeOptionsServerHttpHeadersWriter();
3837

39-
XContentTypeOptionsServerHttpHeadersWriter writerXContentType = new XContentTypeOptionsServerHttpHeadersWriter();
40-
ServerWebExchange exchangeXContentType = MockServerWebExchange.from(MockServerHttpRequest.get("/").build());
41-
HttpHeaders headersXContentType = this.exchangeXContentType.getResponse().getHeaders();
38+
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/").build());
4239

43-
@Test
44-
public void writeHeadersWhenNoHeadersThenWriteHeaders() {
45-
this.writer.writeHttpHeaders(this.exchange);
46-
assertThat(this.headers).hasSize(1);
47-
assertThat(this.headers.get(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS))
48-
.containsOnly(ContentTypeOptionsServerHttpHeadersWriter.NOSNIFF);
49-
}
40+
HttpHeaders headers = this.exchange.getResponse().getHeaders();
5041

5142
@Test
52-
public void writeHeadersWhenHeaderWrittenThenDoesNotOverride() {
53-
String headerValue = "value";
54-
this.headers.set(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS, headerValue);
43+
public void writeHeadersWhenNoHeadersThenWriteHeadersForXContentTypeOptionsServerHttpHeadersWriter() {
5544
this.writer.writeHttpHeaders(this.exchange);
5645
assertThat(this.headers).hasSize(1);
57-
assertThat(this.headers.get(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS))
58-
.containsOnly(headerValue);
59-
}
60-
61-
@Test
62-
public void constantsMatchExpectedHeaderAndValue() {
63-
assertThat(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS)
64-
.isEqualTo("X-Content-Type-Options");
65-
assertThat(ContentTypeOptionsServerHttpHeadersWriter.NOSNIFF).isEqualTo("nosniff");
66-
}
67-
68-
@Test
69-
public void writeHeadersWhenNoHeadersThenWriteHeadersForXContentTypeOptionsServerHttpHeadersWriter() {
70-
this.writerXContentType.writeHttpHeaders(this.exchangeXContentType);
71-
assertThat(this.headersXContentType).hasSize(1);
72-
assertThat(this.headersXContentType.get(XContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS))
46+
assertThat(this.headers.get(XContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS))
7347
.containsOnly(XContentTypeOptionsServerHttpHeadersWriter.NOSNIFF);
7448
}
7549

7650
@Test
7751
public void writeHeadersWhenHeaderWrittenThenDoesNotOverrideForXContentTypeOptionsServerHttpHeadersWriter() {
7852
String headerValue = "value";
79-
this.headersXContentType.set(XContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS, headerValue);
80-
this.writerXContentType.writeHttpHeaders(this.exchangeXContentType);
81-
assertThat(this.headersXContentType).hasSize(1);
82-
assertThat(this.headersXContentType.get(XContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS))
53+
this.headers.set(XContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS, headerValue);
54+
this.writer.writeHttpHeaders(this.exchange);
55+
assertThat(this.headers).hasSize(1);
56+
assertThat(this.headers.get(XContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS))
8357
.containsOnly(headerValue);
8458
}
8559

8660
@Test
8761
public void constantsMatchExpectedHeaderAndValueForXContentTypeOptionsServerHttpHeadersWriter() {
88-
assertThat(XContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS)
89-
.isEqualTo("X-Content-Type-Options");
62+
assertThat(XContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS).isEqualTo("X-Content-Type-Options");
9063
assertThat(XContentTypeOptionsServerHttpHeadersWriter.NOSNIFF).isEqualTo("nosniff");
9164
}
9265

0 commit comments

Comments
 (0)