1
0
mirror of https://github.com/etesync/android synced 2024-11-22 16:08:13 +00:00

Add basic support for cookies

* add basic support for cookies (doesn't work for URLs with ports: https://code.google.com/p/android/issues/detail?id=193475)
* MemorizingTrustManager: log reason for inaccessible key store files
This commit is contained in:
Ricki Hirner 2015-11-08 18:51:19 +01:00
parent 243dac9952
commit 2414b42867
No known key found for this signature in database
GPG Key ID: C4A212CF0B2B4566
4 changed files with 45 additions and 9 deletions

1
.gitmodules vendored
View File

@ -10,3 +10,4 @@
[submodule "MemorizingTrustManager"] [submodule "MemorizingTrustManager"]
path = MemorizingTrustManager path = MemorizingTrustManager
url = https://github.com/ge0rg/MemorizingTrustManager url = https://github.com/ge0rg/MemorizingTrustManager
ignore = dirty

@ -1 +1 @@
Subproject commit 9e30ffdf7dc12744ab069d584febdc6a4ca0de7e Subproject commit b6a3d558e4b78cd9ad5e8ad5246e44f04c854137

View File

@ -11,29 +11,28 @@ package at.bitfire.davdroid;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.squareup.okhttp.ConnectionSpec; import com.squareup.okhttp.ConnectionSpec;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.TlsVersion; import com.squareup.okhttp.TlsVersion;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer; import com.squareup.okhttp.mockwebserver.MockWebServer;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections; import java.util.Collections;
public class HttpClientTest extends InstrumentationTestCase { public class HttpClientTest extends InstrumentationTestCase {
final MockWebServer server = new MockWebServer(); MockWebServer server;
HttpClient httpClient; HttpClient httpClient;
@Override @Override
public void setUp() throws IOException { public void setUp() throws IOException {
httpClient = new HttpClient(null, getInstrumentation().getTargetContext().getApplicationContext()); httpClient = new HttpClient(null, getInstrumentation().getTargetContext().getApplicationContext());
httpClient.setConnectionSpecs(Collections.singletonList(new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) server = new MockWebServer();
.tlsVersions(TlsVersion.TLS_1_2)
.build()));
server.start(); server.start();
server.useHttps(new SSLSocketFactoryCompat(null), false);
assertEquals("https", server.url("/").scheme());
} }
@Override @Override
@ -41,7 +40,38 @@ public class HttpClientTest extends InstrumentationTestCase {
server.shutdown(); server.shutdown();
} }
public void testCookies() throws IOException, InterruptedException, URISyntaxException {
HttpUrl url = server.url("/");
// set cookie in first response
server.enqueue(new MockResponse()
.setResponseCode(200)
.setHeader("Set-Cookie", "theme=light; path=/")
.setBody("Cookie set"));
httpClient.newCall(new Request.Builder()
.get().url(url)
.build()).execute();
assertNull(server.takeRequest().getHeader("Cookie"));
// cookie should be sent with second request
server.enqueue(new MockResponse()
.setResponseCode(200));
httpClient.newCall(new Request.Builder()
.get().url(url)
.build()).execute();
//assertEquals("theme=light", server.takeRequest().getHeader("Cookie"));
// doesn't work for URLs with ports, see https://code.google.com/p/android/issues/detail?id=193475
}
public void testTLSVersion() throws IOException { public void testTLSVersion() throws IOException {
server.useHttps(new SSLSocketFactoryCompat(null), false);
assertEquals("https", server.url("/").scheme());
httpClient.setConnectionSpecs(Collections.singletonList(new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.build()));
// FIXME // FIXME
/*server.enqueue(new MockResponse().setResponseCode(204)); /*server.enqueue(new MockResponse().setResponseCode(204));
Response response = httpClient.newCall(new Request.Builder() Response response = httpClient.newCall(new Request.Builder()

View File

@ -24,6 +24,7 @@ import org.slf4j.Logger;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.net.CookieManager;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -68,6 +69,10 @@ public class HttpClient extends OkHttpClient {
// add User-Agent to every request // add User-Agent to every request
networkInterceptors().add(userAgentInterceptor); networkInterceptors().add(userAgentInterceptor);
// add cookie store for non-persistent cookies (some services like Horde use cookies for session tracking)
CookieManager cookies = new CookieManager();
setCookieHandler(cookies);
// enable verbose logs, if requested // enable verbose logs, if requested
if (this.log.isTraceEnabled()) { if (this.log.isTraceEnabled()) {
HttpLoggingInterceptor logger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { HttpLoggingInterceptor logger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {