From 3225e68e89ab743e1ceb9223c060db5f77d427b6 Mon Sep 17 00:00:00 2001 From: Leigh Zhu Date: Thu, 31 Jul 2014 23:31:56 +0800 Subject: [PATCH] fix custom headers && add image upload test --- Makefile | 4 ++-- README.md | 8 ++++---- index.js | 31 +++++++++++++++++++++++++------ test/basic-apis.js | 22 +++++++++++++--------- test/image.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 test/image.js diff --git a/Makefile b/Makefile index fa59cc0..6beb83b 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ test-cov: ./node_modules/.bin/_mocha \ -- -u exports \ --require should \ - --timeout 15000 \ + --timeout 30000 \ $(TESTS) \ --bail @@ -28,7 +28,7 @@ test-travis: -- -u exports \ --require should \ --slow 2s \ - --timeout 15000 \ + --timeout 30000 \ $(TESTS) \ --bail diff --git a/README.md b/README.md index 74150d1..bfd2d77 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ __Arguments__ * `file` The file you want to upload. It can be a `path` string or the file's raw data. * `make_dir` Auto create parent dir if it isn't exists.(Default: `true`). * `checksum` Set `true` to force SDK send a md5 of local file to UPYUN. Or set a md5value string by yourself. -* `opts` The additional http request headers. More detail in [Official Docs](http://docs.upyun.com/api/http_api/#上传文件) +* `opts` The additional http request headers(JavaScript Object). More detail in [Official Docs](http://docs.upyun.com/api/http_api/#上传文件) --------------------------------------- @@ -131,7 +131,7 @@ __Arguments__ Use this method to set api endpoint manually. __Arguments__ -* `endpoint` The value can be these: +* `endpoint` The value can be these(leave blank to let sdk auto select the best one): * `ctcc` China Telecom * `cucc` China Unicom * `cmcc` China Mobile @@ -145,9 +145,9 @@ For easy to use, all of the apis will return a response in this format: "data": {}, "headers": {} } -``` +``` -__For Chinese Docs, please visit [wiki](https://github.com/lisposter/co-upyun/wiki).__ +__中文文档,请查看 [WIKI](https://github.com/lisposter/co-upyun/wiki).__ # ATTENTION __ATTENTION: This SDK is under develop now. lots of APIs are not implemented temporary.__ diff --git a/index.js b/index.js index b745085..b8e19ba 100644 --- a/index.js +++ b/index.js @@ -62,11 +62,23 @@ function request(method, path, checksum, opts, body, localpath, cb){ resData += chunk; }); res.on('end', function() { - callback(null, { - statusCode: res.statusCode, - headers: res.headers, - data: resData - }); + if(res.statusCode > 200) { + var result = { + error: { + code: res.statusCode, + message: resData + }, + headers: res.headers + }; + callback(null, result); + } else { + callback(null, { + statusCode: res.statusCode, + headers: res.headers, + data: resData + }); + } + }); } }); @@ -183,10 +195,17 @@ UPYUN.prototype.getFileInfo = function(path) { UPYUN.prototype.uploadFile = function(path, data, mkdir, checksum, opts) { return function(fn) { - var opts = {}; + opts = opts || {}; if(mkdir !== false) opts["Mkdir"] = true; request('PUT', path, checksum, opts, data, null, function(err, res) { if(err) return fn(err); + res.data = Object.keys(res.headers).filter(function(itm) { + return itm.indexOf('x-upyun') >= 0; + }).reduce(function(prev, curr) { + prev[curr.split('-').pop()] = res.headers[curr]; + // TODO: covert date value to millisecond. + return prev; + }, {}); fn(null, res); }) } diff --git a/test/basic-apis.js b/test/basic-apis.js index f3647e2..8d8f9a4 100644 --- a/test/basic-apis.js +++ b/test/basic-apis.js @@ -72,29 +72,29 @@ describe('API', function() { it('should return 200', function(done) { co(function *() { - var res = yield upyun.uploadFile('/lorem/lorem.txt', './LICENSE', true, '69e97c8b91968c5878f331e53b8dcbf4', null); + var res = yield upyun.uploadFile('/lorem/lorem_md5_custom.txt', './LICENSE', true, '69e97c8b91968c5878f331e53b8dcbf4', null); res.should.have.property('statusCode').be.exactly(200); })(done) }) it('should response 200', function(done) { co(function *() { - var res = yield upyun.uploadFile('/lorem/lorem.txt', 'TESTTEST', true, null, null); + var res = yield upyun.uploadFile('/lorem/lorem_raw.txt', 'TESTTEST', true, null, null); res.should.have.property('statusCode').be.exactly(200); })(done) }) it('should response 200', function(done) { co(function *() { - var res = yield upyun.uploadFile('/lorem/lorem.txt', 'TESTTEST', true, true, null); + var res = yield upyun.uploadFile('/lorem/lorem_md5.txt', 'TESTTEST', true, true, null); res.should.have.property('statusCode').be.exactly(200); })(done) }) it('should return not found', function(done) { co(function *() { - var res = yield upyun.uploadFile('/lorem2/lorem.txt', './LICENSE', false, null, null); - res.should.have.property('statusCode').be.exactly(404); + var res = yield upyun.uploadFile('/lorem_notfound/lorem.txt', './LICENSE', false, null, null); + res.error.code.should.be.exactly(404); })(done) }) }) @@ -102,7 +102,8 @@ describe('API', function() { describe('.getFileInfo(path)', function() { it('should response file info', function(done) { co(function *() { - var res = yield upyun.getFileInfo('/lorem/lorem.txt'); + yield upyun.uploadFile('/lorem/lorem_for_getinfo.txt', 'TESTTEST', true, null, null); + var res = yield upyun.getFileInfo('/lorem/lorem_for_getinfo.txt'); res.should.have.property('data').not.be.empty; })(done) }) @@ -111,9 +112,11 @@ describe('API', function() { describe('.downloadFile(path)', function() { it('should download a file to local', function(done) { co(function *() { - var res = yield upyun.downloadFile('/lorem/lorem.txt', './test/lorem.txt'); + yield upyun.uploadFile('/lorem/lorem_for_download.txt', './LICENSE', true, null, null); + var res = yield upyun.downloadFile('/lorem/lorem_for_download.txt', './test/lorem.txt'); var a = read('./test/lorem.txt', 'utf8'); - a.should.match(/MIT/); + var str = yield a; + str.should.match(/MIT/); fs.unlinkSync('./test/lorem.txt'); })(done) }) @@ -132,7 +135,8 @@ describe('API', function() { describe('.removeFile(path)', function() { it('should response 200', function(done) { co(function *() { - var res = yield upyun.removeFile('/lorem/lorem.txt'); + yield upyun.uploadFile('/lorem/lorem_for_delete.txt', 'TESTTEST', true, null, null); + var res = yield upyun.removeFile('/lorem/lorem_for_delete.txt'); res.should.have.property('statusCode').be.exactly(200); })(done) }) diff --git a/test/image.js b/test/image.js new file mode 100644 index 0000000..b360fe8 --- /dev/null +++ b/test/image.js @@ -0,0 +1,29 @@ +var co = require('co'); +var should = require('should'); +var thunk = require('thunkify'); +var UPYUN = require('..'); + +var fs = require('fs'); +var read = thunk(fs.readFile); + +var upyun_img = new UPYUN('travis-img', 'travisci', 'testtest', 'ctcc'); + +describe('Image process', function() { + describe('image upload and process', function(done) { + it('should return pic info when upload file to image bucket', function(done) { + co(function *() { + upyun_img.setConf('bucket', 'travis-img'); + upyun_img.setEndpoint('ctcc'); + yield upyun_img.downloadFile('/res/upyun_logo.png', './upyun_logo.png'); + var res = yield upyun_img.uploadFile('/test/upyun_logo.png', './upyun_logo.png', true, null, { + "x-gmkerl-type": "fix_both", + "x-gmkerl-value": "400x400", + "x-gmkerl-rotate": 90 + }); + res.should.have.property('data').not.empty; + upyun_img.setConf('bucket', 'travis'); + fs.unlinkSync('./upyun_logo.png'); + })(done) + }) + }) +}) \ No newline at end of file