From 18c08bc9ddd15d7eb6afe2be8fda37b6ac42095c Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Fri, 12 Jun 2015 00:41:21 +0200 Subject: [PATCH] Don't disable per-session cookie management + test (closes #525) --- .../davdroid/webdav/DavHttpClientTest.java | 74 +++++++++++++++++++ .../robohydra/plugins/dav/index.js | 22 ++++++ .../androidTest/robohydra/plugins/headdav.js | 2 + .../androidTest/robohydra/plugins/simple.js | 2 + .../davdroid/webdav/DavHttpClient.java | 7 +- 5 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 app/src/androidTest/java/at/bitfire/davdroid/webdav/DavHttpClientTest.java diff --git a/app/src/androidTest/java/at/bitfire/davdroid/webdav/DavHttpClientTest.java b/app/src/androidTest/java/at/bitfire/davdroid/webdav/DavHttpClientTest.java new file mode 100644 index 00000000..4a222fc7 --- /dev/null +++ b/app/src/androidTest/java/at/bitfire/davdroid/webdav/DavHttpClientTest.java @@ -0,0 +1,74 @@ +/* + * Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering). + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ + +package at.bitfire.davdroid.webdav; + +import android.test.InstrumentationTestCase; + +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGetHC4; +import org.apache.http.client.methods.HttpPostHC4; +import org.apache.http.client.methods.HttpRequestBaseHC4; +import org.apache.http.impl.client.CloseableHttpClient; + +import java.io.IOException; +import java.net.URI; + +import at.bitfire.davdroid.TestConstants; +import lombok.Cleanup; + +public class DavHttpClientTest extends InstrumentationTestCase { + final static URI testCookieURI = TestConstants.roboHydra.resolve("/dav/testCookieStore"); + + CloseableHttpClient httpClient; + + @Override + protected void setUp() throws Exception { + httpClient = DavHttpClient.create(); + } + + @Override + protected void tearDown() throws Exception { + httpClient.close(); + } + + + public void testCookies() throws IOException { + CloseableHttpResponse response = null; + + HttpGetHC4 get = new HttpGetHC4(testCookieURI); + get.setHeader("Accept", "text/xml"); + + // at first, DavHttpClient doesn't send a cookie + try { + response = httpClient.execute(get); + assertEquals(412, response.getStatusLine().getStatusCode()); + } finally { + if (response != null) + response.close(); + } + + // POST sets a cookie to DavHttpClient + try { + response = httpClient.execute(new HttpPostHC4(testCookieURI)); + assertEquals(200, response.getStatusLine().getStatusCode()); + } finally { + if (response != null) + response.close(); + } + + // and now DavHttpClient sends a cookie for GET, too + try { + response = httpClient.execute(get); + assertEquals(200, response.getStatusLine().getStatusCode()); + } finally { + if (response != null) + response.close(); + } + } +} diff --git a/app/src/androidTest/robohydra/plugins/dav/index.js b/app/src/androidTest/robohydra/plugins/dav/index.js index 40b208ee..5a836258 100644 --- a/app/src/androidTest/robohydra/plugins/dav/index.js +++ b/app/src/androidTest/robohydra/plugins/dav/index.js @@ -1,3 +1,5 @@ +// vim: ts=4:sw=4 + var roboHydraHeadDAV = require("../headdav"); exports.getBodyParts = function(conf) { @@ -6,6 +8,26 @@ exports.getBodyParts = function(conf) { /* base URL, provide default DAV here */ new RoboHydraHeadDAV({ path: "/dav/" }), + /* test cookie: + * POST /dav/testCookieStore will cause the mock server to set a cookie + * GET /dav/testCookieStore will cause the mock server to check the request cookie + * and return 412 Precondition failed when it's not set correctly + */ + new RoboHydraHeadDAV({ + path: "/dav/testCookieStore", + handler: function(req,res,next) { + var cookie = 'sess=MY_SESSION_12345'; + if (req.method == "POST") { + res.statusCode = 200; + res.headers['Set-Cookie'] = cookie; + res.send("Setting cookie"); + } else { + res.statusCode = (req.headers['cookie'] == cookie) ? 200 : 412; + res.send("Checking cookie"); + } + } + }), + /* multistatus parsing */ new RoboHydraHeadDAV({ path: "/dav/collection-response-with-trailing-slash", diff --git a/app/src/androidTest/robohydra/plugins/headdav.js b/app/src/androidTest/robohydra/plugins/headdav.js index 613a2678..9cb1ff3c 100644 --- a/app/src/androidTest/robohydra/plugins/headdav.js +++ b/app/src/androidTest/robohydra/plugins/headdav.js @@ -1,3 +1,5 @@ +// vim: ts=4:sw=4 + var roboHydra = require("robohydra"), roboHydraHeads = roboHydra.heads, roboHydraHead = roboHydraHeads.RoboHydraHead; diff --git a/app/src/androidTest/robohydra/plugins/simple.js b/app/src/androidTest/robohydra/plugins/simple.js index 3d506ee2..3af7f727 100644 --- a/app/src/androidTest/robohydra/plugins/simple.js +++ b/app/src/androidTest/robohydra/plugins/simple.js @@ -1,3 +1,5 @@ +// vim: ts=4:sw=4 + var roboHydra = require("robohydra"), roboHydraHeads = roboHydra.heads, roboHydraHead = roboHydraHeads.RoboHydraHead; diff --git a/app/src/main/java/at/bitfire/davdroid/webdav/DavHttpClient.java b/app/src/main/java/at/bitfire/davdroid/webdav/DavHttpClient.java index eb289bc6..c102928d 100644 --- a/app/src/main/java/at/bitfire/davdroid/webdav/DavHttpClient.java +++ b/app/src/main/java/at/bitfire/davdroid/webdav/DavHttpClient.java @@ -7,6 +7,7 @@ */ package at.bitfire.davdroid.webdav; +import android.annotation.SuppressLint; import android.util.Log; import org.apache.http.client.config.RequestConfig; @@ -43,7 +44,8 @@ public class DavHttpClient { } - public static CloseableHttpClient create() { + @SuppressLint("LogTagMismatch") + public static CloseableHttpClient create() { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); // limits per DavHttpClient (= per DavSyncAdapter extends AbstractThreadedSyncAdapter) connectionManager.setMaxTotal(3); // max. 3 connections in total @@ -55,8 +57,7 @@ public class DavHttpClient { .setDefaultRequestConfig(defaultRqConfig) .setRetryHandler(DavHttpRequestRetryHandler.INSTANCE) .setRedirectStrategy(DavRedirectStrategy.INSTANCE) - .setUserAgent("DAVdroid/" + Constants.APP_VERSION) - .disableCookieManagement(); + .setUserAgent("DAVdroid/" + Constants.APP_VERSION); if (Log.isLoggable("Wire", Log.DEBUG)) { Log.i(TAG, "Wire logging active, disabling HTTP compression");