api.test.js
2.18 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
/**
* http api 测试
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/05/17
*/
'use strict';
const test = require('ava');
const sign = require('../../library/sign');
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('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) {
t.pass();
} else {
t.fail();
}
});
});
test('api get test, api return an error', t => {
let api = new ServiceAPI();
return api.get(getUrl + '/error', sign.apiSign({})).catch(err => {
// 故意调用一个错误的接口
if (err && err.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) {
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) {
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();
}
});
});