Commit 9180f8b3 authored by Ricki Hirner's avatar Ricki Hirner

Basic multi-status parsing

parent 79b4c494
/build
# Created by https://www.gitignore.io
### Android ###
# Built application files
*.apk
*.ap_
# Files for the Dalvik VM
*.dex
# Java class files
*.class
# Generated files
bin/
gen/
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
*.iml
## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:
# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries
# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml
# Gradle:
# .idea/gradle.xml
# .idea/libraries
# Mongo Explorer plugin:
# .idea/mongoSettings.xml
## File-based project format:
*.ipr
*.iws
## Plugin-specific files:
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
### Gradle ###
.gradle
build/
# Ignore Gradle GUI config
gradle-app.setting
### external libs ###
.svn
......@@ -21,6 +21,7 @@ android {
dependencies {
compile 'com.squareup.okhttp:okhttp:2.6.0-SNAPSHOT'
compile 'com.squareup.okhttp:logging-interceptor:2.6.0-SNAPSHOT'
provided 'org.projectlombok:lombok:1.16.6'
androidTestCompile 'com.squareup.okhttp:mockwebserver:2.6.0-SNAPSHOT'
......
......@@ -7,11 +7,12 @@ import com.squareup.okhttp.mockwebserver.MockWebServer;
import junit.framework.TestCase;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import at.bitfire.dav4android.properties.DisplayName;
import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.HttpException;
import at.bitfire.dav4android.exception.InvalidDavResponseException;
import at.bitfire.dav4android.property.DisplayName;
public class DavResourceTest extends TestCase {
......@@ -20,7 +21,7 @@ public class DavResourceTest extends TestCase {
@Override
public void setUp() throws IOException {
mockServer.start(Constants.PORT);
mockServer.start(2540);
}
@Override
......@@ -29,13 +30,120 @@ public class DavResourceTest extends TestCase {
}
public void testPropfind() throws XmlPullParserException, IOException, HttpException {
public void testPropfindAndMultiStatus() throws IOException, HttpException, DavException {
HttpUrl url = mockServer.url("/dav");
DavResource dav = new DavResource(httpClient, url);
/*** NEGATIVE TESTS ***/
// test for non-Multi-Status responses:
// * 500 Internal Server Error
mockServer.enqueue(new MockResponse().setResponseCode(500));
try {
dav.propfind(DisplayName.NAME);
fail("Expected HttpException");
} catch(HttpException e) {
}
// * 200 OK (instead of 207 Multi-Status)
mockServer.enqueue(new MockResponse().setResponseCode(200));
try {
dav.propfind(DisplayName.NAME);
fail("Expected InvalidDavResponseException");
} catch(InvalidDavResponseException e) {
}
// test for invalid Multi-Status responses:
// * non-XML response
mockServer.enqueue(new MockResponse()
.setResponseCode(404));
.setResponseCode(207)
.setHeader("Content-Type", "text/html")
.setBody("<html></html>"));
try {
dav.propfind(DisplayName.NAME);
fail("Expected InvalidDavResponseException");
} catch(InvalidDavResponseException e) {
}
DavResource dav = new DavResource(httpClient, url);
// * malformed XML response
mockServer.enqueue(new MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")
.setBody("<malformed-xml>"));
try {
dav.propfind(DisplayName.NAME);
fail("Expected InvalidDavResponseException");
} catch(InvalidDavResponseException e) {
}
// * response without <multistatus> root element
mockServer.enqueue(new MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")
.setBody("<test></test>"));
try {
dav.propfind(DisplayName.NAME);
fail("Expected InvalidDavResponseException");
} catch(InvalidDavResponseException e) {
}
// multi-status response with <response>/<status> element indicating failure
mockServer.enqueue(new MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")
.setBody("<multistatus xmlns='DAV:'>" +
" <response>" +
" <href>/dav</href>" +
" <status>HTTP/1.1 403 Forbidden</status>" +
" </response>" +
"</multistatus>"));
try {
dav.propfind(DisplayName.NAME);
fail("Expected HttpException");
} catch(HttpException e) {
}
/*** POSITIVE TESTS ***/
// multi-status response without <response> elements
mockServer.enqueue(new MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")
.setBody("<multistatus xmlns='DAV:'></multistatus>"));
dav.propfind(DisplayName.NAME);
// TODO assert zero members and no changed properties
// multi-status response with <response>/<status> element indicating success
mockServer.enqueue(new MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")
.setBody("<multistatus xmlns='DAV:'>" +
" <response>" +
" <href>/dav</href>" +
" <status>HTTP/1.1 200 OK</status>" +
" </response>" +
"</multistatus>"));
dav.propfind(DisplayName.NAME);
// TODO assert zero members and no changed properties
// multi-status response with <response>/<propstat> element
mockServer.enqueue(new MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")
.setBody("<multistatus xmlns='DAV:'>" +
" <response>" +
" <href>/dav</href>" +
" <propstat>" +
" <prop>" +
" <displayname>My DAV Collection</displayname>" +
" </prop>" +
" <status>HTTP/1.1 200 OK</status>" +
" </propstat>" +
" </response>" +
"</multistatus>"));
dav.propfind(DisplayName.NAME);
// TODO assert zero members, but changed properties
// TODO hrefs with :, @ etc.
}
}
......@@ -2,6 +2,6 @@ package at.bitfire.dav4android;
public class Constants {
final static int PORT = 2541;
static final String LOG_TAG = "dav4android";
}
......@@ -9,6 +9,9 @@
package at.bitfire.dav4android;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
import java.util.logging.Level;
import lombok.extern.slf4j.Slf4j;
......@@ -17,7 +20,9 @@ public class HttpClient extends OkHttpClient {
public HttpClient() {
super();
// TODO add logging
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
networkInterceptors().add(logging);
}
}
......@@ -12,9 +12,6 @@ import lombok.RequiredArgsConstructor;
public interface Property {
String NS_WEBDAV = "DAV:";
@RequiredArgsConstructor
class Name {
public final String namespace;
......
package at.bitfire.dav4android;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import lombok.SneakyThrows;
public class XmlUtils {
public static final String NS_WEBDAV = "DAV:";
private static final XmlPullParserFactory factory;
static {
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
} catch (XmlPullParserException e) {
throw new RuntimeException("Couldn't create XmlPullParserFactory", e);
}
}
@SneakyThrows(XmlPullParserException.class)
public static XmlPullParser newPullParser() {
return factory.newPullParser();
}
}
package at.bitfire.dav4android.exception;
public class DavException extends Exception {
public DavException(String message) {
super(message);
}
public DavException(String message, Throwable throwable) {
super(message, throwable);
}
}
......@@ -6,7 +6,7 @@
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android;
package at.bitfire.dav4android.exception;
import lombok.RequiredArgsConstructor;
......
package at.bitfire.dav4android.exception;
public class InvalidDavResponseException extends DavException {
public InvalidDavResponseException(String message) {
super(message);
}
public InvalidDavResponseException(String message, Throwable throwable) {
super(message, throwable);
}
}
package at.bitfire.dav4android.exception;
public class UnsupportedDavException extends DavException {
public UnsupportedDavException(String message) {
super(message);
}
}
......@@ -6,12 +6,13 @@
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android.properties;
package at.bitfire.dav4android.property;
import at.bitfire.dav4android.Property;
import at.bitfire.dav4android.XmlUtils;
public class DisplayName implements Property {
public static final Name NAME = new Name(NS_WEBDAV, "displayname");
public static final Name NAME = new Name(XmlUtils.NS_WEBDAV, "displayname");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment