Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to OKHttp #3163

Merged
merged 17 commits into from
Jun 19, 2019
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
Binary file removed collect_app/libs/httpclientandroidlib-4.5.2-2.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.android.http.CollectServerClient;
import org.odk.collect.android.http.CollectThenSystemContentTypeMapper;
import org.odk.collect.android.http.HttpClientConnection;
import org.odk.collect.android.http.HttpGetResult;
import org.odk.collect.android.http.OpenRosaHttpInterface;
import org.odk.collect.android.utilities.WebCredentialsUtils;

import java.io.BufferedReader;
Expand All @@ -21,6 +20,7 @@

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import static org.odk.collect.android.support.CollectHelpers.getAppDependencyComponent;

/**
* An on-device test for TLS server name indication support.
Expand All @@ -29,14 +29,16 @@
*/
@Suppress
@RunWith(AndroidJUnit4.class)
public class SNITest {
public class ServerNameIndicationTest {

private static final String SNI_URI = "https://sni.velox.ch/";
private static final String SUCCESS_SENTINEL = "sent the following TLS server name indication extension";

@Test
public void testThatHttpClientSupportsSNI() throws Exception {
CollectServerClient serverClient = new CollectServerClient(new HttpClientConnection(new CollectThenSystemContentTypeMapper()), new WebCredentialsUtils());
OpenRosaHttpInterface openRosaHttpInterface = getAppDependencyComponent().openRosaHttpInterface();
CollectServerClient serverClient = new CollectServerClient(openRosaHttpInterface, new WebCredentialsUtils());

HttpGetResult inputStreamResult = serverClient.getHttpInputStream(SNI_URI, null);
assertHttpSuccess(inputStreamResult.getStatusCode());
assertPageContent(inputStreamResult.getInputStream());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.odk.collect.android.support;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;

import org.odk.collect.android.application.Collect;
import org.odk.collect.android.injection.config.AppDependencyComponent;
import org.odk.collect.android.logic.FormController;

public final class CollectHelpers {
Expand All @@ -16,4 +21,10 @@ public static FormController waitForFormController() throws InterruptedException

return Collect.getInstance().getFormController();
}

public static AppDependencyComponent getAppDependencyComponent() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
Collect application = (Collect) context;
return application.getComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.odk.collect.android.tasks;

import android.net.Uri;

import androidx.test.rule.GrantPermissionRule;

import org.junit.After;
Expand All @@ -18,6 +19,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.odk.collect.android.test.TestUtils.assertMatches;
import static org.odk.collect.android.test.TestUtils.cleanUpTempFiles;
import static org.odk.collect.android.test.TestUtils.createTempFile;
Expand Down Expand Up @@ -63,7 +65,7 @@ public void shouldUploadAnInstance() throws Exception {
assertMatches("/submission\\?deviceID=\\w+%3A\\w+", r.getPath());
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent"));
assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
assertEquals("gzip,deflate", r.getHeader("Accept-Encoding"));
assertTrue(r.getHeader("Accept-Encoding").contains("gzip"));
}

// and
Expand All @@ -73,17 +75,9 @@ public void shouldUploadAnInstance() throws Exception {
assertMatches("/submission\\?deviceID=\\w+%3A\\w+", r.getPath());
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent"));
assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
assertEquals("gzip,deflate", r.getHeader("Accept-Encoding"));
assertTrue(r.getHeader("Accept-Encoding").contains("gzip"));
assertMatches("multipart/form-data; boundary=.*", r.getHeader("Content-Type"));
assertMatches(join(
"--.*\r",
"Content-Disposition: form-data; name=\"xml_submission_file\"; filename=\"tst.*\\.tmp\"\r",
"Content-Type: text/xml; charset=ISO-8859-1\r",
"Content-Transfer-Encoding: binary\r",
"\r",
"<form-content-here/>\r",
"--.*--\r"),
r.getBody().readUtf8());
assertTrue(r.getBody().readUtf8().contains("<form-content-here/>"));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.odk.collect.android.utilities;

import android.webkit.MimeTypeMap;

import org.junit.Before;
import org.junit.Test;
import org.odk.collect.android.http.CollectServerClient;
import org.odk.collect.android.http.CollectThenSystemContentTypeMapper;
import org.odk.collect.android.http.HttpClientConnection;
import org.odk.collect.android.http.OkHttpConnection;
import org.odk.collect.android.test.MockedServerTest;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;

Expand All @@ -22,7 +25,7 @@ public class CollectServerClientTest extends MockedServerTest {
public void setUp() throws Exception {
// server hangs without a response queued:
server.enqueue(new MockResponse());
collectServerClient = new CollectServerClient(new HttpClientConnection(new CollectThenSystemContentTypeMapper()), new WebCredentialsUtils());
collectServerClient = new CollectServerClient(new OkHttpConnection(null, new CollectThenSystemContentTypeMapper(MimeTypeMap.getSingleton())), new WebCredentialsUtils());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,65 @@
import androidx.annotation.NonNull;

import org.odk.collect.android.utilities.FileUtils;
import org.opendatakit.httpclientandroidlib.entity.ContentType;

public class CollectThenSystemContentTypeMapper implements OpenRosaHttpInterface.FileToContentTypeMapper {

private final MimeTypeMap androidTypeMap;

public CollectThenSystemContentTypeMapper(MimeTypeMap androidTypeMap) {
this.androidTypeMap = androidTypeMap;
}

@NonNull
@Override
public String map(String fileName) {
ContentType contentType = ContentTypeMapping.of(fileName);
if (contentType == null) {
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mime = mimeTypeMap.getMimeTypeFromExtension(FileUtils.getFileExtension(fileName));
if (mime != null) {
contentType = ContentType.create(mime);
} else {
contentType = ContentType.APPLICATION_OCTET_STREAM;
}
String extension = FileUtils.getFileExtension(fileName);

String collectContentType = CollectContentTypeMappings.of(extension);
String androidContentType = androidTypeMap.getMimeTypeFromExtension(extension);

if (collectContentType != null) {
return collectContentType;
} else if (androidContentType != null) {
return androidContentType;
} else {
return "application/octet-stream";
}
}

private enum CollectContentTypeMappings {
XML("xml", "text/xml"),
_3GPP("3gpp", "audio/3gpp"),
_3GP("3gp", "video/3gpp"),
AVI("avi", "video/avi"),
AMR("amr", "audio/amr"),
CSV("csv", "text/csv"),
JPG("jpg", "image/jpeg"),
MP3("mp3", "audio/mp3"),
MP4("mp4", "video/mp4"),
OGA("oga", "audio/ogg"),
OGG("ogg", "audio/ogg"),
OGV("ogv", "video/ogg"),
WAV("wav", "audio/wav"),
WEBM("webm", "video/webm"),
XLS("xls", "application/vnd.ms-excel");

private String extension;
private String contentType;

CollectContentTypeMappings(String extension, String contentType) {
this.extension = extension;
this.contentType = contentType;
}

return contentType.getMimeType();
public static String of(String extension) {
for (CollectContentTypeMappings m : CollectContentTypeMappings.values()) {
if (m.extension.equals(extension)) {
return m.contentType;
}
}

return null;
}
}
}

This file was deleted.

Loading