api.test.js
2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* http api 测试
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/05/17
*/
'use strict';
const test = require('ava');
// const rewire = require('rewire');
// const shelljs = require('shelljs');
const sign = require('../../library/sign');
// let config = rewire('../../config/common');
const API = require('../../library/api').API;
const ServiceAPI = require('../../library/api').ServiceAPI;
const SearchAPI = require('../../library/api').SearchAPI;
const getUrl = 'operations/api/v6/category/getCategory';
// test.before('create log folder', (t) => {
// shelljs.mkdir('log');
// t.pass();
// });
//
// test.after('delete log folder', (t) => {
// shelljs.rm('-rf', 'log');
// t.pass();
// });
test('api constructor test', (t) => {
let api = new ServiceAPI();
let api2 = new API();
let api3 = new SearchAPI();
t.true(api !== null);
t.true(api2 !== null);
t.true(api3 !== null);
});
test('api get test', t => {
let api = new ServiceAPI();
return api.get(getUrl, sign.apiSign({})).then(result => {
if (result && (result.code === 200 || result.code === 500)) {
t.pass();
} else {
t.fail();
}
});
});
test('api get use cache test', t => {
let api = new ServiceAPI();
return api.get(getUrl, sign.apiSign({}), true).then(result => {
if (result && (result.code === 200 || result.code === 500)) {
t.pass();
} else {
t.fail();
}
});
});
test('api post test', t => {
let api = new ServiceAPI();
return api.post(getUrl, sign.apiSign({})).then(result => {
if (result && (result.code === 200 || result.code === 500)) {
t.pass();
} else {
t.fail();
}
});
});
test('api multiple call test', (t) => {
let api = new ServiceAPI();
let multi = [api.get(getUrl, sign.apiSign({})), api.get(getUrl, sign.apiSign({}))];
return api.all(multi).then(result => {
if (result.length === 2) {
t.pass();
} else {
t.fail();
}
});
});
test('api multiple fail call test', (t) => {
let api = new ServiceAPI();
return api.all(1).catch((e) => {
if (e) {
t.pass();
} else {
t.fail();
}
});
});