add test case

This commit is contained in:
zhengqingxin 2017-07-10 16:33:05 +08:00
parent 2a9c7c7b04
commit acbd5d48a0

View File

@ -1,100 +1,139 @@
import test from 'ava';
const helper = require('think-helper');
const mock = require('mock-require');
const mockie = require('../../lib/mockie');
const utils = require('../../lib/utils');
// const path = require('path');
let context = require('../../../lib/extend/context');
function getApplication() {
return mock.reRequire('../../../lib/application');
}
const defaultOption = {
ROOT_PATH: __dirname,
// APP_PATH: path.resolve(__dirname,'../runtime')
};
const ctx = {
body:'hello thinkjs',
status:404,
ip:'10.10.10.10',
ips: '10.10.10.10,11.11.11.11',
module: {},
const mockContext = {
header: {
'user-agent': 'Mozilla/5.0',
referer: 'https://github.com/thinkjs/thinkjs',
'x-requested-with':'XMLHttpRequest',
},
method: 'GET',
userAgent:'test',
referrer(onlyHost){
return onlyHost
configuration:{
jsonpCallbackField:'_callback',
errnoField:'errno',
errmsgField:'errmsg',
defaultErrno:10010,
jsonContentType:'application/json;charset=utf-8'
},
referer(onlyHost){
return onlyHost
config(name){
return this.configuration[name]
},
type: 'text/html; charset=UTF-8',
isGet: true,
isPost: false,
isCli: true,
params: {name: 'thinkjs'},
header:{
accept:'application/json, text/plain, */*',
'Accept-Encoding':'gzip, deflate, br'
},
res:{
},
redirect(url,alt){
return url
},
set(name,value){
if(helper.isObject(name)){
this.header = Object.assign({},this.header,name);
return;
param(name){
if(name === '_callback'){
return 'test'
}
this.header[name] = value;
},
isMethod(method){
return method === this.method;
},
isAjax(){
return true
},
isJsonp(callback){
return callback
},
json(str){
return JSON.parse(str)
},
jsonp(data,callback){
return data;
},
success(data,message){
return {
errno: 0,
data,
errmsg: message || ''
}
},
fail({errno, errmsg, data}){
return {errno, errmsg, data}
},
expires(time){return time},
param(name,value){
if(value){
this.params[name] = value;
return value;
}
return this.params[name]
},
post(name,value){return value},
file(name,value){return value},
cookie(name,value){return value}
}
};
mockie.mockCluster(false);
mockie.mockThinkCluster({isFirstWorker:()=>{return true}});
const App = getApplication();
let app = new App(defaultOption);
// const controller = new think.Controller(ctx);
app.parseArgv = ()=>{return {port:8362}};
app.run();
const mockThink = {
isCli: true,
isNumber(param){
const numberReg = /^((\-?\d*\.?\d*(?:e[+-]?\d*(?:\d?\.?|\.?\d?)\d*)?)|(0[0-7]+)|(0x[0-9a-f]+))$/i;
return numberReg.test(param)
},
isArray(param){
return Array.isArray(param)
},
app:{
validators:{
messages:{
TEST_RULE:[1000,'test'],
TEST_NON_ARRAY_RULE:'test'
}
}
}
};
test.serial('get/set body', async t => {
// console.log(think.app.context)
Object.assign(context, mockContext);
global.think = Object.assign({}, mockThink);
test.serial('get userAgent', async t => {
t.is(context.userAgent, mockContext.header['user-agent'])
});
test.serial('referer', async t => {
t.is(context.referer(), mockContext.header['referer']);
t.is(context.referer(true), 'github.com');
});
test.serial('isGet', async t => {
t.is(context.isGet, true)
});
test.serial('isPost', async t => {
t.is(context.isPost, false)
});
test.serial('isMethod', async t => {
t.is(context.isMethod('GET'), true);
t.is(context.isMethod('POST'), false);
});
test.serial('isMethod', async t => {
t.is(context.isMethod('GET'), true);
t.is(context.isMethod('POST'), false);
});
test.serial('get isCli', async t => {
t.is(context.isCli, true);
});
test.serial('isAjax', async t => {
t.is(context.isAjax('GET'),true)
t.is(context.isAjax('POST'),false)
});
test.serial('isJsonp', async t => {
t.is(context.isJsonp(),true)
t.is(context.isJsonp('_callback'),true)
});
test.serial('jsonp', async t => {
context.jsonp({name:'thinkjs'})
t.is(context.body,'test({"name":"thinkjs"})');
});
test.serial('jsonp', async t => {
context.jsonp({name:'thinkjs'})
t.is(context.body,'test({"name":"thinkjs"})');
});
test.serial('jsonp empty fields', async t => {
context.jsonp('test','empty');
t.is(context.body,'test');
});
test.serial('json', async t => {
context.json(JSON.stringify({name:'thinkjs'}));
t.is(context.body,JSON.stringify({name:'thinkjs'}));
});
test.serial('success', async t => {
context.success([],'success');
t.deepEqual(context.body,{errno:0,errmsg:'success',data:[]});
});
test.serial('success', async t => {
let errObj = {errno:404,errmsg:'fail',data:[]};
context.fail(errObj);
t.deepEqual(context.body,errObj);
context.fail(404,'fail',[]);
t.deepEqual(context.body,{errno:404,errmsg:'fail',data:[]});
context.fail('fail',[]);
t.deepEqual(context.body,{errno:10010,errmsg:'fail',data:[]});
context.fail('fail');
t.deepEqual(context.body,{errno:10010,errmsg:'fail'});
context.fail('TEST_RULE')
t.deepEqual(context.body,{errno:1000,errmsg:'test'});
context.fail('TEST_NON_ARRAY_RULE')
t.deepEqual(context.body,{ errno: 10010, errmsg: 'TEST_NON_ARRAY_RULE' });
});