mirror of
https://github.com/etesync/android
synced 2025-03-24 19:35:42 +00:00
Send appropriate Accept/Content-Type headers + tests (closes #328)
This commit is contained in:
parent
ebe13cef5e
commit
428c09c390
@ -17,7 +17,7 @@ public class CardDavAddressBook extends RemoteCollection<Contact> {
|
||||
|
||||
@Override
|
||||
protected String memberContentType() {
|
||||
return "text/vcard";
|
||||
return Contact.MIME_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -61,6 +61,8 @@ import ezvcard.property.Url;
|
||||
public class Contact extends Resource {
|
||||
private final static String TAG = "davdroid.Contact";
|
||||
|
||||
public final static String MIME_TYPE = "text/vcard";
|
||||
|
||||
public final static String
|
||||
PROPERTY_STARRED = "X-DAVDROID-STARRED",
|
||||
PROPERTY_PHONETIC_FIRST_NAME = "X-PHONETIC-FIRST-NAME",
|
||||
|
@ -67,6 +67,8 @@ import at.bitfire.davdroid.syncadapter.DavSyncAdapter;
|
||||
public class Event extends Resource {
|
||||
private final static String TAG = "davdroid.Event";
|
||||
|
||||
public final static String MIME_TYPE = "text/calendar";
|
||||
|
||||
private final static TimeZoneRegistry tzRegistry = new DefaultTimeZoneRegistryFactory().createRegistry();
|
||||
|
||||
@Getter @Setter private String summary, location, description;
|
||||
|
@ -120,7 +120,15 @@ public abstract class RemoteCollection<T extends Resource> {
|
||||
|
||||
public Resource get(Resource resource) throws IOException, HttpException, DavException, InvalidResourceException {
|
||||
WebDavResource member = new WebDavResource(collection, resource.getName());
|
||||
member.get();
|
||||
|
||||
if (resource instanceof Contact)
|
||||
member.get(Contact.MIME_TYPE);
|
||||
else if (resource instanceof Event)
|
||||
member.get(Event.MIME_TYPE);
|
||||
else {
|
||||
Log.wtf(TAG, "Should fetch something, but neither contact nor calendar");
|
||||
throw new InvalidResourceException("Didn't now which MIME type to accept");
|
||||
}
|
||||
|
||||
byte[] data = member.getContent();
|
||||
if (data == null)
|
||||
|
@ -39,8 +39,6 @@ public class HttpPropfind extends HttpEntityEnclosingRequestBase {
|
||||
|
||||
HttpPropfind(URI uri, Mode mode) {
|
||||
this(uri);
|
||||
|
||||
setHeader("Content-Type", "text/xml; charset=UTF-8");
|
||||
|
||||
DavPropfind propfind = new DavPropfind();
|
||||
propfind.prop = new DavProp();
|
||||
@ -87,6 +85,8 @@ public class HttpPropfind extends HttpEntityEnclosingRequestBase {
|
||||
StringWriter writer = new StringWriter();
|
||||
serializer.write(propfind, writer);
|
||||
|
||||
setHeader("Content-Type", "text/xml; charset=UTF-8");
|
||||
setHeader("Accept", "text/xml");
|
||||
setHeader("Depth", String.valueOf(depth));
|
||||
setEntity(new StringEntity(writer.toString(), "UTF-8"));
|
||||
} catch(Exception ex) {
|
||||
|
@ -25,6 +25,7 @@ public class HttpReport extends HttpEntityEnclosingRequestBase {
|
||||
this(uri);
|
||||
|
||||
setHeader("Content-Type", "text/xml; charset=UTF-8");
|
||||
setHeader("Accept", "text/xml");
|
||||
setHeader("Depth", "0");
|
||||
|
||||
setEntity(new StringEntity(entity, "UTF-8"));
|
||||
|
@ -329,8 +329,10 @@ public class WebDavResource {
|
||||
|
||||
/* resource operations */
|
||||
|
||||
public void get() throws IOException, HttpException, DavException {
|
||||
public void get(String acceptedType) throws IOException, HttpException, DavException {
|
||||
HttpGet get = new HttpGet(location);
|
||||
get.addHeader("Accept", acceptedType);
|
||||
|
||||
CloseableHttpResponse response = httpClient.execute(get, context);
|
||||
try {
|
||||
checkResponse(response);
|
||||
|
@ -16,8 +16,14 @@ RoboHydraHeadDAV = roboHydraHeads.roboHydraHeadType({
|
||||
res.headers['DAV'] = 'addressbook, calendar-access';
|
||||
res.statusCode = 500;
|
||||
|
||||
// verify Accept header
|
||||
var accept = req.headers['accept'];
|
||||
if (req.method == "GET" && (accept == undefined || !accept.match(/text\/(calendar|vcard|xml)/)) ||
|
||||
(req.method == "PROPFIND" || req.method == "REPORT") && (accept == undefined || accept != "text/xml"))
|
||||
res.statusCode = 406;
|
||||
|
||||
// DAV operations that work on all URLs
|
||||
if (req.method == "OPTIONS") {
|
||||
else if (req.method == "OPTIONS") {
|
||||
res.statusCode = 204;
|
||||
res.headers['Allow'] = 'OPTIONS, PROPFIND, GET, PUT, DELETE, REPORT';
|
||||
|
||||
|
@ -145,7 +145,7 @@ public class WebDavResourceTest extends InstrumentationTestCase {
|
||||
}
|
||||
|
||||
public void testGet() throws URISyntaxException, IOException, HttpException, DavException {
|
||||
simpleFile.get();
|
||||
simpleFile.get("*/*");
|
||||
@Cleanup InputStream is = assetMgr.open("test.random", AssetManager.ACCESS_STREAMING);
|
||||
byte[] expected = IOUtils.toByteArray(is);
|
||||
assertTrue(Arrays.equals(expected, simpleFile.getContent()));
|
||||
@ -156,7 +156,7 @@ public class WebDavResourceTest extends InstrumentationTestCase {
|
||||
|
||||
boolean sniWorking = false;
|
||||
try {
|
||||
file.get();
|
||||
file.get("*/*");
|
||||
sniWorking = true;
|
||||
} catch (SSLPeerUnverifiedException e) {
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user