Skip to content

Commit cc96e16

Browse files
committed
form sync
1 parent 0c99635 commit cc96e16

File tree

7 files changed

+471
-6
lines changed

7 files changed

+471
-6
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
package com.qiniu.android;
2+
3+
import android.test.InstrumentationTestCase;
4+
import android.test.suitebuilder.annotation.MediumTest;
5+
import android.test.suitebuilder.annotation.SmallTest;
6+
7+
import com.qiniu.android.common.FixedZone;
8+
import com.qiniu.android.common.ServiceAddress;
9+
import com.qiniu.android.common.Zone;
10+
import com.qiniu.android.http.ResponseInfo;
11+
import com.qiniu.android.storage.Configuration;
12+
import com.qiniu.android.storage.UploadManager;
13+
import com.qiniu.android.storage.UploadOptions;
14+
import com.qiniu.android.utils.Etag;
15+
16+
import junit.framework.Assert;
17+
18+
import org.json.JSONObject;
19+
20+
import java.io.File;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
public class SyncFormUploadTest extends InstrumentationTestCase {
25+
private UploadManager uploadManager;
26+
private volatile String key;
27+
private volatile ResponseInfo info;
28+
private volatile JSONObject resp;
29+
30+
public void setUp() throws Exception {
31+
uploadManager = new UploadManager();
32+
}
33+
34+
@SmallTest
35+
public void testHello() throws Throwable {
36+
final String expectKey = "你好;\"\r\n\r\n\r\n";
37+
Map<String, String> params = new HashMap<String, String>();
38+
params.put("x:foo", "fooval");
39+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
40+
byte[] b = "hello".getBytes();
41+
info = uploadManager.put(b, expectKey, TestConfig.token, opt);
42+
resp = info.response;
43+
44+
Assert.assertTrue(info.toString(), info.isOK());
45+
Assert.assertNotNull(info.reqId);
46+
Assert.assertNotNull(resp);
47+
48+
String hash = resp.optString("hash");
49+
Assert.assertEquals(hash, Etag.data(b));
50+
Assert.assertEquals(expectKey, key = resp.optString("key"));
51+
}
52+
53+
@SmallTest
54+
public void test0Data() throws Throwable {
55+
final String expectKey = "你好;\"\r\n\r\n\r\n";
56+
Map<String, String> params = new HashMap<String, String>();
57+
params.put("x:foo", "fooval");
58+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
59+
60+
info = uploadManager.put("".getBytes(), expectKey, TestConfig.token, opt);
61+
resp = info.response;
62+
63+
// key = resp.optString("key");
64+
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
65+
// Assert.assertEquals(info.toString(), expectKey, key);
66+
Assert.assertFalse(info.toString(), info.isOK());
67+
Assert.assertEquals(info.toString(), "", info.reqId);
68+
Assert.assertNull(resp);
69+
}
70+
71+
@SmallTest
72+
public void testNoKey() throws Throwable {
73+
final String expectKey = null;
74+
Map<String, String> params = new HashMap<String, String>();
75+
params.put("x:foo", "fooval");
76+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
77+
info = uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, opt);
78+
79+
resp = info.response;
80+
key = resp.optString("key");
81+
Assert.assertTrue(info.toString(), info.isOK());
82+
83+
Assert.assertNotNull(info.reqId);
84+
Assert.assertNotNull(resp);
85+
Assert.assertEquals("Fqr0xh3cxeii2r7eDztILNmuqUNN", resp.optString("key", ""));
86+
}
87+
88+
@SmallTest
89+
public void testInvalidToken() throws Throwable {
90+
final String expectKey = "你好";
91+
info = uploadManager.put("hello".getBytes(), expectKey, "invalid", null);
92+
93+
resp = info.response;
94+
// key = resp.optString("key");
95+
// Assert.assertEquals(info.toString(), expectKey, key);
96+
Assert.assertEquals(info.toString(), ResponseInfo.InvalidToken, info.statusCode);
97+
Assert.assertNotNull(info.reqId);
98+
Assert.assertNull(resp);
99+
}
100+
101+
@SmallTest
102+
public void testNoData() throws Throwable {
103+
final String expectKey = "你好";
104+
105+
info = uploadManager.put((byte[]) null, expectKey, "invalid", null);
106+
107+
resp = info.response;
108+
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument,
109+
info.statusCode);
110+
Assert.assertNull(resp);
111+
}
112+
113+
@SmallTest
114+
public void testNoToken() throws Throwable {
115+
final String expectKey = "你好";
116+
info = uploadManager.put(new byte[1], expectKey, null, null);
117+
118+
resp = info.response;
119+
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument, info.statusCode);
120+
Assert.assertNull(resp);
121+
}
122+
123+
@SmallTest
124+
public void testEmptyToken() throws Throwable {
125+
final String expectKey = "你好";
126+
info = uploadManager.put(new byte[1], expectKey, "", null);
127+
128+
resp = info.response;
129+
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument,
130+
info.statusCode);
131+
Assert.assertNull(resp);
132+
}
133+
134+
@MediumTest
135+
public void testFile() throws Throwable {
136+
final String expectKey = "世/界";
137+
final File f = TempFile.createFile(1);
138+
Map<String, String> params = new HashMap<String, String>();
139+
params.put("x:foo", "fooval");
140+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
141+
info = uploadManager.put(f, expectKey, TestConfig.token, opt);
142+
143+
resp = info.response;
144+
key = resp.optString("key");
145+
Assert.assertEquals(info.toString(), expectKey, key);
146+
Assert.assertTrue(info.toString(), info.isOK());
147+
//上传策略含空格 \"fname\":\" $(fname) \"
148+
Assert.assertEquals(f.getName(), resp.optString("fname", "res doesn't include the FNAME").trim());
149+
Assert.assertNotNull(info.reqId);
150+
Assert.assertNotNull(resp);
151+
String hash = resp.getString("hash");
152+
Assert.assertEquals(hash, Etag.file(f));
153+
TempFile.remove(f);
154+
}
155+
156+
@MediumTest
157+
public void test0File() throws Throwable {
158+
final String expectKey = "世/界";
159+
final File f = TempFile.createFile(0);
160+
Map<String, String> params = new HashMap<String, String>();
161+
params.put("x:foo", "fooval");
162+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
163+
info = uploadManager.put(f, expectKey, TestConfig.token, opt);
164+
165+
resp = info.response;
166+
Assert.assertEquals(f.toString(), 0, f.length());
167+
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
168+
Assert.assertNull(resp);
169+
Assert.assertFalse(info.toString(), info.isOK());
170+
Assert.assertEquals(info.toString(), "", info.reqId);
171+
TempFile.remove(f);
172+
}
173+
174+
@SmallTest
175+
public void testNoComplete() {
176+
info = uploadManager.put(new byte[0], null, null, null);
177+
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
178+
179+
info = uploadManager.put("", null, null, null);
180+
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
181+
}
182+
183+
@SmallTest
184+
public void testIpBack() throws Throwable {
185+
ServiceAddress s = new ServiceAddress("http://upwelcome.qiniu.com", Zone.zone0.upHost("").backupIps);
186+
Zone z = new FixedZone(s, Zone.zone0.upHostBackup(""));
187+
Configuration c = new Configuration.Builder()
188+
.zone(z)
189+
.build();
190+
191+
UploadManager uploadManager2 = new UploadManager(c);
192+
final String expectKey = "你好;\"\r\n\r\n\r\n";
193+
Map<String, String> params = new HashMap<String, String>();
194+
params.put("x:foo", "fooval");
195+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
196+
197+
info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt);
198+
199+
Assert.assertTrue(info.toString(), info.isOK());
200+
Assert.assertNotNull(info.reqId);
201+
resp = info.response;
202+
Assert.assertNotNull(resp);
203+
key = resp.optString("key");
204+
Assert.assertEquals(info.toString(), expectKey, key);
205+
}
206+
207+
@SmallTest
208+
public void testPortBackup() throws Throwable {
209+
ServiceAddress s = new ServiceAddress("http://upload.qiniu.com:9999", null);
210+
Zone z = new FixedZone(s, Zone.zone0.upHostBackup(""));
211+
Configuration c = new Configuration.Builder()
212+
.zone(z)
213+
.build();
214+
UploadManager uploadManager2 = new UploadManager(c);
215+
final String expectKey = "你好;\"\r\n\r\n\r\n";
216+
Map<String, String> params = new HashMap<String, String>();
217+
params.put("x:foo", "fooval");
218+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
219+
220+
info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt);
221+
222+
Assert.assertTrue(info.toString(), info.isOK());
223+
Assert.assertNotNull(info.reqId);
224+
resp = info.response;
225+
Assert.assertNotNull(resp);
226+
key = resp.optString("key");
227+
Assert.assertEquals(info.toString(), expectKey, key);
228+
}
229+
230+
@SmallTest
231+
public void testDnsHijacking() throws Throwable {
232+
ServiceAddress s = new ServiceAddress("http://uphijacktest.qiniu.com", Zone.zone0.upHost("").backupIps);
233+
Zone z = new FixedZone(s, Zone.zone0.upHostBackup(""));
234+
Configuration c = new Configuration.Builder()
235+
.zone(z)
236+
.build();
237+
UploadManager uploadManager2 = new UploadManager(c);
238+
final String expectKey = "你好;\"\r\n\r\n\r\n";
239+
Map<String, String> params = new HashMap<String, String>();
240+
params.put("x:foo", "fooval");
241+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
242+
243+
info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt);
244+
245+
resp = info.response;
246+
Assert.assertTrue(info.toString(), info.isOK());
247+
Assert.assertNotNull(info.reqId);
248+
Assert.assertNotNull(resp);
249+
}
250+
251+
@SmallTest
252+
public void testHttps() throws Throwable {
253+
254+
final String expectKey = "你好;\"\r\n\r\n\r\n";
255+
Map<String, String> params = new HashMap<String, String>();
256+
params.put("x:foo", "fooval");
257+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
258+
ServiceAddress s = new ServiceAddress("https://up.qbox.me", null);
259+
Zone z = new FixedZone(s, Zone.zone0.upHostBackup(""));
260+
Configuration c = new Configuration.Builder()
261+
.zone(z)
262+
.build();
263+
UploadManager uploadManager2 = new UploadManager(c);
264+
info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt);
265+
266+
resp = info.response;
267+
key = resp.optString("key");
268+
Assert.assertEquals(info.toString(), expectKey, key);
269+
Assert.assertTrue(info.toString(), info.isOK());
270+
Assert.assertNotNull(info.reqId);
271+
Assert.assertNotNull(resp);
272+
}
273+
}

