@@ -109,13 +109,15 @@ public void testGetResponseBodyDecodesOnePartAndSplitPartsIdentically() {
109109 // 0xC3 0xA9 encodes U+00E9; split between its two bytes so neither half decodes on its own
110110 int split = 4 ;
111111
112+ // Clones onto the wire, so that utf8 stays an oracle rather than becoming a part's own storage.
112113 List <HttpResponseBodyPart > onePart = new LinkedList <>();
113- onePart .add (new EagerResponseBodyPart (Unpooled .wrappedBuffer (utf8 ), true ));
114+ onePart .add (new EagerResponseBodyPart (Unpooled .wrappedBuffer (utf8 . clone () ), true ));
114115 NettyResponse single = new NettyResponse (new NettyResponseStatus (null , null , null ), null , onePart );
115116
117+ byte [] wire = utf8 .clone ();
116118 List <HttpResponseBodyPart > splitParts = new LinkedList <>();
117- splitParts .add (new EagerResponseBodyPart (Unpooled .wrappedBuffer (utf8 , 0 , split ), false ));
118- splitParts .add (new EagerResponseBodyPart (Unpooled .wrappedBuffer (utf8 , split , utf8 .length - split ), true ));
119+ splitParts .add (new EagerResponseBodyPart (Unpooled .wrappedBuffer (wire , 0 , split ), false ));
120+ splitParts .add (new EagerResponseBodyPart (Unpooled .wrappedBuffer (wire , split , wire .length - split ), true ));
119121 NettyResponse multiple = new NettyResponse (new NettyResponseStatus (null , null , null ), null , splitParts );
120122
121123 assertEquals (expected , single .getResponseBody (StandardCharsets .UTF_8 ));
@@ -182,7 +184,10 @@ public void testGetResponseBodyAsBytesDoesNotShareTheBodyPartArray() {
182184 public void testGetResponseBodyAsBytesViewReturnsEmptyArray () {
183185 NettyResponse response = new NettyResponse (new NettyResponseStatus (null , null , null ), null , new LinkedList <>());
184186
185- assertArrayEquals (new byte [0 ], response .getResponseBodyAsBytesView ());
187+ byte [] view = response .getResponseBodyAsBytesView ();
188+ assertArrayEquals (new byte [0 ], view );
189+ // One shared instance rather than an allocation per call, which is the whole point of the branch.
190+ assertSame (view , response .getResponseBodyAsBytesView ());
186191 }
187192
188193 @ Test
@@ -198,6 +203,22 @@ public void testBodylessResponseHasAnEmptyByteBuf() {
198203 }
199204 }
200205
206+ @ Test
207+ public void testOverridingTheViewLeavesTheResponseTextAlone () {
208+ // The string accessors go through a private helper rather than the overridable view, so a subclass that
209+ // hardens the view cannot silently change what this response's text says.
210+ List <HttpResponseBodyPart > bodyParts = new LinkedList <>();
211+ bodyParts .add (new EagerResponseBodyPart (Unpooled .wrappedBuffer ("Hello World" .getBytes (StandardCharsets .UTF_8 )), true ));
212+ NettyResponse response = new NettyResponse (new NettyResponseStatus (null , null , null ), null , bodyParts ) {
213+ @ Override
214+ public byte [] getResponseBodyAsBytesView () {
215+ return "Goodbye" .getBytes (StandardCharsets .UTF_8 );
216+ }
217+ };
218+
219+ assertEquals ("Hello World" , response .getResponseBody (StandardCharsets .UTF_8 ));
220+ }
221+
201222 @ Test
202223 public void testGetResponseBodyAsBytesViewDefaultImplementationDelegates () throws Throwable {
203224 byte [] expected = "Hello World" .getBytes (StandardCharsets .UTF_8 );
0 commit comments