-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
122 lines (90 loc) · 3.72 KB
/
index.test.js
File metadata and controls
122 lines (90 loc) · 3.72 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { expect } from "chai"
import md5 from "js-md5"
import generateReqAny from "./index.js"
describe("request-any",()=>{
it("throw Error when generateReqAny called without param {reqUrl:string} ",()=>{
expect(()=>{generateReqAny()}).to.throw("request-any param reqUrl is required!")
})
it("when res data String format",done=>{
// request a web page
const reqAny = generateReqAny({reqUrl:"http://www.baidu.com"})
async function t0 () {
const res = await reqAny().catch(err=>{console.log(err);done()})
expect(res).to.have.property("data")
expect(res).to.have.property("timestamp")
expect(res).to.have.property("cache")
expect(res).to.have.property("expire")
let type = typeof res.data
expect(type).to.eq("string")
done()
}
t0()
})
it("when res data Json Object format",done=>{
// request api interface
const reqAny = generateReqAny({reqUrl:"http://cdn.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome"})
async function t1 () {
const res = await reqAny().catch(err=>{console.log(err);done()})
expect(res).to.have.property("data")
expect(res).to.have.property("timestamp")
expect(res).to.have.property("cache")
expect(res).to.have.property("expire")
let type = typeof res.data
expect(type).to.eq("object")
done()
}
t1()
})
it("should cache data when the request format parameters remain unchanged",done=>{
// request api interface
const reqAny = generateReqAny({reqUrl:"http://cdn.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome"})
async function t2 () {
let sleep = async ()=> await new Promise(r=>{setTimeout(()=>{r()},3000)})
let api = async ()=> await reqAny({cache:true,expire:1000}).catch(err=>{console.log(err);done()})
let res_1 = await api()
expect(res_1.cache).to.be.false
let res_2 = await api()
expect(res_2.cache).to.be.true
await sleep()
let res_3 = await api()
expect(res_3.cache).to.be.false
let res_4 = await api()
expect(res_4.cache).to.be.true
done()
}
t2()
})
it("can do something with data & headers before / after request send / back",done=>{
const sign = md5(new Date().getTime()+ "")
const reqAny = generateReqAny({
reqUrl:"http://cdn.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome" ,
beforeRequest:(data,headers)=>{
headers["Sign"] = sign
},
afterResponse:(data,headers) =>{
data.sign = sign
},
debug:true
})
async function t3 () {
let api = async ()=> await reqAny({cache:true,expire:1000}).catch(err=>{console.log(err);done()})
let res_1 = await api()
expect(res_1).to.haveOwnProperty("sign")
done()
}
t3()
})
it("if set timeout at generate Request Function,it will be default",done=>{
const reqAny = generateReqAny({
reqUrl:"http://cdn.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome" ,
debug:true,
timeout:5000
})
reqAny().then(()=>{
done()
})
})
it("transform data from response with big int",done=> {
//ADD YOUR TEST CODE
})
})