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

Dropbox: use URITemplate to build URL

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