Commit 9d15334f authored by Romain Courteaud's avatar Romain Courteaud

Dropbox: use URITemplate to build URL

parent 77a8fbc0
...@@ -23,8 +23,11 @@ ...@@ -23,8 +23,11 @@
remote_template = UriTemplate.parse(REMOVE_URL), remote_template = UriTemplate.parse(REMOVE_URL),
GET_URL = "https://content.dropboxapi.com/1/files" + GET_URL = "https://content.dropboxapi.com/1/files" +
"{/root,id}{+name}{?access_token}", "{/root,id}{+name}{?access_token}",
get_template = UriTemplate.parse(GET_URL); get_template = UriTemplate.parse(GET_URL),
//LIST_URL = 'https://api.dropboxapi.com/1/metadata/sandbox/'; //LIST_URL = 'https://api.dropboxapi.com/1/metadata/sandbox/';
METADATA_URL = "https://api.dropboxapi.com/1/metadata" +
"{/root}{+id}{?access_token}",
metadata_template = UriTemplate.parse(METADATA_URL);
function restrictDocumentId(id) { function restrictDocumentId(id) {
if (id.indexOf("/") !== 0) { if (id.indexOf("/") !== 0) {
...@@ -86,6 +89,7 @@ ...@@ -86,6 +89,7 @@
.push(undefined, function (err) { .push(undefined, function (err) {
if ((err.target !== undefined) && if ((err.target !== undefined) &&
(err.target.status === 405)) { (err.target.status === 405)) {
// Directory already exists, no need to fail
return; return;
} }
throw err; throw err;
...@@ -115,10 +119,12 @@ ...@@ -115,10 +119,12 @@
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ return jIO.util.ajax({
"type": "GET", type: "GET",
"url": "https://api.dropboxapi.com/1/metadata/" + url: metadata_template.expand({
that._root + "/" + id + access_token: that._access_token,
'?access_token=' + that._access_token root: that._root,
id: id
})
}); });
}) })
.push(function (evt) { .push(function (evt) {
...@@ -144,10 +150,12 @@ ...@@ -144,10 +150,12 @@
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ return jIO.util.ajax({
"type": "GET", type: "GET",
"url": "https://api.dropboxapi.com/1/metadata/" + url: metadata_template.expand({
that._root + "/" + id + access_token: that._access_token,
'?access_token=' + that._access_token root: that._root,
id: id
})
}); });
}) })
.push(function (evt) { .push(function (evt) {
...@@ -250,6 +258,7 @@ ...@@ -250,6 +258,7 @@
throw new jIO.util.jIOError("Cannot find attachment: " + throw new jIO.util.jIOError("Cannot find attachment: " +
id + ", " + name, 404); id + ", " + name, 404);
} }
throw error;
}); });
}; };
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
deepEqual = QUnit.deepEqual, deepEqual = QUnit.deepEqual,
equal = QUnit.equal, equal = QUnit.equal,
module = QUnit.module, module = QUnit.module,
throws = QUnit.throws,
token = "sample_token"; token = "sample_token";
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -21,10 +22,30 @@ ...@@ -21,10 +22,30 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "dropbox", type: "dropbox",
access_token: token, access_token: token,
root : "dropbox" root : "sandbox"
}); });
equal(jio.__type, "dropbox"); equal(jio.__type, "dropbox");
deepEqual(jio.__storage._access_token, token); deepEqual(jio.__storage._access_token, token);
deepEqual(jio.__storage._root, "sandbox");
});
test("reject invalid root", function () {
throws(
function () {
jIO.createJIO({
type: "dropbox",
access_token: token,
root : "foobar"
});
},
function (error) {
ok(error instanceof TypeError);
equal(error.message,
"root must be 'dropbox' or 'sandbox'");
return true;
}
);
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -301,11 +322,6 @@ ...@@ -301,11 +322,6 @@
}); });
test("get inexistent document", function () { test("get inexistent document", function () {
var url_get = "https://api.dropboxapi.com/1/metadata/dropbox" +
"//inexistent/?access_token=" + token;
this.server.respondWith("GET", url_get, [404, {
"Content-Type": "text/xml"
}, '']);
stop(); stop();
expect(3); expect(3);
...@@ -323,9 +339,9 @@ ...@@ -323,9 +339,9 @@
}); });
}); });
test("get document", function () { test("get directory", function () {
var url = "https://api.dropboxapi.com/1/metadata/dropbox" + var url = "https://api.dropboxapi.com/1/metadata/dropbox" +
"//id1/?access_token=" + token; "/id1/?access_token=" + token;
this.server.respondWith("GET", url, [200, { this.server.respondWith("GET", url, [200, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
}, '{"is_dir": true, "contents": []}' }, '{"is_dir": true, "contents": []}'
...@@ -345,6 +361,30 @@ ...@@ -345,6 +361,30 @@
}); });
}); });
test("get file", function () {
var url = "https://api.dropboxapi.com/1/metadata/dropbox" +
"/id1/?access_token=" + token;
this.server.respondWith("GET", url, [200, {
"Content-Type": "text/xml"
}, '{"is_dir": false, "contents": []}'
]);
stop();
expect(3);
this.jio.get("/id1/")
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Not a directory: /id1/");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// DropboxStorage.allAttachments // DropboxStorage.allAttachments
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -403,12 +443,31 @@ ...@@ -403,12 +443,31 @@
}); });
}); });
test("get inexistent document", function () { test("get file", function () {
var url = "/inexistent/"; var url = "https://api.dropboxapi.com/1/metadata/dropbox" +
this.server.respondWith("PROPFIND", url, [404, { "/id1/?access_token=" + token;
"Content-Type": "text/html" this.server.respondWith("GET", url, [200, {
}, "foo"]); "Content-Type": "text/xml"
}, '{"is_dir": false, "contents": []}'
]);
stop();
expect(3);
this.jio.allAttachments("/id1/")
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Not a directory: /id1/");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get inexistent document", function () {
stop(); stop();
expect(3); expect(3);
...@@ -428,7 +487,7 @@ ...@@ -428,7 +487,7 @@
test("get document without attachment", function () { test("get document without attachment", function () {
var url = "https://api.dropboxapi.com/1/metadata/dropbox" + var url = "https://api.dropboxapi.com/1/metadata/dropbox" +
"//id1/?access_token=" + token; "/id1/?access_token=" + token;
this.server.respondWith("GET", url, [200, { this.server.respondWith("GET", url, [200, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
}, '{"is_dir": true, "contents": []}' }, '{"is_dir": true, "contents": []}'
...@@ -450,7 +509,7 @@ ...@@ -450,7 +509,7 @@
test("get document with attachment", function () { test("get document with attachment", function () {
var url = "https://api.dropboxapi.com/1/metadata/dropbox" + var url = "https://api.dropboxapi.com/1/metadata/dropbox" +
"//id1/?access_token=" + token; "/id1/?access_token=" + token;
this.server.respondWith("GET", url, [200, { this.server.respondWith("GET", url, [200, {
"Content-Type": "text/xml" "Content-Type": "text/xml"
}, '{"is_dir": true, "path": "/id1", ' + }, '{"is_dir": true, "path": "/id1", ' +
...@@ -737,13 +796,6 @@ ...@@ -737,13 +796,6 @@
}); });
test("remove inexistent attachment", function () { test("remove inexistent attachment", function () {
var url_get_id = "https://api.dropboxapi.com/1/metadata/dropbox" +
"//removeAttachment1/attachment1?access_token=" + token;
this.server.respondWith("GET", url_get_id, [404, {
"Content-Type": "text/xml"
}, '']);
stop(); stop();
expect(3); expect(3);
...@@ -890,12 +942,6 @@ ...@@ -890,12 +942,6 @@
}); });
test("get inexistent attachment", function () { test("get inexistent attachment", function () {
var url = "https://content.dropboxapi.com/1/files/dropbox//" +
"getAttachment1/attachment1?access_token=" + token;
this.server.respondWith("GET", url, [404, {
"Content-Type": "text/xml"
}, ""]);
stop(); stop();
expect(3); expect(3);
......
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