Showing
7 changed files
with
216 additions
and
50 deletions
@@ -19,7 +19,7 @@ exports.index = (req, res) => { | @@ -19,7 +19,7 @@ exports.index = (req, res) => { | ||
19 | id: req.params.id, | 19 | id: req.params.id, |
20 | uid: uid, | 20 | uid: uid, |
21 | vipLevel: vipLevel, | 21 | vipLevel: vipLevel, |
22 | - ua: req.get('user-agent') || '' | 22 | + ua: req.get('user-agent') || '' |
23 | }).then((result) => { | 23 | }).then((result) => { |
24 | res.render('detail', { | 24 | res.render('detail', { |
25 | resultShow: JSON.stringify(result, null, 4), | 25 | resultShow: JSON.stringify(result, null, 4), |
@@ -18,12 +18,11 @@ const api = config.domains.api; | @@ -18,12 +18,11 @@ const api = config.domains.api; | ||
18 | const serviceApi = config.domains.service; | 18 | const serviceApi = config.domains.service; |
19 | const searchApi = config.domains.search; | 19 | const searchApi = config.domains.search; |
20 | 20 | ||
21 | -let ApiUrl; | ||
22 | 21 | ||
23 | -class API { | 22 | +class Http { |
24 | 23 | ||
25 | - constructor() { | ||
26 | - ApiUrl = api; | 24 | + constructor(baseUrl) { |
25 | + this.ApiUrl = baseUrl; | ||
27 | } | 26 | } |
28 | 27 | ||
29 | /** | 28 | /** |
@@ -38,10 +37,11 @@ class API { | @@ -38,10 +37,11 @@ class API { | ||
38 | */ | 37 | */ |
39 | _requestFromAPI(options, cacheOption, reqId) { | 38 | _requestFromAPI(options, cacheOption, reqId) { |
40 | let timer = new Timer(); | 39 | let timer = new Timer(); |
40 | + let method = options.method || 'get'; | ||
41 | 41 | ||
42 | timer.put('getApi');// 统计时间开始 | 42 | timer.put('getApi');// 统计时间开始 |
43 | 43 | ||
44 | - log.info(`get api: ${options.url}?${qs.stringify(options.qs)}`); | 44 | + log.info(`${method} api: ${options.url}?${qs.stringify(options.qs)}`); |
45 | return rp(options).then((result) => { | 45 | return rp(options).then((result) => { |
46 | let duration = timer.put('getApi');// 统计时间结束 | 46 | let duration = timer.put('getApi');// 统计时间结束 |
47 | 47 | ||
@@ -60,10 +60,11 @@ class API { | @@ -60,10 +60,11 @@ class API { | ||
60 | } | 60 | } |
61 | } | 61 | } |
62 | return result; | 62 | return result; |
63 | - }).catch((error)=>{ | 63 | + }).catch((error)=> { |
64 | let duration = timer.put('getApi');// 统计时间结束 | 64 | let duration = timer.put('getApi');// 统计时间结束 |
65 | 65 | ||
66 | - log.error(`get api fail: use: ${duration}ms, statusCode: ${error.statusCode}, error: ${error.message}`); | 66 | + log.error(`${method} api fail: use: ${duration}ms, statusCode: |
67 | + ${error.statusCode}, error: ${error.message}`); | ||
67 | 68 | ||
68 | // 使用缓存的时候,读取二级缓存 | 69 | // 使用缓存的时候,读取二级缓存 |
69 | if (config.useCache) { | 70 | if (config.useCache) { |
@@ -116,12 +117,14 @@ class API { | @@ -116,12 +117,14 @@ class API { | ||
116 | */ | 117 | */ |
117 | get(url, data, cacheOption) { | 118 | get(url, data, cacheOption) { |
118 | let options = { | 119 | let options = { |
119 | - url: `${ApiUrl}${url}`, | 120 | + url: `${this.ApiUrl}${url}`, |
120 | qs: data, | 121 | qs: data, |
121 | json: true, | 122 | json: true, |
122 | timeout: 3000 | 123 | timeout: 3000 |
123 | }; | 124 | }; |
124 | 125 | ||
126 | + console.log('in api : ' + config.useCache); | ||
127 | + | ||
125 | // 从缓存获取数据 | 128 | // 从缓存获取数据 |
126 | if (config.useCache && cacheOption) { | 129 | if (config.useCache && cacheOption) { |
127 | return this._requestFromCache(options); | 130 | return this._requestFromCache(options); |
@@ -137,7 +140,7 @@ class API { | @@ -137,7 +140,7 @@ class API { | ||
137 | */ | 140 | */ |
138 | post(url, data) { | 141 | post(url, data) { |
139 | let options = { | 142 | let options = { |
140 | - url: `${ApiUrl}${url}`, | 143 | + url: `${this.ApiUrl}${url}`, |
141 | form: data, | 144 | form: data, |
142 | method: 'post', | 145 | method: 'post', |
143 | json: true, | 146 | json: true, |
@@ -150,22 +153,27 @@ class API { | @@ -150,22 +153,27 @@ class API { | ||
150 | all(list) { | 153 | all(list) { |
151 | if (_.isArray(list)) { | 154 | if (_.isArray(list)) { |
152 | return Promise.all(list); | 155 | return Promise.all(list); |
156 | + } else { | ||
157 | + return Promise.reject(Error('the parameters of api all method should be Array!')); | ||
153 | } | 158 | } |
154 | - throw Error('the parameters of api all method should be Array!'); | ||
155 | } | 159 | } |
156 | } | 160 | } |
157 | 161 | ||
158 | -class ServiceAPI extends API { | 162 | +class API extends Http { |
163 | + constructor() { | ||
164 | + super(api); | ||
165 | + } | ||
166 | +} | ||
167 | + | ||
168 | +class ServiceAPI extends Http { | ||
159 | constructor() { | 169 | constructor() { |
160 | - super(); | ||
161 | - ApiUrl = serviceApi; | 170 | + super(serviceApi); |
162 | } | 171 | } |
163 | } | 172 | } |
164 | 173 | ||
165 | -class SearchAPI extends API { | 174 | +class SearchAPI extends Http { |
166 | constructor() { | 175 | constructor() { |
167 | - super(); | ||
168 | - ApiUrl = searchApi; | 176 | + super(searchApi); |
169 | } | 177 | } |
170 | } | 178 | } |
171 | 179 |
@@ -79,6 +79,7 @@ | @@ -79,6 +79,7 @@ | ||
79 | "postcss-sprites": "^3.1.2", | 79 | "postcss-sprites": "^3.1.2", |
80 | "postcss-use": "^2.0.2", | 80 | "postcss-use": "^2.0.2", |
81 | "precss": "^1.4.0", | 81 | "precss": "^1.4.0", |
82 | + "rewire": "^2.5.1", | ||
82 | "shelljs": "^0.7.0", | 83 | "shelljs": "^0.7.0", |
83 | "stylelint": "^6.3.3", | 84 | "stylelint": "^6.3.3", |
84 | "stylelint-config-yoho": "^1.2.3", | 85 | "stylelint-config-yoho": "^1.2.3", |
@@ -4,19 +4,104 @@ | @@ -4,19 +4,104 @@ | ||
4 | * @author: jiangfeng<jeff.jiang@yoho.cn> | 4 | * @author: jiangfeng<jeff.jiang@yoho.cn> |
5 | * @date: 2016/05/17 | 5 | * @date: 2016/05/17 |
6 | */ | 6 | */ |
7 | +'use strict'; | ||
7 | 8 | ||
8 | const test = require('ava'); | 9 | const test = require('ava'); |
10 | + | ||
11 | +//const rewire = require('rewire'); | ||
12 | + | ||
13 | +//const shelljs = require('shelljs'); | ||
9 | const sign = require('../../library/sign'); | 14 | const sign = require('../../library/sign'); |
10 | -const API = require('../../library/api').ServiceAPI; | ||
11 | 15 | ||
12 | -const api = new API(); | 16 | +//let config = rewire('../../config/common'); |
17 | + | ||
18 | + | ||
19 | +const API = require('../../library/api').API; | ||
20 | +const ServiceAPI = require('../../library/api').ServiceAPI; | ||
21 | +const SearchAPI = require('../../library/api').SearchAPI; | ||
22 | + | ||
23 | +const getUrl = 'operations/api/v6/category/getCategory'; | ||
24 | + | ||
13 | 25 | ||
14 | -test('api get test', (t) => { | ||
15 | - return api.get('operations/api/v6/category/getCategory', sign.apiSign({})).then(result => { | ||
16 | - if (result && result.code === 200) { | 26 | +//test.before('create log folder', (t) => { |
27 | +// shelljs.mkdir('log'); | ||
28 | +// t.pass(); | ||
29 | +//}); | ||
30 | +// | ||
31 | +//test.after('delete log folder', (t) => { | ||
32 | +// shelljs.rm('-rf', 'log'); | ||
33 | +// t.pass(); | ||
34 | +//}); | ||
35 | + | ||
36 | +test('api constructor test', (t) => { | ||
37 | + let api = new ServiceAPI(); | ||
38 | + let api2 = new API(); | ||
39 | + let api3 = new SearchAPI(); | ||
40 | + | ||
41 | + t.true(api !== null); | ||
42 | + t.true(api2 !== null); | ||
43 | + t.true(api3 !== null); | ||
44 | +}); | ||
45 | + | ||
46 | +test('api get test', t => { | ||
47 | + let api = new ServiceAPI(); | ||
48 | + | ||
49 | + return api.get(getUrl, sign.apiSign({})).then(result => { | ||
50 | + if (result && (result.code === 200 || result.code === 500)) { | ||
17 | t.pass(); | 51 | t.pass(); |
18 | } else { | 52 | } else { |
19 | t.fail(); | 53 | t.fail(); |
20 | } | 54 | } |
21 | }); | 55 | }); |
22 | }); | 56 | }); |
57 | + | ||
58 | +test('api get use cache test', t => { | ||
59 | + let api = new ServiceAPI(); | ||
60 | + | ||
61 | + return api.get(getUrl, sign.apiSign({}), true).then(result => { | ||
62 | + if (result && (result.code === 200 || result.code === 500)) { | ||
63 | + t.pass(); | ||
64 | + } else { | ||
65 | + t.fail(); | ||
66 | + } | ||
67 | + }); | ||
68 | +}); | ||
69 | + | ||
70 | +test('api post test', t => { | ||
71 | + let api = new ServiceAPI(); | ||
72 | + | ||
73 | + return api.post(getUrl, sign.apiSign({})).then(result => { | ||
74 | + if (result && (result.code === 200 || result.code === 500)) { | ||
75 | + t.pass(); | ||
76 | + } else { | ||
77 | + t.fail(); | ||
78 | + } | ||
79 | + }); | ||
80 | +}); | ||
81 | + | ||
82 | +test('api multiple call test', (t) => { | ||
83 | + let api = new ServiceAPI(); | ||
84 | + let multi = [api.get(getUrl, sign.apiSign({})), api.get(getUrl, sign.apiSign({}))]; | ||
85 | + | ||
86 | + return api.all(multi).then(result => { | ||
87 | + if (result.length === 2) { | ||
88 | + t.pass(); | ||
89 | + } else { | ||
90 | + t.fail(); | ||
91 | + } | ||
92 | + }); | ||
93 | +}); | ||
94 | + | ||
95 | +test('api multiple fail call test', (t) => { | ||
96 | + let api = new ServiceAPI(); | ||
97 | + | ||
98 | + return api.all(1).catch((e) => { | ||
99 | + if (e) { | ||
100 | + t.pass(); | ||
101 | + } else { | ||
102 | + t.fail(); | ||
103 | + } | ||
104 | + }); | ||
105 | +}); | ||
106 | + | ||
107 | + |
test/library/cache.test.js
0 → 100644
1 | +/** | ||
2 | + * cache 测试 | ||
3 | + * | ||
4 | + * @author: jf<jeff.jiang@yoho.cn> | ||
5 | + * @date: 2016/5/18 | ||
6 | + */ | ||
7 | + | ||
8 | +'use strict'; | ||
9 | + | ||
10 | +import test from 'ava'; | ||
11 | + | ||
12 | +import cache from '../../library/cache'; | ||
13 | + | ||
14 | +let testKey = 'test_unit_key:' + (new Date()).getTime(); | ||
15 | +let testValue = 'anotherValue'; | ||
16 | +let anotherKey = 'test_unit_key2:' + (new Date()).getTime(); | ||
17 | +let anotherValue = {a: 1}; | ||
18 | + | ||
19 | +let slaveTestKey = 'test_unit_key3:' + (new Date()).getTime(); | ||
20 | +let slaveTestValue = 'anotherValue3'; | ||
21 | + | ||
22 | +test.before('set test key', (t) => { | ||
23 | + cache.set(testKey, testValue); | ||
24 | + cache.set(anotherKey, anotherValue); | ||
25 | + t.pass(); | ||
26 | +}); | ||
27 | + | ||
28 | +test.after('del test key', (t) => { | ||
29 | + cache.del(testKey); | ||
30 | + cache.del(anotherKey); | ||
31 | + t.pass(); | ||
32 | +}); | ||
33 | + | ||
34 | +test('cache get test', (t) => { | ||
35 | + return cache.get(testKey).then((v) => { | ||
36 | + t.is(v, testValue); | ||
37 | + }); | ||
38 | +}); | ||
39 | + | ||
40 | +test('cache get multi test', (t) => { | ||
41 | + cache.set(anotherKey, anotherValue); | ||
42 | + return cache.getMulti([testKey, anotherKey]).then((values) => { | ||
43 | + console.log(values); | ||
44 | + t.is(values[testKey], testValue); | ||
45 | + t.is(values[anotherKey], JSON.stringify(anotherValue)); | ||
46 | + }); | ||
47 | +}); | ||
48 | + | ||
49 | +test('cache get from slave test', (t) => { | ||
50 | + return cache.getFromSlave(testKey).then((v) => { | ||
51 | + t.is(v, testValue); | ||
52 | + }); | ||
53 | +}); | ||
54 | + | ||
55 | +test('cache get multi from slave test', (t) => { | ||
56 | + cache.set(anotherKey, anotherValue); | ||
57 | + return cache.getMultiFromSlave([testKey, anotherKey]).then((values) => { | ||
58 | + t.is(values[testKey], testValue); | ||
59 | + t.is(values[anotherKey], JSON.stringify(anotherValue)); | ||
60 | + }); | ||
61 | +}); | ||
62 | + | ||
63 | +test('cache set to slave', (t) => { | ||
64 | + return cache.setSlave(slaveTestKey, slaveTestValue).then(() => { | ||
65 | + return cache.getFromSlave(slaveTestKey); | ||
66 | + }).then((v) => { | ||
67 | + t.is(v, slaveTestValue); | ||
68 | + cache.del(slaveTestKey); | ||
69 | + }); | ||
70 | +}); |
@@ -6,31 +6,42 @@ const test = require('ava'); | @@ -6,31 +6,42 @@ const test = require('ava'); | ||
6 | const shelljs = require('shelljs'); | 6 | const shelljs = require('shelljs'); |
7 | const logger = require('../../library/logger'); | 7 | const logger = require('../../library/logger'); |
8 | 8 | ||
9 | -// const today = () => { | ||
10 | -// let now = new Date(); | ||
11 | -// let s = now.getFullYear(); | 9 | +const today = () => { |
10 | + let now = new Date(); | ||
11 | + let s = now.getFullYear(); | ||
12 | 12 | ||
13 | -// if (now.getMonth() < 10) { | ||
14 | -// s += '-0' + now.getMonth(); | ||
15 | -// } else { | ||
16 | -// s += now.getMonth(); | ||
17 | -// } | ||
18 | -// if (now.getDay() < 10) { | ||
19 | -// s += '-0' + now.getDay(); | ||
20 | -// } else { | ||
21 | -// s += now.getDay(); | ||
22 | -// } | ||
23 | -// }; | 13 | + if (now.getMonth() < 10) { |
14 | + s += '-0' + now.getMonth(); | ||
15 | + } else { | ||
16 | + s += now.getMonth(); | ||
17 | + } | ||
18 | + if (now.getDay() < 10) { | ||
19 | + s += '-0' + now.getDay(); | ||
20 | + } else { | ||
21 | + s += now.getDay(); | ||
22 | + } | ||
23 | + return s; | ||
24 | +}; | ||
25 | + | ||
26 | +//test.before('create log folder', t => { | ||
27 | +// shelljs.mkdir('log'); | ||
28 | +// t.pass(); | ||
29 | +//}); | ||
30 | +// | ||
31 | +//test.after('clean test log file ', t => { | ||
32 | +// shelljs.rm('-rf', 'log'); | ||
33 | +// t.pass(); | ||
34 | +//}); | ||
24 | 35 | ||
25 | test.cb('logger test', t => { | 36 | test.cb('logger test', t => { |
26 | shelljs.rm('-f', 'log/*.log.*'); | 37 | shelljs.rm('-f', 'log/*.log.*'); |
27 | logger.info('xxx', () => { | 38 | logger.info('xxx', () => { |
28 | - shelljs.ls('info.log.*').forEach(s => console.log('generate log file:' + s)); | 39 | + shelljs.ls('log/info.log.*').some(s => { |
40 | + console.log('generate log file:' + s); | ||
41 | + return s === 'info.log.' + today(); | ||
42 | + }); | ||
29 | t.end(); | 43 | t.end(); |
30 | }); | 44 | }); |
31 | }); | 45 | }); |
32 | 46 | ||
33 | -test.after('clean test log file ', t => { | ||
34 | - shelljs.rm('-f', 'log/*.log.*'); | ||
35 | - t.pass(); | ||
36 | -}); | 47 | + |
-
Please register or login to post a comment