Don't disable per-session cookie management + test (closes #525)

This commit is contained in:
Ricki Hirner 2015-06-12 00:41:21 +02:00
parent 6ec0e6119f
commit 5bc2287b66
5 changed files with 104 additions and 3 deletions

View file

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

View file

@ -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",

View file

@ -1,3 +1,5 @@
// vim: ts=4:sw=4
var roboHydra = require("robohydra"),
roboHydraHeads = roboHydra.heads,
roboHydraHead = roboHydraHeads.RoboHydraHead;

View file

@ -1,3 +1,5 @@
// vim: ts=4:sw=4
var roboHydra = require("robohydra"),
roboHydraHeads = roboHydra.heads,
roboHydraHead = roboHydraHeads.RoboHydraHead;

View file

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