1212
1313import org .json .JSONObject ;
1414
15+ import java .net .URI ;
16+ import java .net .URISyntaxException ;
1517import java .util .concurrent .CountDownLatch ;
1618import java .util .concurrent .TimeUnit ;
1719
@@ -25,7 +27,15 @@ public class HttpTest extends InstrumentationTestCase {
2527 final CountDownLatch signal = new CountDownLatch (1 );
2628 private HttpManager httpManager ;
2729 private ResponseInfo info ;
28- private JSONObject resp ;
30+
31+ private static URI newURI (String s ) {
32+ try {
33+ return new URI (s );
34+ } catch (URISyntaxException e ) {
35+ e .printStackTrace ();
36+ }
37+ return null ;
38+ }
2939
3040 @ Override
3141 protected void setUp () throws Exception {
@@ -34,14 +44,14 @@ protected void setUp() throws Exception {
3444
3545 @ SmallTest
3646 public void testPost1 () throws Throwable {
37- httpManager .postData ("http://www.baidu.com" , "hello" .getBytes (), null , null , new CompletionHandler () {
47+ httpManager .postData (newURI ( "http://www.baidu.com" ) , "hello" .getBytes (), null , null , new CompletionHandler () {
3848 @ Override
3949 public void complete (ResponseInfo rinfo , JSONObject response ) {
4050 Log .d ("qiniutest" , rinfo .toString ());
4151 info = rinfo ;
4252 signal .countDown ();
4353 }
44- }, null , false );
54+ }, null );
4555
4656 try {
4757 signal .await (6000 , TimeUnit .SECONDS ); // wait for callback
@@ -54,14 +64,14 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
5464 @ SmallTest
5565 public void testPost2 () throws Throwable {
5666
57- httpManager .postData ("http://up.qiniu.com" , "hello" .getBytes (), null , null , new CompletionHandler () {
67+ httpManager .postData (newURI ( "http://up.qiniu.com" ) , "hello" .getBytes (), null , null , new CompletionHandler () {
5868 @ Override
5969 public void complete (ResponseInfo rinfo , JSONObject response ) {
6070 Log .d ("qiniutest" , rinfo .toString ());
6171 info = rinfo ;
6272 signal .countDown ();
6373 }
64- }, null , false );
74+ }, null );
6575
6676 try {
6777 signal .await (60 , TimeUnit .SECONDS ); // wait for callback
@@ -75,14 +85,15 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
7585 public void testPost3 () throws Throwable {
7686 runTestOnUiThread (new Runnable () { // THIS IS THE KEY TO SUCCESS
7787 public void run () {
78- httpManager .postData ("http://httpbin.org/status/500" , "hello" .getBytes (), null , null , new CompletionHandler () {
79- @ Override
80- public void complete (ResponseInfo rinfo , JSONObject response ) {
81- Log .d ("qiniutest" , rinfo .toString ());
82- info = rinfo ;
83- signal .countDown ();
84- }
85- }, null , false );
88+ httpManager .postData (newURI ("http://httpbin.org/status/500" ), "hello" .getBytes (),
89+ null , null , new CompletionHandler () {
90+ @ Override
91+ public void complete (ResponseInfo rinfo , JSONObject response ) {
92+ Log .d ("qiniutest" , rinfo .toString ());
93+ info = rinfo ;
94+ signal .countDown ();
95+ }
96+ }, null );
8697 }
8798 });
8899
@@ -99,14 +110,15 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
99110 public void testPost4 () throws Throwable {
100111 runTestOnUiThread (new Runnable () { // THIS IS THE KEY TO SUCCESS
101112 public void run () {
102- httpManager .postData ("http://httpbin.org/status/418" , "hello" .getBytes (), null , null , new CompletionHandler () {
103- @ Override
104- public void complete (ResponseInfo rinfo , JSONObject response ) {
105- Log .d ("qiniutest" , rinfo .toString ());
106- info = rinfo ;
107- signal .countDown ();
108- }
109- }, null , false );
113+ httpManager .postData (newURI ("http://httpbin.org/status/418" ), "hello" .getBytes (),
114+ null , null , new CompletionHandler () {
115+ @ Override
116+ public void complete (ResponseInfo rinfo , JSONObject response ) {
117+ Log .d ("qiniutest" , rinfo .toString ());
118+ info = rinfo ;
119+ signal .countDown ();
120+ }
121+ }, null );
110122 }
111123 });
112124
@@ -122,14 +134,15 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
122134 @ SmallTest
123135 public void testPostNoDomain () throws Throwable {
124136
125- httpManager .postData ("http://no-domain.qiniu.com" , "hello" .getBytes (), null , null , new CompletionHandler () {
126- @ Override
127- public void complete (ResponseInfo rinfo , JSONObject response ) {
128- Log .d ("qiniutest" , rinfo .toString ());
129- info = rinfo ;
130- signal .countDown ();
131- }
132- }, null , false );
137+ httpManager .postData (newURI ("http://no-domain.qiniu.com" ), "hello" .getBytes (),
138+ null , null , new CompletionHandler () {
139+ @ Override
140+ public void complete (ResponseInfo rinfo , JSONObject response ) {
141+ Log .d ("qiniutest" , rinfo .toString ());
142+ info = rinfo ;
143+ signal .countDown ();
144+ }
145+ }, null );
133146
134147 try {
135148 signal .await (60 , TimeUnit .SECONDS ); // wait for callback
@@ -143,14 +156,15 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
143156 @ SmallTest
144157 public void testPostNoPort () throws Throwable {
145158
146- httpManager .postData ("http://up.qiniu.com:12345" , "hello" .getBytes (), null , null , new CompletionHandler () {
147- @ Override
148- public void complete (ResponseInfo rinfo , JSONObject response ) {
149- Log .d ("qiniutest" , rinfo .toString ());
150- info = rinfo ;
151- signal .countDown ();
152- }
153- }, null , false );
159+ httpManager .postData (newURI ("http://up.qiniu.com:12345" ), "hello" .getBytes (),
160+ null , null , new CompletionHandler () {
161+ @ Override
162+ public void complete (ResponseInfo rinfo , JSONObject response ) {
163+ Log .d ("qiniutest" , rinfo .toString ());
164+ info = rinfo ;
165+ signal .countDown ();
166+ }
167+ }, null );
154168
155169 try {
156170 signal .await (60 , TimeUnit .SECONDS ); // wait for callback
@@ -164,14 +178,15 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
164178 @ SmallTest
165179 public void testPostIP () throws Throwable {
166180 Header [] x = {new BasicHeader ("Host" , "www.qiniu.com" )};
167- httpManager .postData ("http://183.136.139.12/" , "hello" .getBytes (), x , null , new CompletionHandler () {
168- @ Override
169- public void complete (ResponseInfo rinfo , JSONObject response ) {
170- Log .d ("qiniutest" , rinfo .toString ());
171- info = rinfo ;
172- signal .countDown ();
173- }
174- }, null , false );
181+ httpManager .postData (newURI ("http://183.136.139.12/" ), "hello" .getBytes (),
182+ x , null , new CompletionHandler () {
183+ @ Override
184+ public void complete (ResponseInfo rinfo , JSONObject response ) {
185+ Log .d ("qiniutest" , rinfo .toString ());
186+ info = rinfo ;
187+ signal .countDown ();
188+ }
189+ }, null );
175190
176191 try {
177192 signal .await (60 , TimeUnit .SECONDS ); // wait for callback
0 commit comments