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
273 changes: 273 additions & 0 deletions library/src/androidTest/java/com/qiniu/android/SyncFormUploadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
package com.qiniu.android;

import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;

import com.qiniu.android.common.FixedZone;
import com.qiniu.android.common.ServiceAddress;
import com.qiniu.android.common.Zone;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.qiniu.android.utils.Etag;

import junit.framework.Assert;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class SyncFormUploadTest extends InstrumentationTestCase {
private UploadManager uploadManager;
private volatile String key;
private volatile ResponseInfo info;
private volatile JSONObject resp;

public void setUp() throws Exception {
uploadManager = new UploadManager();
}

@SmallTest
public void testHello() 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);
byte[] b = "hello".getBytes();
info = uploadManager.syncPut(b, expectKey, TestConfig.token, opt);
resp = info.response;

Assert.assertTrue(info.toString(), info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);

String hash = resp.optString("hash");
Assert.assertEquals(hash, Etag.data(b));
Assert.assertEquals(expectKey, key = resp.optString("key"));
}

@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);

info = uploadManager.syncPut("".getBytes(), expectKey, TestConfig.token, opt);
resp = info.response;

// key = resp.optString("key");
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;
Map<String, String> params = new HashMap<String, String>();
params.put("x:foo", "fooval");
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
info = uploadManager.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);

resp = info.response;
key = resp.optString("key");
Assert.assertTrue(info.toString(), info.isOK());

Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
Assert.assertEquals("Fqr0xh3cxeii2r7eDztILNmuqUNN", resp.optString("key", ""));
}

@SmallTest
public void testInvalidToken() throws Throwable {
final String expectKey = "你好";
info = uploadManager.syncPut("hello".getBytes(), expectKey, "invalid", null);

resp = info.response;
// key = resp.optString("key");
// Assert.assertEquals(info.toString(), expectKey, key);
Assert.assertEquals(info.toString(), ResponseInfo.InvalidToken, info.statusCode);
Assert.assertNotNull(info.reqId);
Assert.assertNull(resp);
}

@SmallTest
public void testNoData() throws Throwable {
final String expectKey = "你好";

info = uploadManager.syncPut((byte[]) null, expectKey, "invalid", null);

resp = info.response;
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument,
info.statusCode);
Assert.assertNull(resp);
}

@SmallTest
public void testNoToken() throws Throwable {
final String expectKey = "你好";
info = uploadManager.syncPut(new byte[1], expectKey, null, null);

resp = info.response;
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument, info.statusCode);
Assert.assertNull(resp);
}

@SmallTest
public void testEmptyToken() throws Throwable {
final String expectKey = "你好";
info = uploadManager.syncPut(new byte[1], expectKey, "", null);

resp = info.response;
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument,
info.statusCode);
Assert.assertNull(resp);
}

@MediumTest
public void testFile() throws Throwable {
final String expectKey = "世/界";
final File f = TempFile.createFile(1);
Map<String, String> params = new HashMap<String, String>();
params.put("x:foo", "fooval");
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
info = uploadManager.syncPut(f, expectKey, TestConfig.token, opt);

resp = info.response;
key = resp.optString("key");
Assert.assertEquals(info.toString(), expectKey, key);
Assert.assertTrue(info.toString(), info.isOK());
//上传策略含空格 \"fname\":\" $(fname) \"
Assert.assertEquals(f.getName(), resp.optString("fname", "res doesn't include the FNAME").trim());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
String hash = resp.getString("hash");
Assert.assertEquals(hash, Etag.file(f));
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);
info = uploadManager.syncPut(f, expectKey, TestConfig.token, opt);

resp = info.response;
Assert.assertEquals(f.toString(), 0, f.length());
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
Assert.assertNull(resp);
Assert.assertFalse(info.toString(), info.isOK());
Assert.assertEquals(info.toString(), "", info.reqId);
TempFile.remove(f);
}

@SmallTest
public void testNoComplete() {
info = uploadManager.syncPut(new byte[0], null, null, null);
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);

info = uploadManager.syncPut("", null, null, null);
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
}

@SmallTest
public void testIpBack() throws Throwable {
ServiceAddress s = new ServiceAddress("http://upwelcome.qiniu.com", Zone.zone0.upHost("").backupIps);
Zone z = new FixedZone(s, Zone.zone0.upHostBackup(""));
Configuration c = new Configuration.Builder()
.zone(z)
.build();

UploadManager uploadManager2 = new UploadManager(c);
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);

info = uploadManager2.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);

Assert.assertTrue(info.toString(), info.isOK());
Assert.assertNotNull(info.reqId);
resp = info.response;
Assert.assertNotNull(resp);
key = resp.optString("key");
Assert.assertEquals(info.toString(), expectKey, key);
}

@SmallTest
public void testPortBackup() throws Throwable {
ServiceAddress s = new ServiceAddress("http://upload.qiniu.com:9999", null);
Zone z = new FixedZone(s, Zone.zone0.upHostBackup(""));
Configuration c = new Configuration.Builder()
.zone(z)
.build();
UploadManager uploadManager2 = new UploadManager(c);
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);

info = uploadManager2.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);

