|
11 | 11 | import static org.mockito.Mockito.mock; |
12 | 12 | import static org.mockito.Mockito.when; |
13 | 13 |
|
| 14 | +import java.io.ByteArrayInputStream; |
| 15 | +import java.io.InputStream; |
14 | 16 | import java.net.SocketException; |
| 17 | +import java.nio.charset.StandardCharsets; |
15 | 18 | import java.util.Collection; |
16 | 19 | import java.util.Collections; |
17 | 20 | import java.util.Map; |
|
20 | 23 | import org.apache.http.HttpEntity; |
21 | 24 | import org.apache.http.HttpResponse; |
22 | 25 | import org.apache.http.client.HttpClient; |
| 26 | +import org.apache.http.entity.InputStreamEntity; |
23 | 27 | import org.apache.http.entity.StringEntity; |
24 | 28 | import org.apache.http.message.BasicHeader; |
25 | 29 | import org.apache.http.message.BasicHttpResponse; |
@@ -162,4 +166,35 @@ void ensureNoRedundantHeadersForPaginatedRequests() |
162 | 166 | final Map<String, Collection<String>> lastRequestHeaders = nextResult.getODataRequest().getHeaders(); |
163 | 167 | assertThat(lastRequestHeaders).containsExactly(entry("Accept", Collections.singletonList("application/json"))); |
164 | 168 | } |
| 169 | + |
| 170 | + @Test |
| 171 | + @SneakyThrows |
| 172 | + void testDisabledBuffer() |
| 173 | + { |
| 174 | + // test setup for request |
| 175 | + final ODataRequestGeneric oDataRequest = |
| 176 | + new ODataRequestRead("generic/service/path", "entity(123)", null, ODataProtocol.V4); |
| 177 | + |
| 178 | + // test setup for streamed http response |
| 179 | + final BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, 200, "OK"); |
| 180 | + final String json = "{\"value\":[]}"; |
| 181 | + final InputStream inputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); |
| 182 | + httpResponse.setEntity(new InputStreamEntity(inputStream, json.length())); |
| 183 | + |
| 184 | + // system under test |
| 185 | + final ODataRequestResultGeneric testResult = new ODataRequestResultGeneric(oDataRequest, httpResponse); |
| 186 | + testResult.disableBufferingHttpResponse(); |
| 187 | + |
| 188 | + // sanity checks do not consume the response |
| 189 | + assertThat(testResult.getHeaderValues("Content-Length")).isEmpty(); |
| 190 | + assertThat(testResult.getHttpResponse().getStatusLine().getStatusCode()).isEqualTo(200); |
| 191 | + |
| 192 | + // true-positive, successfully read once |
| 193 | + assertThat(testResult.asListOfMaps()).isEmpty(); |
| 194 | + |
| 195 | + // true-negative, no second read possible |
| 196 | + assertThatThrownBy(testResult::asListOfMaps) |
| 197 | + .isInstanceOf(ODataDeserializationException.class) |
| 198 | + .hasMessageContaining("Unable to read OData 4.0 response."); |
| 199 | + } |
165 | 200 | } |
0 commit comments