-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (86 loc) · 1.65 KB
/
index.js
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
import "suchjs/lib/browser";
import Such from 'such-mock-browser';
import $ from 'jquery';
const globalSuch = Such.default;
const { mock } = globalSuch;
const { target, method } = mock;
const interceptor = mock.intercept(target.XHR | target.FETCH, {
timeout: [1000, 5000]
});
mock('/a', 'GET', {
a: ':string'
});
mock('/b', method.GET | method.POST, {
b: ':number'
});
mock('/c', '*', {
c: ':increment:{3}'
});
mock('/d', function(req){
return req.method === 'POST';
}, {
d: ':::haha'
});
mock(/\/\w+/, method.ANY, function(req){
return {
'*': '*'
};
});
mock('/e/:id', 'GET,POST', function(req, params){
return {
id: params.id,
words: globalSuch.as(':string')
};
});
mock('/f', 'GET', function(req, params){
return globalSuch.as({ plain: ':::haha,`:string`'});
}, {
timeout: 2000,
transformer: {
headers: {
'Content-Type': 'text/html'
}
}
});
mock('/g', 'GET', function(req){
return globalSuch.as({
'fetch{1,3}': ':string'
});
}, {
timeout: 3000
});
interceptor.on('response', (req, res) => {
console.log(res);
});
// XHR
$.post('/a', function(res){
console.log(res);
});
$.get('/b', function(res){
console.log(res);
});
$.post('/b', function(res){
console.log(res);
});
$.post('/c', function(res){
console.log(res);
});
$.post('/d', function(res){
console.log(res);
});
$.post('/e/123', function(res){
console.log(res);
});
$.get('/f', function(res){
console.log(res);
});
// fetch
window.fetch('/g', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
}).then((res) => res.json()).then((data) => {
console.log(data);
});