1+ /*
2+ * Copyright 2017 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+ * http://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 .ws .transport .http ;
18+
19+ import java .io .ByteArrayInputStream ;
20+ import java .io .InputStream ;
21+ import java .util .Collections ;
22+ import java .util .Random ;
23+
24+ import org .apache .commons .io .IOUtils ;
25+ import org .apache .commons .io .input .CountingInputStream ;
26+ import org .easymock .Capture ;
27+ import org .junit .Test ;
28+
29+ import org .springframework .ws .WebServiceMessage ;
30+ import org .springframework .ws .WebServiceMessageFactory ;
31+
32+ import static org .easymock .EasyMock .*;
33+ import static org .junit .Assert .*;
34+
35+ /**
36+ * @author Andreas Veithen
37+ */
38+ public class AbstractHttpSenderConnectionTest {
39+
40+ /**
41+ * Tests that {@link AbstractHttpSenderConnection} doesn't consume the response stream before
42+ * passing it to the message factory. This is a regression test for SWS-707.
43+ *
44+ * @param chunking
45+ * Specifies whether the test should simulate a response with chunking enabled.
46+ * @throws Exception
47+ */
48+ private void testSupportsStreaming (boolean chunking ) throws Exception {
49+ byte [] content = new byte [16 *1024 ];
50+ new Random ().nextBytes (content );
51+ CountingInputStream rawInputStream = new CountingInputStream (new ByteArrayInputStream (content ));
52+
53+ AbstractHttpSenderConnection connection = createNiceMock (AbstractHttpSenderConnection .class );
54+ expect (connection .getResponseCode ()).andReturn (200 );
55+ // Simulate response with chunking enabled
56+ expect (connection .getResponseContentLength ()).andReturn (chunking ? -1L : content .length );
57+ expect (connection .getRawResponseInputStream ()).andReturn (rawInputStream );
58+ expect (connection .getResponseHeaders (anyObject ())).andReturn (Collections .emptyIterator ());
59+
60+ // Create a mock message factory to capture the InputStream passed to it
61+ WebServiceMessageFactory messageFactory = createNiceMock (WebServiceMessageFactory .class );
62+ WebServiceMessage message = createNiceMock (WebServiceMessage .class );
63+ Capture <InputStream > inputStreamCapture = new Capture <>();
64+ expect (messageFactory .createWebServiceMessage (capture (inputStreamCapture ))).andReturn (message );
65+
66+ replay (connection , messageFactory , message );
67+
68+ connection .receive (messageFactory );
69+
70+ assertTrue ("The raw input stream has been completely consumed" ,
71+ rawInputStream .getCount () < content .length );
72+ assertArrayEquals ("Unexpected content received by the message factory" ,
73+ content , IOUtils .toByteArray (inputStreamCapture .getValue ()));
74+ }
75+
76+ @ Test
77+ public void testSupportsStreamingWithChunkingEnabled () throws Exception {
78+ testSupportsStreaming (true );
79+ }
80+
81+ @ Test
82+ public void testSupportsStreamingWithChunkingDisabled () throws Exception {
83+ testSupportsStreaming (false );
84+ }
85+ }
0 commit comments