|
15 | 15 | import com.ning.http.client.AsyncHttpClient;
|
16 | 16 | import com.ning.http.client.FluentCaseInsensitiveStringsMap;
|
17 | 17 | import com.ning.http.client.Response;
|
| 18 | +import com.ning.http.client.generators.FileBodyGenerator; |
18 | 19 | import com.ning.http.client.listener.TransferCompletionHandler;
|
19 | 20 | import com.ning.http.client.listener.TransferListener;
|
20 | 21 | import org.eclipse.jetty.server.handler.AbstractHandler;
|
@@ -190,6 +191,66 @@ public void onThrowable(Throwable t) {
|
190 | 191 | c.close();
|
191 | 192 | }
|
192 | 193 |
|
| 194 | + @Test(groups = {"standalone", "default_provider"}) |
| 195 | + public void basicPutBodyTest() throws Throwable { |
| 196 | + AsyncHttpClient c = new AsyncHttpClient(); |
| 197 | + |
| 198 | + final AtomicReference<Throwable> throwable = new AtomicReference<Throwable>(); |
| 199 | + final AtomicReference<FluentCaseInsensitiveStringsMap> hSent = new AtomicReference<FluentCaseInsensitiveStringsMap>(); |
| 200 | + final AtomicReference<FluentCaseInsensitiveStringsMap> hRead = new AtomicReference<FluentCaseInsensitiveStringsMap>(); |
| 201 | + final AtomicInteger bbReceivedLenght = new AtomicInteger(0); |
| 202 | + final AtomicInteger bbSentLenght = new AtomicInteger(0); |
| 203 | + |
| 204 | + final AtomicBoolean completed = new AtomicBoolean(false); |
| 205 | + |
| 206 | + byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes("UTF-16"); |
| 207 | + long repeats = (1024 * 100 * 10 / bytes.length) + 1; |
| 208 | + File largeFile = createTempFile(bytes, (int) repeats); |
| 209 | + |
| 210 | + TransferCompletionHandler tl = new TransferCompletionHandler(); |
| 211 | + tl.addTransferListener(new TransferListener() { |
| 212 | + |
| 213 | + public void onRequestHeadersSent(FluentCaseInsensitiveStringsMap headers) { |
| 214 | + hSent.set(headers); |
| 215 | + } |
| 216 | + |
| 217 | + public void onResponseHeadersReceived(FluentCaseInsensitiveStringsMap headers) { |
| 218 | + hRead.set(headers); |
| 219 | + } |
| 220 | + |
| 221 | + public void onBytesReceived(ByteBuffer buffer) { |
| 222 | + bbReceivedLenght.addAndGet(buffer.capacity()); |
| 223 | + } |
| 224 | + |
| 225 | + public void onBytesSent(ByteBuffer buffer) { |
| 226 | + bbSentLenght.addAndGet(buffer.capacity()); |
| 227 | + } |
| 228 | + |
| 229 | + public void onRequestResponseCompleted() { |
| 230 | + completed.set(true); |
| 231 | + } |
| 232 | + |
| 233 | + public void onThrowable(Throwable t) { |
| 234 | + throwable.set(t); |
| 235 | + } |
| 236 | + }); |
| 237 | + |
| 238 | + try { |
| 239 | + Response response = c.preparePut(getTargetUrl()).setBody(new FileBodyGenerator(largeFile)) |
| 240 | + .execute(tl).get(); |
| 241 | + |
| 242 | + assertNotNull(response); |
| 243 | + assertEquals(response.getStatusCode(), 200); |
| 244 | + assertNotNull(hRead.get()); |
| 245 | + assertNotNull(hSent.get()); |
| 246 | + assertEquals(bbReceivedLenght.get(), largeFile.length()); |
| 247 | + assertEquals(bbSentLenght.get(), largeFile.length()); |
| 248 | + } catch (IOException ex) { |
| 249 | + fail("Should have timed out"); |
| 250 | + } |
| 251 | + c.close(); |
| 252 | + } |
| 253 | + |
193 | 254 | public String getTargetUrl() {
|
194 | 255 | return String.format("http://127.0.0.1:%d/foo/test", port1);
|
195 | 256 | }
|
|
0 commit comments