Skip to content

Commit

Permalink
fix custom headers && add image upload test
Browse files Browse the repository at this point in the history
  • Loading branch information
lisposter committed Jul 31, 2014
1 parent 1524e22 commit 3225e68
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -16,7 +16,7 @@ test-cov:
./node_modules/.bin/_mocha \
-- -u exports \
--require should \
--timeout 15000 \
--timeout 30000 \
$(TESTS) \
--bail

Expand All @@ -28,7 +28,7 @@ test-travis:
-- -u exports \
--require should \
--slow 2s \
--timeout 15000 \
--timeout 30000 \
$(TESTS) \
--bail

Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -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/#上传文件)

---------------------------------------

Expand Down Expand Up @@ -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
Expand All @@ -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.__
Expand Down
31 changes: 25 additions & 6 deletions index.js
Expand Up @@ -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
});
}

});
}
});
Expand Down Expand Up @@ -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);
})
}
Expand Down
22 changes: 13 additions & 9 deletions test/basic-apis.js
Expand Up @@ -72,37 +72,38 @@ 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)
})
})

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)
})
Expand All @@ -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)
})
Expand All @@ -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)
})
Expand Down
29 changes: 29 additions & 0 deletions 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)
})
})
})

0 comments on commit 3225e68

Please sign in to comment.