1
+ /*
2
+ * Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
3
+ *
4
+ * This program is licensed to you under the Apache License Version 2.0,
5
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
6
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7
+ *
8
+ * Unless required by applicable law or agreed to in writing,
9
+ * software distributed under the Apache License Version 2.0 is distributed on an
10
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12
+ */
13
+
14
+ package com .ning .http .client .async .netty ;
15
+
16
+ import com .ning .http .client .*;
17
+ import com .ning .http .client .async .AbstractBasicTest ;
18
+ import com .ning .http .client .async .ChunkingTest ;
19
+ import com .ning .http .client .async .ProviderUtil ;
20
+ import com .ning .http .client .providers .netty .FeedableBodyGenerator ;
21
+ import org .eclipse .jetty .server .handler .AbstractHandler ;
22
+ import org .testng .Assert ;
23
+ import org .testng .annotations .Test ;
24
+
25
+ import javax .servlet .ServletException ;
26
+ import javax .servlet .ServletInputStream ;
27
+ import javax .servlet .http .HttpServletRequest ;
28
+ import javax .servlet .http .HttpServletResponse ;
29
+ import java .io .File ;
30
+ import java .io .FileInputStream ;
31
+ import java .io .IOException ;
32
+ import java .net .URL ;
33
+ import java .nio .ByteBuffer ;
34
+ import java .nio .channels .FileChannel ;
35
+
36
+ import static org .testng .FileAssert .fail ;
37
+
38
+ public class NettyFeedableBodyGeneratorTest extends AbstractBasicTest {
39
+
40
+ @ Override
41
+ public AsyncHttpClient getAsyncHttpClient (AsyncHttpClientConfig config ) {
42
+ return ProviderUtil .nettyProvider (config );
43
+ }
44
+
45
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = true )
46
+ public void testPutImageFile () throws Exception {
47
+ File largeFile = getTestFile ();
48
+ final FileChannel fileChannel = new FileInputStream (largeFile ).getChannel ();
49
+
50
+ AsyncHttpClientConfig config = new AsyncHttpClientConfig .Builder ().setRequestTimeoutInMs (100 * 6000 ).build ();
51
+ AsyncHttpClient client = getAsyncHttpClient (config );
52
+ final FeedableBodyGenerator bodyGenerator = new FeedableBodyGenerator ();
53
+
54
+ try {
55
+ RequestBuilder builder = new RequestBuilder ("PUT" )
56
+ .setUrl (getTargetUrl ())
57
+ .setBody (bodyGenerator );
58
+
59
+ ListenableFuture <Response > listenableFuture = client .executeRequest (builder .build ());
60
+
61
+ boolean repeat = true ;
62
+ while (repeat ) {
63
+ final ByteBuffer buffer = ByteBuffer .allocate (1024 );
64
+ if (fileChannel .read (buffer ) > 0 ) {
65
+ buffer .flip ();
66
+ bodyGenerator .feed (buffer , false );
67
+ } else {
68
+ repeat = false ;
69
+ }
70
+ }
71
+ ByteBuffer emptyBuffer = ByteBuffer .wrap (new byte [0 ]);
72
+ bodyGenerator .feed (emptyBuffer , true );
73
+
74
+ Response response = listenableFuture .get ();
75
+ Assert .assertEquals (200 , response .getStatusCode ());
76
+ Assert .assertEquals ("" + largeFile .length (), response .getHeader ("X-TRANSFERRED" ));
77
+ } finally {
78
+ fileChannel .close ();
79
+ client .close ();
80
+ }
81
+ }
82
+
83
+ private static File getTestFile () {
84
+ String testResource1 = "300k.png" ;
85
+
86
+ File testResource1File = null ;
87
+ try {
88
+ ClassLoader cl = ChunkingTest .class .getClassLoader ();
89
+ URL url = cl .getResource (testResource1 );
90
+ testResource1File = new File (url .toURI ());
91
+ } catch (Throwable e ) {
92
+ // TODO Auto-generated catch block
93
+ fail ("unable to find " + testResource1 );
94
+ }
95
+
96
+ return testResource1File ;
97
+ }
98
+
99
+ @ Override
100
+ public AbstractHandler configureHandler () throws Exception {
101
+ return new AbstractHandler () {
102
+
103
+ public void handle (String arg0 , org .eclipse .jetty .server .Request arg1 , HttpServletRequest req , HttpServletResponse resp ) throws IOException , ServletException {
104
+
105
+ ServletInputStream in = req .getInputStream ();
106
+ byte [] b = new byte [8192 ];
107
+
108
+ int count = -1 ;
109
+ int total = 0 ;
110
+ while ((count = in .read (b )) != -1 ) {
111
+ b = new byte [8192 ];
112
+ total += count ;
113
+ }
114
+ System .err .println ("consumed " + total + " bytes." );
115
+
116
+ resp .setStatus (200 );
117
+ resp .addHeader ("X-TRANSFERRED" , String .valueOf (total ));
118
+ resp .getOutputStream ().flush ();
119
+ resp .getOutputStream ().close ();
120
+
121
+ arg1 .setHandled (true );
122
+
123
+ }
124
+ };
125
+ }
126
+
127
+
128
+ }
0 commit comments