Assert.assertTrue(info.toString(), info.isOK());
Assert.assertNotNull(info.reqId);
resp = info.response;
Assert.assertNotNull(resp);
key = resp.optString("key");
Assert.assertEquals(info.toString(), expectKey, key);
}

@SmallTest
public void testDnsHijacking() throws Throwable {
ServiceAddress s = new ServiceAddress("http://uphijacktest.qiniu.com", Zone.zone0.upHost("").backupIps);
Zone z = new FixedZone(s, Zone.zone0.upHostBackup(""));
Configuration c = new Configuration.Builder()
.zone(z)
.build();
UploadManager uploadManager2 = new UploadManager(c);
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);

info = uploadManager2.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);

resp = info.response;
Assert.assertTrue(info.toString(), info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
}

@SmallTest
public void testHttps() 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);
ServiceAddress s = new ServiceAddress("https://up.qbox.me", null);
Zone z = new FixedZone(s, Zone.zone0.upHostBackup(""));
Configuration c = new Configuration.Builder()
.zone(z)
.build();
UploadManager uploadManager2 = new UploadManager(c);
info = uploadManager2.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);

resp = info.response;
key = resp.optString("key");
Assert.assertEquals(info.toString(), expectKey, key);
Assert.assertTrue(info.toString(), info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.qiniu.android.storage.UploadOptions;
import com.qiniu.android.storage.persistent.FileRecorder;
import com.qiniu.android.utils.Etag;
import com.qiniu.android.utils.UrlSafeBase64;

import junit.framework.Assert;

Expand Down
9 changes: 4 additions & 5 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<manifest
package="com.qiniu.android"
<manifest package="com.qiniu.android"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

Expand Down
69 changes: 69 additions & 0 deletions library/src/main/java/com/qiniu/android/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.qiniu.android.dns.DnsManager;
import com.qiniu.android.dns.Domain;
import com.qiniu.android.storage.UpCancellationSignal;
import com.qiniu.android.storage.UpToken;
import com.qiniu.android.utils.AsyncRun;
import com.qiniu.android.utils.StringMap;
import com.qiniu.android.utils.StringUtils;
Expand Down Expand Up @@ -347,6 +348,74 @@ public void accept(String key, Object value) {
return buildResponseInfo(res, tag.ip, tag.duration);
}

public ResponseInfo syncMultipartPost(String url,
PostArgs args,
final UpToken upToken) {
RequestBody file;
if (args.file != null) {
file = RequestBody.create(MediaType.parse(args.mimeType), args.file);
} else {
file = RequestBody.create(MediaType.parse(args.mimeType), args.data);
}
return syncMultipartPost(url, args.params, upToken, args.fileName, file);
}

private ResponseInfo syncMultipartPost(String url,
StringMap fields,
final UpToken upToken,
String fileName,
RequestBody file) {
final MultipartBody.Builder mb = new MultipartBody.Builder();
mb.addFormDataPart("file", fileName, file);

fields.forEach(new StringMap.Consumer() {
@Override
public void accept(String key, Object value) {
mb.addFormDataPart(key, value.toString());
}
});
mb.setType(MediaType.parse("multipart/form-data"));
RequestBody body = mb.build();
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
return syncSend(requestBuilder, null, upToken);
}

public ResponseInfo syncSend(final Request.Builder requestBuilder, StringMap headers, final UpToken upToken) {
if (headers != null) {
headers.forEach(new StringMap.Consumer() {
@Override
public void accept(String key, Object value) {
requestBuilder.header(key, value.toString());
}
});
}

requestBuilder.header("User-Agent", UserAgent.instance().getUa(upToken.accessKey));
final ResponseTag tag = new ResponseTag();
Request req = null;
try {
req = requestBuilder.tag(tag).build();
okhttp3.Response response = httpClient.newCall(req).execute();
return buildResponseInfo(response, tag.ip, tag.duration);
} catch (Exception e) {
e.printStackTrace();
int statusCode = NetworkError;
String msg = e.getMessage();
if (e instanceof UnknownHostException) {
statusCode = ResponseInfo.UnknownHost;
} else if (msg != null && msg.indexOf("Broken pipe") == 0) {
statusCode = ResponseInfo.NetworkConnectionLost;
} else if (e instanceof SocketTimeoutException) {
statusCode = ResponseInfo.TimedOut;
} else if (e instanceof java.net.ConnectException) {
statusCode = ResponseInfo.CannotConnectToHost;
}

HttpUrl u = req.url();
return new ResponseInfo(null, statusCode, "", "", "", u.host(), u.encodedPath(), "", u.port(), 0, 0, e.getMessage());
}
}

private static class ResponseTag {
public String ip = "";
public long duration = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public final class ResponseInfo {
public static final int Cancelled = -2;
public static final int NetworkError = -1;

public static final int UnknownError = 0;

// <-- error code copy from ios
public static final int TimedOut = -1001;
public static final int UnknownHost = -1003;
Expand Down Expand Up @@ -84,7 +86,7 @@ public final class ResponseInfo {
public final long sent;

/**
* hide, 内部使用
* 响应体,json 格式
*/
public final JSONObject response;

Expand Down
Loading