Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 64 additions & 2 deletions library/src/androidTest/java/com/qiniu/android/FormUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Assert.assertNotNull(resp);
}

@SmallTest
public void test0Data() throws Throwable {
final String expectKey = "你好;\"\r\n\r\n\r\n";
Map<String, String> params = new HashMap<String, String>();
params.put("x:foo", "fooval");
final UploadOptions opt = new UploadOptions(params, null, true, null, null);

uploadManager.put("".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
info = rinfo;
resp = response;
signal.countDown();
}
}, opt);

try {
signal.await(10, TimeUnit.SECONDS); // wait for callback
Assert.assertNotNull("timeout", info);
} catch (InterruptedException e) {
e.printStackTrace();
}
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
Assert.assertEquals(info.toString(), expectKey, key);
Assert.assertFalse(info.toString(), info.isOK());
Assert.assertEquals(info.toString(), "", info.reqId);
Assert.assertNull(resp);
}

@SmallTest
public void testNoKey() throws Throwable {
final String expectKey = null;
Expand Down Expand Up @@ -159,7 +189,7 @@ public void testNoToken() throws Throwable {
final String expectKey = "你好";
runTestOnUiThread(new Runnable() { // THIS IS THE KEY TO SUCCESS
public void run() {
uploadManager.put(new byte[0], expectKey, null, new UpCompletionHandler() {
uploadManager.put(new byte[1], expectKey, null, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
Expand Down Expand Up @@ -187,7 +217,7 @@ public void testEmptyToken() throws Throwable {
final String expectKey = "你好";
runTestOnUiThread(new Runnable() { // THIS IS THE KEY TO SUCCESS
public void run() {
uploadManager.put(new byte[0], expectKey, "", new UpCompletionHandler() {
uploadManager.put(new byte[1], expectKey, "", new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
Expand Down Expand Up @@ -242,6 +272,38 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
TempFile.remove(f);
}

@MediumTest
public void test0File() throws Throwable {
final String expectKey = "世/界";
final File f = TempFile.createFile(0);
Map<String, String> params = new HashMap<String, String>();
params.put("x:foo", "fooval");
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
uploadManager.put(f, expectKey, TestConfig.token, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
info = rinfo;
resp = response;
signal.countDown();
}
}, opt);

try {
signal.await(10, TimeUnit.SECONDS); // wait for callback
Assert.assertNotNull("timeout", info);
} catch (InterruptedException e) {
e.printStackTrace();
}
Assert.assertEquals(f.toString(), 0, f.length());
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
Assert.assertEquals(info.toString(), expectKey, key);
Assert.assertFalse(info.toString(), info.isOK());
Assert.assertEquals(info.toString(), "", info.reqId);
Assert.assertNull(resp);
TempFile.remove(f);
}

@SmallTest
public void testNoComplete() {
Exception error = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* 定义HTTP请求的日志信息和常规方法
*/
public final class ResponseInfo {
public static final int ZeroSizeFile = -6;
public static final int InvalidToken = -5;
public static final int InvalidArgument = -4;
public static final int InvalidFile = -3;
Expand Down Expand Up @@ -96,6 +97,9 @@ public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, Stri
this.sent = sent;
}

public static ResponseInfo zeroSize(){
return new ResponseInfo(ZeroSizeFile, "", "", "", "", "", "", -1, 0, 0, "file or data size is zero");
}
public static ResponseInfo cancelled() {
return new ResponseInfo(Cancelled, "", "", "", "", "", "", -1, 0, 0, "cancelled by user");
}
Expand Down
12 changes: 10 additions & 2 deletions library/src/main/java/com/qiniu/android/storage/UploadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,24 @@ private static boolean areInvalidArg(final String key, byte[] data, File f,
} else if (token == null || token.equals("")) {
message = "no token";
}

ResponseInfo info = null;
if (message != null) {
final ResponseInfo info = ResponseInfo.invalidArgument(message);
info = ResponseInfo.invalidArgument(message);
} if ((f!=null && f.length() == 0) || (data != null && data.length == 0)){
info = ResponseInfo.zeroSize();
}
if (info != null){
final ResponseInfo info2 = info;
AsyncRun.run(new Runnable() {
@Override
public void run() {
completionHandler.complete(key, info, null);
completionHandler.complete(key, info2, null);
}
});
return true;
}

return false;
}

Expand Down