|
12 | 12 | */
|
13 | 13 | package com.ning.http.client.async;
|
14 | 14 |
|
| 15 | +import static org.testng.Assert.assertEquals; |
| 16 | +import static org.testng.Assert.assertTrue; |
| 17 | +import static org.testng.FileAssert.fail; |
| 18 | + |
| 19 | +import org.testng.annotations.Test; |
| 20 | + |
15 | 21 | import com.ning.http.client.AsyncHttpClient;
|
16 | 22 | import com.ning.http.client.AsyncHttpClientConfig;
|
17 | 23 | import com.ning.http.client.ListenableFuture;
|
| 24 | +import com.ning.http.client.Request; |
18 | 25 | import com.ning.http.client.RequestBuilder;
|
19 | 26 | import com.ning.http.client.Response;
|
20 | 27 | import com.ning.http.client.generators.InputStreamBodyGenerator;
|
21 | 28 |
|
22 |
| -import org.testng.annotations.Test; |
23 |
| - |
24 | 29 | import java.io.BufferedInputStream;
|
| 30 | +import java.io.ByteArrayInputStream; |
25 | 31 | import java.io.ByteArrayOutputStream;
|
26 | 32 | import java.io.File;
|
27 | 33 | import java.io.FileInputStream;
|
28 | 34 | import java.io.IOException;
|
29 | 35 | import java.io.InputStream;
|
30 |
| -import java.net.URISyntaxException; |
31 | 36 | import java.net.URL;
|
32 | 37 | import java.util.Random;
|
33 | 38 |
|
34 |
| -import static org.testng.Assert.*; |
35 |
| -import static org.testng.FileAssert.fail; |
36 |
| - |
37 | 39 | /**
|
38 | 40 | * Test that the url fetcher is able to communicate via a proxy
|
39 | 41 | *
|
@@ -66,77 +68,62 @@ abstract public class ChunkingTest extends AbstractBasicTest {
|
66 | 68 | }
|
67 | 69 | }
|
68 | 70 |
|
69 |
| - /** |
70 |
| - * Tests that the custom chunked stream result in success and content returned that is unchunked |
71 |
| - */ |
| 71 | + // So we can just test the returned data is the image, |
| 72 | + // and doesn't contain the chunked delimeters. |
72 | 73 | @Test()
|
73 |
| - public void testBufferLargerThanFile() throws Throwable { |
74 |
| - doTest(new BufferedInputStream(new FileInputStream(getTestFile()), 400000)); |
| 74 | + public void testBufferLargerThanFileWithStreamBodyGenerator() throws Throwable { |
| 75 | + doTestWithInputStreamBodyGenerator(new BufferedInputStream(new ByteArrayInputStream(LARGE_IMAGE_BYTES), 400000)); |
75 | 76 | }
|
76 | 77 |
|
77 | 78 | @Test()
|
78 |
| - public void testBufferSmallThanFile() throws Throwable { |
79 |
| - doTest(new BufferedInputStream(new FileInputStream(getTestFile()))); |
| 79 | + public void testBufferSmallThanFileWithStreamBodyGenerator() throws Throwable { |
| 80 | + doTestWithInputStreamBodyGenerator(new BufferedInputStream(new ByteArrayInputStream(LARGE_IMAGE_BYTES))); |
80 | 81 | }
|
81 | 82 |
|
82 | 83 | @Test()
|
83 |
| - public void testDirectFile() throws Throwable { |
84 |
| - doTest(new FileInputStream(getTestFile())); |
| 84 | + public void testDirectFileWithStreamBodyGenerator() throws Throwable { |
| 85 | + doTestWithInputStreamBodyGenerator(new ByteArrayInputStream(LARGE_IMAGE_BYTES)); |
85 | 86 | }
|
86 | 87 |
|
87 |
| - public void doTest(InputStream is) throws Throwable { |
88 |
| - AsyncHttpClientConfig.Builder bc = new AsyncHttpClientConfig.Builder()// |
89 |
| - .setAllowPoolingConnections(true)// |
90 |
| - .setMaxConnectionsPerHost(1)// |
91 |
| - .setMaxConnections(1)// |
92 |
| - .setConnectTimeout(1000)// |
93 |
| - .setRequestTimeout(1000)// |
94 |
| - .setFollowRedirect(true); |
| 88 | + private void doTestWithInputStreamBodyGenerator(InputStream is) throws Throwable { |
| 89 | + AsyncHttpClientConfig.Builder bc = httpClientBuilder(); |
| 90 | + |
| 91 | + try (AsyncHttpClient c = getAsyncHttpClient(bc.build())) { |
95 | 92 |
|
96 |
| - try (AsyncHttpClient client = getAsyncHttpClient(bc.build())) { |
97 | 93 | RequestBuilder builder = new RequestBuilder("POST");
|
98 | 94 | builder.setUrl(getTargetUrl());
|
99 |
| - // made buff in stream big enough to mark. |
100 | 95 | builder.setBody(new InputStreamBodyGenerator(is));
|
101 | 96 |
|
102 |
| - ListenableFuture<Response> response = client.executeRequest(builder.build()); |
103 |
| - Response res = response.get(); |
104 |
| - assertNotNull(res.getResponseBodyAsStream()); |
105 |
| - if (500 == res.getStatusCode()) { |
106 |
| - assertEquals(res.getStatusCode(), 500, "Should have 500 status code"); |
107 |
| - assertTrue(res.getHeader("X-Exception").contains("invalid.chunk.length"), "Should have failed due to chunking"); |
108 |
| - fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + res.getHeader("X-Exception")); |
109 |
| - } else { |
110 |
| - assertEquals(readInputStreamToBytes(res.getResponseBodyAsStream()), LARGE_IMAGE_BYTES); |
111 |
| - } |
| 97 | + Request r = builder.build(); |
| 98 | + |
| 99 | + final ListenableFuture<Response> responseFuture = c.executeRequest(r); |
| 100 | + waitForAndAssertResponse(responseFuture); |
112 | 101 | }
|
113 | 102 | }
|
114 |
| - |
115 |
| - |
116 |
| - private byte[] readInputStreamToBytes(InputStream stream) throws IOException { |
117 |
| - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
118 |
| - try { |
119 |
| - int nRead; |
120 |
| - byte[] tmp = new byte[8192]; |
121 |
| - |
122 |
| - while ((nRead = stream.read(tmp, 0, tmp.length)) != -1) { |
123 |
| - buffer.write(tmp, 0, nRead); |
124 |
| - } |
125 |
| - |
126 |
| - buffer.flush(); |
127 |
| - return buffer.toByteArray(); |
128 | 103 |
|
129 |
| - } finally { |
130 |
| - try { |
131 |
| - stream.close(); |
132 |
| - } catch (Exception e2) { |
133 |
| - } |
134 |
| - } |
| 104 | + protected AsyncHttpClientConfig.Builder httpClientBuilder() { |
| 105 | + return new AsyncHttpClientConfig.Builder()// |
| 106 | + .setAllowPoolingConnections(true)// |
| 107 | + .setMaxConnectionsPerHost(1)// |
| 108 | + .setMaxConnections(1)// |
| 109 | + .setConnectTimeout(1000)// |
| 110 | + .setRequestTimeout(1000).setFollowRedirect(true); |
135 | 111 | }
|
136 | 112 |
|
137 |
| - private static File getTestFile() throws URISyntaxException { |
138 |
| - String testResource1 = "300k.png"; |
139 |
| - URL url = ChunkingTest.class.getClassLoader().getResource(testResource1); |
140 |
| - return new File(url.toURI()); |
| 113 | + protected void waitForAndAssertResponse(ListenableFuture<Response> responseFuture) throws InterruptedException, java.util.concurrent.ExecutionException, IOException { |
| 114 | + Response response = responseFuture.get(); |
| 115 | + if (500 == response.getStatusCode()) { |
| 116 | + StringBuilder sb = new StringBuilder(); |
| 117 | + sb.append("==============\n"); |
| 118 | + sb.append("500 response from call\n"); |
| 119 | + sb.append("Headers:" + response.getHeaders() + "\n"); |
| 120 | + sb.append("==============\n"); |
| 121 | + log.debug(sb.toString()); |
| 122 | + assertEquals(response.getStatusCode(), 500, "Should have 500 status code"); |
| 123 | + assertTrue(response.getHeader("X-Exception").contains("invalid.chunk.length"), "Should have failed due to chunking"); |
| 124 | + fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + response.getHeader("X-Exception")); |
| 125 | + } else { |
| 126 | + assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES); |
| 127 | + } |
141 | 128 | }
|
142 | 129 | }
|
0 commit comments