1 package com.atlassian.httpclient.apache.httpcomponents;
2
3 import com.atlassian.httpclient.api.Response;
4 import org.apache.commons.lang3.StringUtils;
5 import org.junit.Test;
6
7 import java.io.IOException;
8 import java.util.concurrent.ExecutionException;
9
10 import static org.junit.Assert.assertEquals;
11
12
13
14
15 public class DefaultResponseTest {
16 @Test
17 public void testRetrievalWithinLimits() throws ExecutionException, InterruptedException, IOException {
18 Response response = DefaultResponse.builder()
19 .setMaxEntitySize(100)
20 .setHeader("Content-Length", "50")
21 .setEntityStream(new GeneratingInputStream('x', 50L))
22 .build();
23 String data = response.getEntity();
24 assertEquals(StringUtils.repeat("x", 50), data);
25 }
26
27 @Test
28 public void testRetrievalWithinLimitsNoLength() throws ExecutionException, InterruptedException, IOException {
29 Response response = DefaultResponse.builder()
30 .setMaxEntitySize(100)
31 .setEntityStream(new GeneratingInputStream('x', 50L))
32 .build();
33 String data = response.getEntity();
34 assertEquals(StringUtils.repeat("x", 50), data);
35 }
36
37 @Test(expected = IllegalArgumentException.class)
38 public void testRetrievalOutsideLimitsWithLength() throws ExecutionException, InterruptedException, IOException {
39 Response response = DefaultResponse.builder()
40 .setMaxEntitySize(100)
41 .setHeader("Content-Length", "110")
42 .setEntityStream(new GeneratingInputStream('x', 110L))
43 .build();
44 response.getEntity();
45 }
46
47 @Test(expected = IllegalArgumentException.class)
48 public void testRetrievalOutsideLimitsNoLength() throws ExecutionException, InterruptedException, IOException {
49 Response response = DefaultResponse.builder()
50 .setMaxEntitySize(100)
51 .setEntityStream(new GeneratingInputStream('x', 150L))
52 .build();
53 response.getEntity();
54 }
55
56 @Test
57 public void testRetrievingContentLengthFromHeader() {
58 Response response = DefaultResponse.builder()
59 .setMaxEntitySize(100)
60 .setEntityStream(new GeneratingInputStream('x', 150L))
61 .setHeader(Headers.Names.CONTENT_LENGTH, "150")
62 .build();
63 assertEquals(Long.valueOf(150), response.getContentLength().get());
64 }
65 }