add lodate_template middleware test case

This commit is contained in:
welefen 2015-07-16 11:40:18 +08:00
parent 3f3fbab87d
commit 40a7a201e1
11 changed files with 360 additions and 30 deletions

View File

@ -16,13 +16,21 @@ import mime from 'mime';
let cookie = think.require('cookie');
export default class {
/**
* constructor
* @param {} args []
* @return {} []
*/
constructor(...args){
this.init(...args);
}
/**
* init method
* @param {Object} req [request]
* @param {Object} res [response]
* @return {} []
*/
constructor(req, res){
init(req, res){
//request object
this.req = req;
//response object
@ -162,8 +170,8 @@ export default class {
}
}
/**
* 通过ajax上传文件
* @return {[type]} [description]
* upload file by ajax
* @return {Promise} []
*/
getAjaxFilePost(){
let filename = this.req.headers[think.config('post.ajax_filename_header')];
@ -618,6 +626,10 @@ export default class {
let promise = fn(obj, encoding, this);
this._outputContentPromise.push(promise);
}
/**
* end
* @return {} []
*/
_end(){
this.cookie(true);
this.res.end();

View File

@ -760,7 +760,7 @@ think.localIp = '127.0.0.1';
* @param {Object} res [http response]
* @return {Object} [http object]
*/
let http;
think._http = (data = {}) => {
if (think.isString(data)) {
if (data[0] === '{') {
@ -798,17 +798,17 @@ think._http = (data = {}) => {
};
};
let Http = null;
think.http = (req, res) => {
if (!http) {
http = think.require('http');
if (!Http) {
Http = think.require('http');
}
//for cli request
if (res === undefined) {
let data = think._http(req);
req = data.req;
res = data.res;
({req, res} = think._http(req));
}
return (new http(req, res)).run();
let instance = new Http(req, res);
return instance.run();
};
/**
* get uuid

View File

@ -13,6 +13,10 @@ export default class extends think.middleware.base {
* @return {} []
*/
run(templateFile){
//is absolute file path
if(templateFile && path.isAbsolute(templateFile)){
return templateFile;
}
let http = this.http;
let {file_depr, file_ext, root_path, theme} = this.config('tpl');
let pathPrefix;

View File

@ -78,30 +78,30 @@ describe('adapter/cache/base.js', function() {
});
it('get expried data', function(done) {
instance.set('thinkjs2', 'maxzhang', 0.1).then(function() {
instance.set('thinkjs2', 'maxzhang', 0.01).then(function() {
setTimeout(function() {
instance.get('thinkjs2').then(function(value) {
assert.equal(value, undefined);
done();
});
}, 150);
}, 15);
});
});
it('run cache gc', function(done) {
instance.set('thinkjs3', 'maxzhang', 0.1).then(function() {
instance.set('thinkjs3', 'maxzhang', 0.01).then(function() {
setTimeout(function() {
instance.gc();
instance.get('thinkjs3').then(function(value) {
assert.equal(value, undefined);
done();
});
}, 150);
}, 15);
});
});
it('custom data timeout', function(done) {
var instance = new BaseCache({ timeout: 0.1 });
var instance = new BaseCache({ timeout: 0.01 });
instance.set('thinkjs4', 'maxzhang', 10).then(function() {
setTimeout(function() {
instance.gc();
@ -109,7 +109,7 @@ describe('adapter/cache/base.js', function() {
assert.equal(value, 'maxzhang');
done();
});
}, 150);
}, 15);
});
});

View File

@ -106,30 +106,30 @@ describe('adapter/cache/file.js', function() {
});
it('get expired data', function(done) {
instance.set('thinkjs2', 'maxzhang', 0.1).then(function() {
instance.set('thinkjs2', 'maxzhang', 0.01).then(function() {
setTimeout(function() {
instance.get('thinkjs2').then(function(value) {
assert.equal(value, undefined);
done();
});
}, 150);
}, 15);
});
});
it('run cache gc', function(done) {
instance.set('thinkjs2', 'maxzhang', 0.1).then(function() {
instance.set('thinkjs2', 'maxzhang', 0.01).then(function() {
setTimeout(function() {
instance.gc();
instance.get('thinkjs2').then(function(value) {
assert.equal(value, undefined);
done();
});
}, 150);
}, 15);
});
});
it('custom data timeout', function(done) {
var instance = new FileCache(think.extend(think.config('cache'), { timeout: 0.1 }));
var instance = new FileCache(think.extend(think.config('cache'), { timeout: 0.01 }));
instance.set('thinkjs3', 'maxzhang', 10).then(function() {
setTimeout(function() {
instance.gc();
@ -137,7 +137,7 @@ describe('adapter/cache/file.js', function() {
assert.equal(value, 'maxzhang');
done();
});
}, 150);
}, 15);
});
});

View File

@ -115,13 +115,13 @@ describe('adapter/cache/memcache.js', function() {
});
it('get expired data', function(done) {
instance.set('thinkjs2', 'maxzhang', 0.1).then(function() {
instance.set('thinkjs2', 'maxzhang', 0.01).then(function() {
setTimeout(function() {
instance.get('thinkjs2').then(function(value) {
assert.equal(value, undefined);
done();
});
}, 150);
}, 15);
});
});

View File

@ -115,13 +115,13 @@ describe('adapter/cache/redis.js', function() {
});
it('get expired data', function(done) {
instance.set('thinkjs2', 'maxzhang', 0.1).then(function() {
instance.set('thinkjs2', 'maxzhang', 0.01).then(function() {
setTimeout(function() {
instance.get('thinkjs2').then(function(value) {
assert.equal(value, undefined);
done();
});
}, 150);
}, 15);
});
});

View File

@ -3,9 +3,17 @@ var assert = require('assert');
var muk = require('muk');
var path = require('path');
var Index = require('../../lib/index.js');
var instance = new Index();
instance.load();
var Base = require('../../lib/middleware/base.js');
describe('middleware/base', function(){
before(function(){
console.log('before')
})
it('base is function', function(){
assert.equal(think.isFunction(Base), true);
})

View File

@ -6,7 +6,9 @@ var path = require('path');
var _http = require('../_http.js');
function execMiddleware(middleware, config){
return think.http(_http.req, _http.res).then(function(http){
var req = think.extend({}, _http.req);
var res = think.extend({}, _http.res);
return think.http(req, res).then(function(http){
if(config){
http._config = config;
}
@ -99,4 +101,12 @@ describe('middleware/deny_ip', function(){
done();
})
})
it('deny_ip 10.0.0.1,192.168.1.1,10.1.2.3', function(done){
execMiddleware('deny_ip', {
deny_ip: ['10.0.0.1', '192.168.1.1', '10.1.2.3']
}).then(function(data){
assert.equal(data, true);
done();
})
})
})

View File

@ -0,0 +1,296 @@
var assert = require('assert');
var muk = require('muk');
var path = require('path');
var _http = require('../_http.js');
function execMiddleware(middleware, config, options, data){
think.APP_PATH = path.dirname(__dirname) + '/testApp';
var req = think.extend({}, _http.req);
var res = think.extend({}, _http.res);
return think.http(req, res).then(function(http){
if(config){
http._config = config;
}
if(options){
for(var key in options){
http[key] = options[key];
}
}
return think.middleware(middleware, http, data);
})
}
describe('middleware/locate_template', function(){
before(function(){
var Index = require('../../lib/index.js');
var instance = new Index();
instance.load();
})
it('mode_mini, file_depr: /', function(done){
think.mode = think.mode_mini;
execMiddleware('locate_template', {
tpl: {
file_depr: '/',
file_ext: '.html'
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, think.APP_PATH + '/view/group/detail.html');
done();
})
})
it('mode_mini, file_ext: .txt', function(done){
think.mode = think.mode_mini;
execMiddleware('locate_template', {
tpl: {
file_depr: '/',
file_ext: '.txt'
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, think.APP_PATH + '/view/group/detail.txt');
done();
})
})
it('mode_mini', function(done){
think.mode = think.mode_mini;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.html'
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, think.APP_PATH + '/view/group_detail.html');
done();
})
})
it('mode_normal', function(done){
think.mode = think.mode_normal;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.html'
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, think.APP_PATH + '/view/home/group_detail.html');
done();
})
})
it('mode_module', function(done){
think.mode = think.mode_module;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.html'
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, think.APP_PATH + '/home/view/group_detail.html');
done();
})
})
it('mode_module, with theme', function(done){
think.mode = think.mode_module;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.html',
theme: 'color'
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, think.APP_PATH + '/home/view/color/group_detail.html');
done();
})
})
it('mode normal, with theme', function(done){
think.mode = think.mode_normal;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.html',
theme: 'color'
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, think.APP_PATH + '/view/home/color/group_detail.html');
done();
})
})
it('mode mini, with theme', function(done){
think.mode = think.mode_mini;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.html',
theme: 'color'
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, think.APP_PATH + '/view/color/group_detail.html');
done();
})
})
it('mode mini, with rootPath', function(done){
think.mode = think.mode_mini;
var rootPath = __dirname + '/rootPath';
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.txt',
root_path: rootPath
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, rootPath + '/group_detail.txt');
done();
})
})
it('mode normal, with rootPath', function(done){
think.mode = think.mode_normal;
var rootPath = __dirname + '/rootPath';
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.txt',
root_path: rootPath
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, rootPath + '/home/group_detail.txt');
done();
})
})
it('mode module, with rootPath', function(done){
think.mode = think.mode_module;
var rootPath = __dirname + '/rootPath';
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.txt',
root_path: rootPath
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}).then(function(data){
assert.equal(data, rootPath + '/home/group_detail.txt');
done();
})
})
it('mode module, xxx', function(done){
think.mode = think.mode_module;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.txt',
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}, 'xxx').then(function(data){
assert.equal(data, think.APP_PATH + '/home/view/group_xxx.txt');
done();
})
})
it('mode module, xxx/yyy', function(done){
think.mode = think.mode_module;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.txt',
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}, 'xxx/yyy').then(function(data){
assert.equal(data, think.APP_PATH + '/home/view/xxx_yyy.txt');
done();
})
})
it('mode module, xxx/yyy/zzz', function(done){
think.mode = think.mode_module;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.txt',
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}, 'xxx/yyy/zzz').then(function(data){
assert.equal(data, think.APP_PATH + '/xxx/view/yyy_zzz.txt');
done();
})
})
it('mode module, xxx/yyy/zzz.hhh', function(done){
think.mode = think.mode_module;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.txt',
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}, 'xxx/yyy/zzz.hhh').then(function(data){
assert.equal(data, think.APP_PATH + '/xxx/view/yyy_zzz.hhh');
done();
})
})
it('mode module, absolute file path', function(done){
think.mode = think.mode_module;
execMiddleware('locate_template', {
tpl: {
file_depr: '_',
file_ext: '.txt',
}
}, {
module: 'home',
controller: 'group',
action: 'detail'
}, '/xxx/yyy/zzz.hhh').then(function(data){
assert.equal(data, '/xxx/yyy/zzz.hhh');
done();
})
})
})

View File

@ -19,7 +19,7 @@ describe('await', function(){
var deferred = think.defer();
setTimeout(function(){
deferred.resolve(1);
}, 100)
}, 3)
return deferred.promise;
}).then(function(data){
assert.equal(data, 1);
@ -33,7 +33,7 @@ describe('await', function(){
var deferred = think.defer();
setTimeout(function(){
deferred.resolve(item);
}, 100)
}, 2)
return deferred.promise;
}).then(function(data){
assert.equal(data, 1);
@ -51,7 +51,7 @@ describe('await', function(){
var deferred = think.defer();
setTimeout(function(){
deferred.reject(item);
}, 100)
}, 3)
return deferred.promise;
}).catch(function(data){
assert.equal(data, 1);