Skip to content

Commit d312ea9

Browse files
committed
Merge pull request #146 from longbai/forbid_0_size
禁止0字节大小文件上传
2 parents a0d4b76 + 5549ff5 commit d312ea9

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,36 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
6363
Assert.assertNotNull(resp);
6464
}
6565

66+
@SmallTest
67+
public void test0Data() throws Throwable {
68+
final String expectKey = "你好;\"\r\n\r\n\r\n";
69+
Map<String, String> params = new HashMap<String, String>();
70+
params.put("x:foo", "fooval");
71+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
72+
73+
uploadManager.put("".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
74+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
75+
Log.i("qiniutest", k + rinfo);
76+
key = k;
77+
info = rinfo;
78+
resp = response;
79+
signal.countDown();
80+
}
81+
}, opt);
82+
83+
try {
84+
signal.await(10, TimeUnit.SECONDS); // wait for callback
85+
Assert.assertNotNull("timeout", info);
86+
} catch (InterruptedException e) {
87+
e.printStackTrace();
88+
}
89+
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
90+
Assert.assertEquals(info.toString(), expectKey, key);
91+
Assert.assertFalse(info.toString(), info.isOK());
92+
Assert.assertEquals(info.toString(), "", info.reqId);
93+
Assert.assertNull(resp);
94+
}
95+
6696
@SmallTest
6797
public void testNoKey() throws Throwable {
6898
final String expectKey = null;
@@ -159,7 +189,7 @@ public void testNoToken() throws Throwable {
159189
final String expectKey = "你好";
160190
runTestOnUiThread(new Runnable() { // THIS IS THE KEY TO SUCCESS
161191
public void run() {
162-
uploadManager.put(new byte[0], expectKey, null, new UpCompletionHandler() {
192+
uploadManager.put(new byte[1], expectKey, null, new UpCompletionHandler() {
163193
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
164194
Log.i("qiniutest", k + rinfo);
165195
key = k;
@@ -187,7 +217,7 @@ public void testEmptyToken() throws Throwable {
187217
final String expectKey = "你好";
188218
runTestOnUiThread(new Runnable() { // THIS IS THE KEY TO SUCCESS
189219
public void run() {
190-
uploadManager.put(new byte[0], expectKey, "", new UpCompletionHandler() {
220+
uploadManager.put(new byte[1], expectKey, "", new UpCompletionHandler() {
191221
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
192222
Log.i("qiniutest", k + rinfo);
193223
key = k;
@@ -242,6 +272,38 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
242272
TempFile.remove(f);
243273
}
244274

275+
@MediumTest
276+
public void test0File() throws Throwable {
277+
final String expectKey = "世/界";
278+
final File f = TempFile.createFile(0);
279+
Map<String, String> params = new HashMap<String, String>();
280+
params.put("x:foo", "fooval");
281+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
282+
uploadManager.put(f, expectKey, TestConfig.token, new UpCompletionHandler() {
283+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
284+
Log.i("qiniutest", k + rinfo);
285+
key = k;
286+
info = rinfo;
287+
resp = response;
288+
signal.countDown();
289+
}
290+
}, opt);
291+
292+
try {
293+
signal.await(10, TimeUnit.SECONDS); // wait for callback
294+
Assert.assertNotNull("timeout", info);
295+
} catch (InterruptedException e) {
296+
e.printStackTrace();
297+
}
298+
Assert.assertEquals(f.toString(), 0, f.length());
299+
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
300+
Assert.assertEquals(info.toString(), expectKey, key);
301+
Assert.assertFalse(info.toString(), info.isOK());
302+
Assert.assertEquals(info.toString(), "", info.reqId);
303+
Assert.assertNull(resp);
304+
TempFile.remove(f);
305+
}
306+
245307
@SmallTest
246308
public void testNoComplete() {
247309
Exception error = null;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 定义HTTP请求的日志信息和常规方法
1010
*/
1111
public final class ResponseInfo {
12+
public static final int ZeroSizeFile = -6;
1213
public static final int InvalidToken = -5;
1314
public static final int InvalidArgument = -4;
1415
public static final int InvalidFile = -3;
@@ -96,6 +97,9 @@ public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, Stri
9697
this.sent = sent;
9798
}
9899

100+
public static ResponseInfo zeroSize(){
101+
return new ResponseInfo(ZeroSizeFile, "", "", "", "", "", "", -1, 0, 0, "file or data size is zero");
102+
}
99103
public static ResponseInfo cancelled() {
100104
return new ResponseInfo(Cancelled, "", "", "", "", "", "", -1, 0, 0, "cancelled by user");
101105
}

library/src/main/java/com/qiniu/android/storage/UploadManager.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,24 @@ private static boolean areInvalidArg(final String key, byte[] data, File f,
4848
} else if (token == null || token.equals("")) {
4949
message = "no token";
5050
}
51+
52+
ResponseInfo info = null;
5153
if (message != null) {
52-
final ResponseInfo info = ResponseInfo.invalidArgument(message);
54+
info = ResponseInfo.invalidArgument(message);
55+
} if ((f!=null && f.length() == 0) || (data != null && data.length == 0)){
56+
info = ResponseInfo.zeroSize();
57+
}
58+
if (info != null){
59+
final ResponseInfo info2 = info;
5360
AsyncRun.run(new Runnable() {
5461
@Override
5562
public void run() {
56-
completionHandler.complete(key, info, null);
63+
completionHandler.complete(key, info2, null);
5764
}
5865
});
5966
return true;
6067
}
68+
6169
return false;
6270
}
6371

0 commit comments

Comments
 (0)