library/src/androidTest/java/com/qiniu/android/TestFileRecorder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.qiniu.android.storage.UploadOptions;
1313
import com.qiniu.android.storage.persistent.FileRecorder;
1414
import com.qiniu.android.utils.Etag;
15-
import com.qiniu.android.utils.UrlSafeBase64;
1615

1716
import junit.framework.Assert;
1817

library/src/main/AndroidManifest.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
<manifest
2-
package="com.qiniu.android"
1+
<manifest package="com.qiniu.android"
32
xmlns:android="http://schemas.android.com/apk/res/android">
43

5-
<uses-permission android:name="android.permission.INTERNET"/>
6-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
7-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
4+
<uses-permission android:name="android.permission.INTERNET" />
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
87

98
<application android:label="@string/app_name"></application>
109

library/src/main/java/com/qiniu/android/http/Client.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.qiniu.android.dns.DnsManager;
55
import com.qiniu.android.dns.Domain;
66
import com.qiniu.android.storage.UpCancellationSignal;
7+
import com.qiniu.android.storage.UpToken;
78
import com.qiniu.android.utils.AsyncRun;
89
import com.qiniu.android.utils.StringMap;
910
import com.qiniu.android.utils.StringUtils;
@@ -347,6 +348,77 @@ public void accept(String key, Object value) {
347348
return buildResponseInfo(res, tag.ip, tag.duration);
348349
}
349350

