Commit 374c1b07 authored by Junming Liu's avatar Junming Liu

QiniuStorage : The latest version of QiniuStorage

This is the lastest QiniuStorage file. Because of Qiniu not support resource manage operations, so it's only have putAttachment and getAttachment.
The method to upload or download file are using URiTemplate via Ajax. Besides, i use CryptoJS to do HMAC-SHA1 algorithm.
parent cfea757e
...@@ -37,18 +37,18 @@ ...@@ -37,18 +37,18 @@
/////////////////////////// ///////////////////////////
// Memory storage // Memory storage
/////////////////////////// ///////////////////////////
return g.run({ // return g.run({
type: "query", // type: "query",
sub_storage: { // sub_storage: {
type: "uuid", // type: "uuid",
sub_storage: { // sub_storage: {
type: "union", // type: "union",
storage_list: [{ // storage_list: [{
type: "memory" // type: "memory"
}] // }]
} // }
} // }
}); // });
/////////////////////////// ///////////////////////////
// IndexedDB storage // IndexedDB storage
...@@ -85,18 +85,18 @@ ...@@ -85,18 +85,18 @@
/////////////////////////// ///////////////////////////
// Qiniu storage // Qiniu storage
/////////////////////////// ///////////////////////////
// return g.run({ return g.run({
// type: "query", type: "query",
// sub_storage: { sub_storage: {
// type: "uuid", type: "uuid",
// sub_storage: { sub_storage: {
// "type": "qiniu", "type": "qiniu",
// "bucket": "BUCKET", "bucket": "7xn150.com1.z0.glb.clouddn.com",
// "access_key": "ACCESSKEY", "access_key": "s90kGV3JYDDQPivaPVpwxrHMi9RCpncLgLctGDJQ",
// "secret_key": "SECRETKEY" "secret_key": "hfqndzXIfqP6aMpTOdgT_UjiUjARkiFXz98Cthjx"
// } }
// } }
// }); });
/////////////////////////// ///////////////////////////
// Replicate storage // Replicate storage
...@@ -159,7 +159,10 @@ ...@@ -159,7 +159,10 @@
deepEqual(doc, {"title": "I don't have ID éà&\n"}, deepEqual(doc, {"title": "I don't have ID éà&\n"},
"Document correctly fetched"); "Document correctly fetched");
// Remove the doc // Remove the doc
return jio.remove(doc_id); return jio.remove(doc_id)
.fail(function (error) {
console.log("remove error", error);
});
}) })
.then(function (doc_id) { .then(function (doc_id) {
ok(doc_id, "Document removed"); ok(doc_id, "Document removed");
...@@ -249,7 +252,6 @@ ...@@ -249,7 +252,6 @@
.then(function (result) { .then(function (result) {
equal(result.target.result, "fooé\nbar", "Attachment correctly fetched"); equal(result.target.result, "fooé\nbar", "Attachment correctly fetched");
return jio.get("test.txt"); return jio.get("test.txt");
}) })
.then(function (doc) { .then(function (doc) {
deepEqual(doc, {}, "Document correctly fetched"); deepEqual(doc, {}, "Document correctly fetched");
......
This diff is collapsed.
/*global define, module, RSVP, jIO, test, ok,
/*global define, module, test_util, RSVP, jIO, test, ok, deepEqual, sinon, expect, stop, start, Blob, equal, define, console */
deepEqual, sinon, expect, stop, start, Blob, equal, define */
/*jslint indent: 2 */ /*jslint indent: 2 */
(function (dependencies, module) { (function (dependencies, module) {
"use strict"; "use strict";
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
return define(dependencies, module); return define(dependencies, module);
} }
module(test_util, RSVP, jIO); module(RSVP, jIO);
}([ }([
'test_util',
'rsvp', 'rsvp',
'jio', 'jio',
'hmacsha1', 'hmacsha1',
...@@ -21,12 +19,12 @@ ...@@ -21,12 +19,12 @@
var qiniu_spec = { var qiniu_spec = {
"type": "qiniu", "type": "qiniu",
"bucket": "uth6nied", "bucket": "7xn150.com1.z0.glb.clouddn.com",
"access_key": "Imh9CFmpVZ5L1TE04Pjt-UmR_Ccr2cW9-KjSmvSA", "access_key": "s90kGV3JYDDQPivaPVpwxrHMi9RCpncLgLctGDJQ",
"secret_key": "vFkNUlI2U4B7G1sz8UL_Z25kYHozfz82z4vMWPgo" "secret_key": "hfqndzXIfqP6aMpTOdgT_UjiUjARkiFXz98Cthjx"
}; };
module("QiniuStorage", { module("QiniuStorage ", {
setup: function () { setup: function () {
this.server = sinon.fakeServer.create(); this.server = sinon.fakeServer.create();
...@@ -45,9 +43,12 @@ ...@@ -45,9 +43,12 @@
test('get', function () { test('get', function () {
var key = "foobar12345", var key = "foobar12345",
server = this.server, server = this.server,
download_url = 'http://uth6nied.u.qiniudn.com/foobar12345?' + /* download_url = 'http://uth6nied.u.qiniudn.com/foobar12345?' +
'e=2451491200&token=Imh9CFmpVZ5L1TE04Pjt-UmR_Ccr2cW9-KjSmvSA:' + 'e=2451491200&token=Imh9CFmpVZ5L1TE04Pjt-UmR_Ccr2cW9-KjSmvSA:' +
'hISFzrC4dQvdOR8A_MozNsB5cME=', 'hISFzrC4dQvdOR8A_MozNsB5cME=', */
download_url = 'http://7xn150.com1.z0.glb.clouddn.com/foobar12345?' +
'e=2451491200&token=s90kGV3JYDDQPivaPVpwxrHMi9RCpncLgLctGDJQ:' +
'lWIEfRXIf6tbwWjuX381DHTSxnU=',
data = { data = {
"_id": key, "_id": key,
"foo": "bar" "foo": "bar"
...@@ -58,15 +59,12 @@ ...@@ -58,15 +59,12 @@
}, JSON.stringify(data)]); }, JSON.stringify(data)]);
stop(); stop();
this.jio_storage.get({"_id": key}) this.jio_storage.get(key)
.then(function (result) { .then(function (result) {
console.log("result", result);
deepEqual(result, { deepEqual(result, {
"data": data, "_id": key,
"id": key, "foo": "bar"
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok"
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -79,11 +77,14 @@ ...@@ -79,11 +77,14 @@
test('getAttachment', function () { test('getAttachment', function () {
var key = "foobar12345", var key = "foobar12345",
object,
resultdata,
attachment = "barfoo54321", attachment = "barfoo54321",
server = this.server, server = this.server,
download_url = 'http://uth6nied.u.qiniudn.com/foobar12345/barfoo54321' + download_url = 'http://7xn150.com1.z0.glb.clouddn.com/foobar12345' +
'?e=2451491200&token=Imh9CFmpVZ5L1TE04Pjt-UmR_Ccr2cW9-KjSmvSA:' + '/barfoo54321?e=2451491200&token=s90kGV3JYDDQPivaPVpwxrHMi9RCpncL' +
'L88mHkZkfjr11DqPUqb5gsDjHFY=', 'gLctGDJQ:l_yS8PFderOhMyqHN0FN41tdJOM=',
data = { data = {
"_id": key, "_id": key,
"foo": "bar", "foo": "bar",
...@@ -95,24 +96,33 @@ ...@@ -95,24 +96,33 @@
}, JSON.stringify(data)]); }, JSON.stringify(data)]);
stop(); stop();
this.jio_storage.getAttachment({"_id": key, "_attachment": attachment}) this.jio_storage.getAttachment(key, attachment)
.then(function (result) { .then(function (result) {
return jIO.util.readBlobAsText(result.data).then(function (e) { return jIO.util.readBlobAsText(result).then(function (e) {
object = JSON.parse(e.target.result);
return { return {
"result": result, "result": object,
"text": e.target.result "text": object.target.responseText
}; };
}); });
}).then(function (result) { }).then(function (result) {
console.log("result", result);
resultdata = {
"data": result.result.target.responseText,
"id": result.result.id,
"attachment": result.result.target.attachment,
"method": result.result.target.method,
"status": result.result.target.status,
"statusText": result.result.target.statusText
};
result.result.data = result.text; result.result.data = result.text;
deepEqual(result.result, { deepEqual(resultdata, {
"data": JSON.stringify(data), "data": JSON.stringify(data),
"id": key, "id": key,
"attachment": attachment, "attachment": attachment,
"method": "getAttachment", "method": "GET",
"result": "success",
"status": 200, "status": 200,
"statusText": "Ok" "statusText": "OK"
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -122,7 +132,7 @@ ...@@ -122,7 +132,7 @@
start(); start();
}); });
}); });
/*
test('post', function () { test('post', function () {
var key = "foobar12345", var key = "foobar12345",
server = this.server, server = this.server,
...@@ -155,10 +165,10 @@ ...@@ -155,10 +165,10 @@
start(); start();
}); });
}); });
*/
test('put', function () { test('QiniuStorage.put', function () {
var key = "foobar12345", var server = this.server,
server = this.server,
upload_url = 'http://up.qiniu.com/', upload_url = 'http://up.qiniu.com/',
data = {"ok": "excellent"}; data = {"ok": "excellent"};
...@@ -167,9 +177,9 @@ ...@@ -167,9 +177,9 @@
}, JSON.stringify(data)]); }, JSON.stringify(data)]);
stop(); stop();
this.jio_storage.put({"_id": key}) this.jio_storage.put("bar", {"title": "foo"})
.then(function () { .then(function (result) {
throw new Error("Not implemented"); equal(result, "bar");
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -179,25 +189,23 @@ ...@@ -179,25 +189,23 @@
}); });
}); });
test('putAttachment', function () { test('QiniuStorage putAttachment', function () {
var key = "foobar12345", var server = this.server,
attachment = "barfoo54321", blob = new Blob(["foo"]),
server = this.server, upload_url = 'http://up.qiniu.com/';
upload_url = 'http://up.qiniu.com/',
data = {"ok": "excellent"};
server.respondWith("POST", upload_url, [200, { server.respondWith("POST", upload_url, [200, {
"Content-Type": "application/json" "Content-Type": "application/json"
}, JSON.stringify(data)]); }, "foo"]);
stop(); stop();
this.jio_storage.putAttachment({ this.jio_storage.putAttachment("bar", "foo", blob)
"_id": key,
"_attachment": attachment,
"_blob": "bar"
})
.then(function () { .then(function () {
throw new Error("Not implemented"); equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, upload_url);
equal(server.requests[0].status, "200");
equal(server.requests[0].responseText, "foo");
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
......
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