Skip to content

Commit 4d7a520

Browse files
committed
Support Rest API
* Add rest api example * Add unit test
1 parent 14bd460 commit 4d7a520

File tree

5 files changed

+131
-6
lines changed

5 files changed

+131
-6
lines changed

__mocks__/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,12 @@ export default {
6363
return Promise.resolve({
6464
data: filtered,
6565
});
66+
},
67+
'/api/users/{userId}': ({ urlparams }) => {
68+
return Promise.resolve({
69+
data: {
70+
userId: urlparams.userId,
71+
},
72+
});
6673
}
6774
}

src/index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseRequest } from './util';
1+
import { parseRequest, matchUrl } from './util';
22

33
class FetchMock {
44
constructor(required) {
@@ -9,6 +9,7 @@ class FetchMock {
99
this.urls = [];
1010
this.loadMocks = this.loadMocks.bind(this);
1111
this.loadMock = this.loadMock.bind(this);
12+
this.matchReqUrl = this.matchReqUrl.bind(this);
1213
this.fetch = this.fetch.bind(this);
1314

1415
this.loadMocks(required);
@@ -38,11 +39,26 @@ class FetchMock {
3839
});
3940
}
4041

41-
fetch(url, options) {
42-
const request = parseRequest(url, options);
43-
const filters = this.urls.filter(uri => uri.url === request.url);
42+
matchReqUrl(request) {
43+
let insideParams;
44+
const filters = this.urls.filter(uri => {
45+
const obj = matchUrl(uri.url, request.url);
46+
if (obj.result) {
47+
insideParams = obj.params;
48+
return true;
49+
}
50+
return false;
51+
});
4452
if (!filters || filters.length == 0) throw new Error(`No url ${url} is defined.`);
45-
const mock = filters[0];
53+
request.urlparams = insideParams;
54+
return {
55+
request,
56+
mock: filters[0],
57+
};
58+
}
59+
60+
fetch(url, options) {
61+
const { request, mock } = this.matchReqUrl(parseRequest(url, options));
4662
if ('function' !== typeof mock.func) {
4763
throw new Error('There is no url defined in __mocks__');
4864
}

src/util.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11

2+
const removeProctol = (url) => {
3+
let index = -1;
4+
if ((index = url.indexOf('://')) > -1) {
5+
// contains proctol
6+
return url.substring(index);
7+
}
8+
return url;
9+
}
10+
211
const parseParamStr = (paramStr, isGet) => {
312
let params = {};
413
const paramPairs = paramStr.split('&');
@@ -60,8 +69,56 @@ const prueUrl = (url) => {
6069
return result;
6170
}
6271

72+
const matchUrl = (sourceUrl, targetUrl) => {
73+
if (sourceUrl === targetUrl) {
74+
return {
75+
result: true,
76+
params: {},
77+
};
78+
}
79+
const sourceUrlWithoutProctol = removeProctol(sourceUrl);
80+
const targetUrlWithoutProctol = removeProctol(targetUrl);
81+
const sourceUrlSplits = sourceUrlWithoutProctol.split('/');
82+
const targetUrlSplits = targetUrlWithoutProctol.split('/');
83+
84+
if (sourceUrlSplits.length !== targetUrlSplits.length) {
85+
return {
86+
result: false,
87+
}
88+
}
89+
90+
let params = {};
91+
for (let i = 0; i < sourceUrlSplits.length; i++) {
92+
const sourceUrlSplit = sourceUrlSplits[i];
93+
const targetUrlSplit = targetUrlSplits[i];
94+
if (sourceUrlSplit === targetUrlSplit) {
95+
continue;
96+
}
97+
98+
if (sourceUrlSplit.startsWith('{') && sourceUrlSplit.endsWith('}')) {
99+
if (sourceUrlSplit.replace(/[^{]/g,'').length > 1 || sourceUrlSplit.replace(/[^}]/g,'').length > 1) {
100+
return {
101+
result: false,
102+
}
103+
}
104+
// contains url parameter
105+
params[sourceUrlSplit.substring(1, sourceUrlSplit.length - 1)] = targetUrlSplit;
106+
continue;
107+
}
108+
return {
109+
result: false,
110+
};
111+
}
112+
113+
return {
114+
result: true,
115+
params,
116+
}
117+
}
118+
63119
export {
64120
prueUrl,
65121
parseUrl,
66122
parseRequest,
123+
matchUrl,
67124
}

test/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ describe('test fetch mock', () => {
4444
expect(data).to.have.length(1);
4545
});
4646

47+
it('fetch /api/users/{userId}', async () => {
48+
const { data } = await fetch('/api/users/123');
49+
expect(data).not.to.be(undefined);
50+
expect(data).not.to.be.empty();
51+
expect(data).to.be.property('userId', '123');
52+
});
53+
4754
it('fetch /api/users/mockjs with mockjs', async () => {
4855
const { data } = await fetch('/api/users/mockjs');
4956
expect(data).not.to.be(undefined);

test/util.test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'babel-polyfill';
22
import expect from 'expect.js';
3-
import { prueUrl, parseUrl, parseRequest } from '../src/util';
3+
import { prueUrl, parseUrl, parseRequest, matchUrl } from '../src/util';
44

55
describe('test util methods', () => {
66
it('get prue url', async () => {
@@ -66,4 +66,42 @@ describe('test util methods', () => {
6666
},
6767
});
6868
});
69+
it('match url', async () => {
70+
expect(matchUrl('http://www.baidu.com', 'http://www.baidu.com')).to.ok();
71+
});
72+
it('match url with inside parameter', async () => {
73+
expect(matchUrl('http://www.baidu.com/{id}', 'http://www.baidu.com/123')).to.be.eql({
74+
result: true,
75+
params: {
76+
id: '123',
77+
},
78+
});
79+
expect(matchUrl('http://www.baidu.com/{id}/do', 'http://www.baidu.com/123/do')).to.be.eql({
80+
result: true,
81+
params: {
82+
id: '123',
83+
},
84+
});
85+
expect(matchUrl('http://www.baidu.com/{id}/do/{name}', 'http://www.baidu.com/123/do/hello')).to.be.eql({
86+
result: true,
87+
params: {
88+
id: '123',
89+
name: 'hello',
90+
},
91+
});
92+
});
93+
it('match error url with inside parameter', async () => {
94+
expect(matchUrl('http://www.baidu.com/{id}{name}/do', 'http://www.baidu.com/123/do')).to.be.eql({
95+
result: false,
96+
});
97+
expect(matchUrl('http://www.baidu.com/{id}{name/do', 'http://www.baidu.com/123/do')).to.be.eql({
98+
result: false,
99+
});
100+
expect(matchUrl('http://www.baidu.com/{id}name}/do', 'http://www.baidu.com/123/do')).to.be.eql({
101+
result: false,
102+
});
103+
expect(matchUrl('http://www.baidu.com/{id}/do', 'http://www.baidu.com/123')).to.be.eql({
104+
result: false,
105+
});
106+
});
69107
});

0 commit comments

Comments
 (0)