@@ -57,24 +57,79 @@ class HttpHeadersTests {
57
57
58
58
final HttpHeaders headers = new HttpHeaders ();
59
59
60
+ /**
61
+ * Not the different behaviors found in {@link HttpHeaders#backedBy(HttpHeaders)) {@link HttpHeaders#HttpHeaders(HttpHeaders)}
62
+ * versus the {@link HttpHeaders#copyOf(HttpHeaders)} methods.
63
+ */
60
64
@ Test
61
- void constructorUnwrapsReadonly () {
65
+ void backedByFactoryUnwrapsReadonly () {
62
66
headers .setContentType (MediaType .APPLICATION_JSON );
63
67
HttpHeaders readOnly = HttpHeaders .readOnlyHttpHeaders (headers );
68
+
64
69
assertThat (readOnly .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
65
- HttpHeaders writable = new HttpHeaders (readOnly );
70
+ HttpHeaders writable = HttpHeaders . backedBy (readOnly );
66
71
writable .setContentType (MediaType .TEXT_PLAIN );
72
+
67
73
// content-type value is cached by ReadOnlyHttpHeaders
74
+ assertThat (readOnly .getContentType ())
75
+ .describedAs ("readOnly HttpHeaders should NOT have updated content-type value" )
76
+ .isEqualTo (MediaType .APPLICATION_JSON ); // Note that the content type was cached by the previous assertion
77
+ assertThat (writable .getContentType ())
78
+ .describedAs ("writable HttpHeaders should have updated content-type value" )
79
+ .isEqualTo (MediaType .TEXT_PLAIN );
80
+ assertThat (headers .getContentType ())
81
+ .describedAs ("initial HttpHeaders should have updated content-type value" )
82
+ .isEqualTo (MediaType .TEXT_PLAIN ); // Note that both writable and the original changed because the backing map is shared
83
+ }
84
+
85
+ @ Test
86
+ void backedByFactoryUnwrapsMultipleHeaders () {
87
+ headers .setContentType (MediaType .APPLICATION_JSON );
88
+ HttpHeaders readonlyOriginalExchangeHeaders = HttpHeaders .readOnlyHttpHeaders (headers );
89
+ HttpHeaders firewallHeaders = HttpHeaders .backedBy (readonlyOriginalExchangeHeaders );
90
+ HttpHeaders writeable = HttpHeaders .backedBy (firewallHeaders );
91
+ writeable .setContentType (MediaType .TEXT_PLAIN );
92
+
93
+ // If readonly headers are unwrapped multiple times, the content-type value should be updated
94
+ assertThat (writeable .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN ); // Note that all changed because the backing map is shared
95
+ assertThat (firewallHeaders .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
96
+ assertThat (readonlyOriginalExchangeHeaders .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN ); // Note that the content type was not cached because there was no previous call to getContentType
97
+ assertThat (headers .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
98
+ }
99
+
100
+ @ Test
101
+ void copyOfFactoryUnwrapsReadonly () {
102
+ headers .setContentType (MediaType .APPLICATION_JSON );
103
+ HttpHeaders readOnly = HttpHeaders .readOnlyHttpHeaders (headers );
104
+
68
105
assertThat (readOnly .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
69
- assertThat (writable .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
106
+ HttpHeaders writable = HttpHeaders .copyOf (readOnly );
107
+ writable .setContentType (MediaType .TEXT_PLAIN );
108
+
109
+ // content-type value is cached by ReadOnlyHttpHeaders
110
+ assertThat (readOnly .getContentType ())
111
+ .describedAs ("readOnly HttpHeaders should NOT have updated content-type value" )
112
+ .isEqualTo (MediaType .APPLICATION_JSON ); // Note that the content type was cached by the previous assertion
113
+ assertThat (writable .getContentType ())
114
+ .describedAs ("writable HttpHeaders should have updated content-type value" )
115
+ .isEqualTo (MediaType .TEXT_PLAIN ); // Note that only the copy is changed, because the backing map is a new instance
116
+ assertThat (headers .getContentType ())
117
+ .describedAs ("initial HttpHeaders should have updated content-type value" )
118
+ .isEqualTo (MediaType .APPLICATION_JSON );
70
119
}
71
120
72
121
@ Test
73
- void writableHttpHeadersUnwrapsMultiple () {
74
- HttpHeaders originalExchangeHeaders = HttpHeaders .readOnlyHttpHeaders (new HttpHeaders ());
75
- HttpHeaders firewallHeaders = new HttpHeaders (originalExchangeHeaders );
76
- HttpHeaders writeable = new HttpHeaders (firewallHeaders );
77
- writeable .setContentType (MediaType .APPLICATION_JSON );
122
+ void copyOfFactoryUnwrapsMultipleHeaders () {
123
+ headers .setContentType (MediaType .APPLICATION_JSON );
124
+ HttpHeaders readonlyOriginalExchangeHeaders = HttpHeaders .readOnlyHttpHeaders (headers );
125
+ HttpHeaders firewallHeaders = HttpHeaders .copyOf (readonlyOriginalExchangeHeaders );
126
+ HttpHeaders writeable = HttpHeaders .copyOf (firewallHeaders );
127
+ writeable .setContentType (MediaType .TEXT_PLAIN );
128
+
129
+ assertThat (writeable .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN ); // Note that only the copy is changed, because the backing map is a new instance
130
+ assertThat (firewallHeaders .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
131
+ assertThat (readonlyOriginalExchangeHeaders .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
132
+ assertThat (headers .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
78
133
}
79
134
80
135
@ Test
0 commit comments