From 40a7a201e1fa2edab90830bdeaef35f3000bc91b Mon Sep 17 00:00:00 2001 From: welefen Date: Thu, 16 Jul 2015 11:40:18 +0800 Subject: [PATCH] add lodate_template middleware test case --- src/core/http.js | 18 +- src/core/think.js | 14 +- src/middleware/locate_template.js | 4 + test/adapter/cache/base.js | 12 +- test/adapter/cache/file.js | 12 +- test/adapter/cache/memcache.js | 4 +- test/adapter/cache/redis.js | 4 +- test/middleware/base.js | 8 + test/middleware/deny_ip.js | 12 +- test/middleware/locate_template.js | 296 +++++++++++++++++++++++++++++ test/util/await.js | 6 +- 11 files changed, 360 insertions(+), 30 deletions(-) create mode 100644 test/middleware/locate_template.js diff --git a/src/core/http.js b/src/core/http.js index c18dec7d..0c958c8a 100644 --- a/src/core/http.js +++ b/src/core/http.js @@ -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(); diff --git a/src/core/think.js b/src/core/think.js index d2397bab..aafe3958 100644 --- a/src/core/think.js +++ b/src/core/think.js @@ -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 diff --git a/src/middleware/locate_template.js b/src/middleware/locate_template.js index 997b3157..42f75554 100644 --- a/src/middleware/locate_template.js +++ b/src/middleware/locate_template.js @@ -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; diff --git a/test/adapter/cache/base.js b/test/adapter/cache/base.js index ad2d687d..051a7608 100644 --- a/test/adapter/cache/base.js +++ b/test/adapter/cache/base.js @@ -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); }); }); diff --git a/test/adapter/cache/file.js b/test/adapter/cache/file.js index 587f1a99..9599f6a5 100644 --- a/test/adapter/cache/file.js +++ b/test/adapter/cache/file.js @@ -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); }); }); diff --git a/test/adapter/cache/memcache.js b/test/adapter/cache/memcache.js index 672e4afe..1736f626 100644 --- a/test/adapter/cache/memcache.js +++ b/test/adapter/cache/memcache.js @@ -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); }); }); diff --git a/test/adapter/cache/redis.js b/test/adapter/cache/redis.js index 45a4ae05..48792ae1 100644 --- a/test/adapter/cache/redis.js +++ b/test/adapter/cache/redis.js @@ -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); }); }); diff --git a/test/middleware/base.js b/test/middleware/base.js index 11e1ce6c..4f37c416 100644 --- a/test/middleware/base.js +++ b/test/middleware/base.js @@ -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); }) diff --git a/test/middleware/deny_ip.js b/test/middleware/deny_ip.js index a4955a57..88926b0b 100644 --- a/test/middleware/deny_ip.js +++ b/test/middleware/deny_ip.js @@ -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(); + }) + }) }) \ No newline at end of file diff --git a/test/middleware/locate_template.js b/test/middleware/locate_template.js new file mode 100644 index 00000000..b8186e8b --- /dev/null +++ b/test/middleware/locate_template.js @@ -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(); + }) + }) +}) \ No newline at end of file diff --git a/test/util/await.js b/test/util/await.js index 9af96706..9cee4b4c 100644 --- a/test/util/await.js +++ b/test/util/await.js @@ -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);