351+
public ResponseInfo syncMultipartPost(String url,
352+
PostArgs args,
353+
final UpToken upToken) {
354+
RequestBody file;
355+
if (args.file != null) {
356+
file = RequestBody.create(MediaType.parse(args.mimeType), args.file);
357+
} else {
358+
file = RequestBody.create(MediaType.parse(args.mimeType), args.data);
359+
}
360+
return syncMultipartPost(url, args.params, upToken, args.fileName, file);
361+
}
362+
363+
private ResponseInfo syncMultipartPost(String url,
364+
StringMap fields,
365+
final UpToken upToken,
366+
String fileName,
367+
RequestBody file) {
368+
final MultipartBody.Builder mb = new MultipartBody.Builder();
369+
mb.addFormDataPart("file", fileName, file);
370+
371+
fields.forEach(new StringMap.Consumer() {
372+
@Override
373+
public void accept(String key, Object value) {
374+
mb.addFormDataPart(key, value.toString());
375+
}
376+
});
377+
mb.setType(MediaType.parse("multipart/form-data"));
378+
RequestBody body = mb.build();
379+
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
380+
return syncSend(requestBuilder, null, upToken);
381+
}
382+
383+
public ResponseInfo syncSend(final Request.Builder requestBuilder, StringMap headers, final UpToken upToken) {
384+
if (headers != null) {
385+
headers.forEach(new StringMap.Consumer() {
386+
@Override
387+
public void accept(String key, Object value) {
388+
requestBuilder.header(key, value.toString());
389+
}
390+
});
391+
}
392+
393+
requestBuilder.header("User-Agent", UserAgent.instance().getUa(upToken.accessKey));
394+
final ResponseTag tag = new ResponseTag();
395+
Request req = null;
396+
try {
397+
req = requestBuilder.tag(tag).build();
398+
okhttp3.Response response = httpClient.newCall(req).execute();
399+
return buildResponseInfo(response, tag.ip, tag.duration);
400+
} catch (Exception e) {
401+
e.printStackTrace();
402+
int statusCode = NetworkError;
403+
String msg = e.getMessage();
404+
if (e instanceof UnknownHostException) {
405+
statusCode = ResponseInfo.UnknownHost;
406+
} else if (msg != null && msg.indexOf("Broken pipe") == 0) {
407+
statusCode = ResponseInfo.NetworkConnectionLost;
408+
} else if (e instanceof SocketTimeoutException) {
409+
statusCode = ResponseInfo.TimedOut;
410+
} else if (e instanceof java.net.ConnectException) {
411+
statusCode = ResponseInfo.CannotConnectToHost;
412+
}
413+
414+
HttpUrl u = req.url();
415+
return new ResponseInfo(null, statusCode, "", "", "", u.host(), u.encodedPath(), "", u.port(), 0, 0, e.getMessage());
416+
// TODO
417+
// return ResponseInfo.create(null, statusCode, "", "", "", u.host(),
418+
// u.encodedPath(), "", u.port(), tag.duration, 0, e.getMessage(), upToken);
419+
}
420+
}
421+
350422
private static class ResponseTag {
351423
public String ip = "";
352424
public long duration = 0;

library/src/main/java/com/qiniu/android/http/ResponseInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public final class ResponseInfo {
1818
public static final int Cancelled = -2;
1919
public static final int NetworkError = -1;
2020

21+
public static final int UnknownError = 0;
22+
2123
// <-- error code copy from ios
2224
public static final int TimedOut = -1001;
2325
public static final int UnknownHost = -1003;

0 commit comments

Comments
 (0)