|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
26 | 26 | import static org.assertj.core.api.Assertions.assertThat;
|
27 | 27 |
|
28 | 28 | /**
|
| 29 | + * Tests for {@link XContentTypeOptionsServerHttpHeadersWriter} |
| 30 | + * |
29 | 31 | * @author Rob Winch
|
30 | 32 | * @since 5.0
|
31 | 33 | */
|
32 | 34 | public class XContentTypeOptionsServerHttpHeadersWriterTests {
|
33 | 35 |
|
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(); |
38 | 37 |
|
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()); |
42 | 39 |
|
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(); |
50 | 41 |
|
51 | 42 | @Test
|
52 |
| - public void writeHeadersWhenHeaderWrittenThenDoesNotOverride() { |
53 |
| - String headerValue = "value"; |
54 |
| - this.headers.set(ContentTypeOptionsServerHttpHeadersWriter.X_CONTENT_OPTIONS, headerValue); |
| 43 | + public void writeHeadersWhenNoHeadersThenWriteHeadersForXContentTypeOptionsServerHttpHeadersWriter() { |
55 | 44 | this.writer.writeHttpHeaders(this.exchange);
|
56 | 45 | 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)) |
73 | 47 | .containsOnly(XContentTypeOptionsServerHttpHeadersWriter.NOSNIFF);
|
74 | 48 | }
|
75 | 49 |
|
76 | 50 | @Test
|
77 | 51 | public void writeHeadersWhenHeaderWrittenThenDoesNotOverrideForXContentTypeOptionsServerHttpHeadersWriter() {
|
78 | 52 | 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)) |
83 | 57 | .containsOnly(headerValue);
|
84 | 58 | }
|
85 | 59 |
|
86 | 60 | @Test
|
87 | 61 | 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"); |
90 | 63 | assertThat(XContentTypeOptionsServerHttpHeadersWriter.NOSNIFF).isEqualTo("nosniff");
|
91 | 64 | }
|
92 | 65 |
|
|
0 commit comments