@@ -58,23 +58,71 @@ class HttpHeadersTests {
5858 final HttpHeaders headers = new HttpHeaders ();
5959
6060 @ Test
61- void constructorUnwrapsReadonly () {
61+ void backedByFactoryUnwrapsReadonly () {
6262 headers .setContentType (MediaType .APPLICATION_JSON );
6363 HttpHeaders readOnly = HttpHeaders .readOnlyHttpHeaders (headers );
6464 assertThat (readOnly .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
65- HttpHeaders writable = new HttpHeaders (readOnly );
65+ HttpHeaders writable = HttpHeaders . backedBy (readOnly );
6666 writable .setContentType (MediaType .TEXT_PLAIN );
67+
6768 // content-type value is cached by ReadOnlyHttpHeaders
69+ assertThat (readOnly .getContentType ())
70+ .describedAs ("readOnly HttpHeaders should NOT have updated content-type value" )
71+ .isEqualTo (MediaType .APPLICATION_JSON );
72+ assertThat (writable .getContentType ())
73+ .describedAs ("writable HttpHeaders should have updated content-type value" )
74+ .isEqualTo (MediaType .TEXT_PLAIN );
75+ assertThat (headers .getContentType ())
76+ .describedAs ("initial HttpHeaders should have updated content-type value" )
77+ .isEqualTo (MediaType .TEXT_PLAIN );
78+ }
79+
80+ @ Test
81+ void backedByFactoryUnwrapsMultipleHeaders () {
82+ headers .setContentType (MediaType .APPLICATION_JSON );
83+ HttpHeaders readonlyOriginalExchangeHeaders = HttpHeaders .readOnlyHttpHeaders (headers );
84+ HttpHeaders firewallHeaders = HttpHeaders .backedBy (readonlyOriginalExchangeHeaders );
85+ HttpHeaders writeable = HttpHeaders .backedBy (firewallHeaders );
86+ writeable .setContentType (MediaType .TEXT_PLAIN );
87+
88+ // If readonly headers are unwrapped multiple times, the content-type value should be updated
89+ assertThat (writeable .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
90+ assertThat (firewallHeaders .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
91+ assertThat (readonlyOriginalExchangeHeaders .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
92+ assertThat (headers .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
93+ }
94+
95+ @ Test
96+ void copyOfFactoryUnwrapsReadonly () {
97+ headers .setContentType (MediaType .APPLICATION_JSON );
98+ HttpHeaders readOnly = HttpHeaders .readOnlyHttpHeaders (headers );
6899 assertThat (readOnly .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
69- assertThat (writable .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
100+ HttpHeaders writable = HttpHeaders .copyOf (readOnly );
101+ writable .setContentType (MediaType .TEXT_PLAIN );
102+
103+ assertThat (readOnly .getContentType ())
104+ .describedAs ("readOnly HttpHeaders should NOT have updated content-type value" )
105+ .isEqualTo (MediaType .APPLICATION_JSON );
106+ assertThat (writable .getContentType ())
107+ .describedAs ("writable HttpHeaders should have updated content-type value" )
108+ .isEqualTo (MediaType .TEXT_PLAIN );
109+ assertThat (headers .getContentType ())
110+ .describedAs ("initial HttpHeaders should have updated content-type value" )
111+ .isEqualTo (MediaType .APPLICATION_JSON );
70112 }
71113
72114 @ 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 );
115+ void copyOfFactoryUnwrapsMultipleHeaders () {
116+ headers .setContentType (MediaType .APPLICATION_JSON );
117+ HttpHeaders readonlyOriginalExchangeHeaders = HttpHeaders .readOnlyHttpHeaders (headers );
118+ HttpHeaders firewallHeaders = HttpHeaders .copyOf (readonlyOriginalExchangeHeaders );
119+ HttpHeaders writeable = HttpHeaders .copyOf (firewallHeaders );
120+ writeable .setContentType (MediaType .TEXT_PLAIN );
121+
122+ assertThat (writeable .getContentType ()).isEqualTo (MediaType .TEXT_PLAIN );
123+ assertThat (firewallHeaders .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
124+ assertThat (readonlyOriginalExchangeHeaders .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
125+ assertThat (headers .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
78126 }
79127
80128 @ Test
0 commit comments