Skip to content

Commit 14bd460

Browse files
committed
Support complex request and bump new version
1 parent 36dc5c5 commit 14bd460

File tree

9 files changed

+167
-28
lines changed

9 files changed

+167
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ So, I create one by myself.
1616
__ mocks__/index.js
1717
```
1818
export default {
19-
'/api/path': (options) => {
19+
'/api/path': ({ method, url, params, headers }) => {
2020
const all = Mock.mock({
2121
'list|2': [{
2222
'id|+1': 1,

__mocks__/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Mock } from '../';
22

33
export default {
4-
'/api/users': (options) => {
4+
'/api/users': ({ params }) => {
55
const all = [
66
{
77
name: 'John',
@@ -13,14 +13,14 @@ export default {
1313
}
1414
];
1515
let filtered;
16-
if ('undefined' !== typeof options) {
16+
if ('undefined' !== typeof params) {
1717
filtered = all.filter(item => {
1818
let result = true;
19-
const keys = Object.keys(options);
19+
const keys = Object.keys(params);
2020
keys.forEach(key => {
21-
const option = options[key];
21+
const param = params[key];
2222

23-
if (item[key] && item[key] !== option) {
23+
if (item[key] && item[key] !== param) {
2424
result = false;
2525
}
2626
});
@@ -34,7 +34,7 @@ export default {
3434
data: filtered,
3535
});
3636
},
37-
'/api/users/mockjs': (options) => {
37+
'/api/users/mockjs': ({ params }) => {
3838
const all = Mock.mock({
3939
'list|2': [{
4040
'id|+1': 1,
@@ -43,14 +43,14 @@ export default {
4343
}]
4444
}).list;
4545
let filtered;
46-
if ('undefined' !== typeof options) {
46+
if ('undefined' !== typeof params) {
4747
filtered = all.filter(item => {
4848
let result = true;
49-
const keys = Object.keys(options);
49+
const keys = Object.keys(params);
5050
keys.forEach(key => {
51-
const option = options[key];
51+
const param = params[key];
5252

53-
if (item[key] && item[key] !== option) {
53+
if (item[key] && item[key] !== param) {
5454
result = false;
5555
}
5656
});

example/Basic/__mocks__/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
export default {
3-
'/api/users': (options) => {
3+
'/api/users': ({ params }) => {
44
const all = [
55
{
66
name: 'John',
@@ -12,14 +12,14 @@ export default {
1212
}
1313
];
1414
let filtered;
15-
if ('undefined' !== typeof options) {
15+
if ('undefined' !== typeof params) {
1616
filtered = all.filter(item => {
1717
let result = true;
18-
const keys = Object.keys(options);
18+
const keys = Object.keys(params);
1919
keys.forEach(key => {
20-
const option = options[key];
20+
const param = params[key];
2121

22-
if (item[key] && item[key] !== option) {
22+
if (item[key] && item[key] !== param) {
2323
result = false;
2424
}
2525
});

example/WithMockJs/__mocks__/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Mock } from 'react-native-fetch-mock';
22

33
export default {
4-
'/api/users/mockjs': (options) => {
4+
'/api/users/mockjs': ({ params }) => {
55
const all = Mock.mock({
66
'list|1-10': [{
77
'id|+1': 1,
@@ -10,14 +10,14 @@ export default {
1010
}]
1111
}).list;
1212
let filtered;
13-
if ('undefined' !== typeof options) {
13+
if ('undefined' !== typeof params) {
1414
filtered = all.filter(item => {
1515
let result = true;
16-
const keys = Object.keys(options);
16+
const keys = Object.keys(params);
1717
keys.forEach(key => {
18-
const option = options[key];
18+
const param = params[key];
1919

20-
if (item[key] && item[key] !== option) {
20+
if (item[key] && item[key] !== param) {
2121
result = false;
2222
}
2323
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-fetch-mock",
3-
"version": "0.1.3",
3+
"version": "0.1.5",
44
"description": "fetch mock for react-native",
55
"main": "index.js",
66
"scripts": {

src/index.js

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

33
class FetchMock {
44
constructor(required) {
@@ -39,13 +39,14 @@ class FetchMock {
3939
}
4040

4141
fetch(url, options) {
42-
const filters = this.urls.filter(uri => uri.url === prueUrl(url));
42+
const request = parseRequest(url, options);
43+
const filters = this.urls.filter(uri => uri.url === request.url);
4344
if (!filters || filters.length == 0) throw new Error(`No url ${url} is defined.`);
4445
const mock = filters[0];
4546
if ('function' !== typeof mock.func) {
4647
throw new Error('There is no url defined in __mocks__');
4748
}
48-
const promise = mock.func(options);
49+
const promise = mock.func(request);
4950
if (!promise || 'function' !== typeof promise.then) {
5051
throw new Error('The result of mock function should be a promise.');
5152
}

src/util.js

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

2+
const parseParamStr = (paramStr, isGet) => {
3+
let params = {};
4+
const paramPairs = paramStr.split('&');
5+
for (var i = 0; i < paramPairs.length; i++) {
6+
const paramPair = paramPairs[i];
7+
if (paramPair.indexOf('=') === -1) {
8+
continue;
9+
}
10+
const paramPairArray = paramPair.split('=');
11+
12+
const paramValue = isGet ? decodeURI(paramPairArray[1]) : paramPairArray[1];
13+
params[paramPairArray[0]] = paramPairArray.length === 2 ? paramValue : null;
14+
}
15+
return params;
16+
}
17+
18+
const parseBody = (body) => {
19+
if ('object' === typeof body) {
20+
return body;
21+
}
22+
try {
23+
return JSON.parse(body);
24+
} catch (e) {
25+
return parseParamStr(body);
26+
}
27+
}
28+
29+
const parseUrl = (url) => {
30+
const index = url.indexOf('?');
31+
const items = index > -1 ? url.split('?') : [url];
32+
if (items.length === 1) {
33+
return {
34+
url: items[0],
35+
params: {},
36+
};
37+
}
38+
39+
return {
40+
url: items[0],
41+
params: parseParamStr(items[1], true),
42+
};
43+
}
44+
45+
46+
const parseRequest = (url, options = {}) => {
47+
const urlObj = parseUrl(url);
48+
const data = parseBody(options.body || {});
49+
return {
50+
method: options.method || 'GET',
51+
url: urlObj.url,
52+
headers: options.headers,
53+
params: Object.assign({}, urlObj.params, data),
54+
};
55+
}
56+
257
const prueUrl = (url) => {
358
const index = url.indexOf('?');
459
const result = index > -1 ? url.substring(0, index) : url;
@@ -7,4 +62,6 @@ const prueUrl = (url) => {
762

863
export {
964
prueUrl,
65+
parseUrl,
66+
parseRequest,
1067
}

test/index.test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,23 @@ describe('test fetch mock', () => {
2020
expect(data).to.have.length(2);
2121
});
2222

23-
it('fetch /api/users with parameters', async () => {
23+
it('fetch /api/users with url parameters', async () => {
24+
const { data } = await fetch('/api/users?name=John');
25+
expect(data).not.to.be(undefined);
26+
expect(data).not.to.be.empty();
27+
expect(data).to.be.an('array');
28+
expect(data).to.have.length(1);
29+
});
30+
31+
it('fetch /api/users with post parameters', async () => {
2432
const { data } = await fetch('/api/users', {
25-
name: 'John',
33+
method: 'POST',
34+
headers: {
35+
'Content-Type': 'application/json',
36+
},
37+
body: JSON.stringify({
38+
name: 'John',
39+
}),
2640
});
2741
expect(data).not.to.be(undefined);
2842
expect(data).not.to.be.empty();
@@ -37,4 +51,12 @@ describe('test fetch mock', () => {
3751
expect(data).to.be.an('array');
3852
expect(data).to.have.length(2);
3953
});
54+
55+
it('fetch /api/users/mockjs with mockjs', async () => {
56+
const { data } = await fetch('/api/users/mockjs');
57+
expect(data).not.to.be(undefined);
58+
expect(data).not.to.be.empty();
59+
expect(data).to.be.an('array');
60+
expect(data).to.have.length(2);
61+
});
4062
});

test/util.test.js

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

55
describe('test util methods', () => {
66
it('get prue url', async () => {
77
expect(prueUrl('http://www.baidu.com?')).to.eql('http://www.baidu.com');
88
expect(prueUrl('http://www.baidu.com?ab=23')).to.eql('http://www.baidu.com');
99
});
10+
it('parse basic url', async () => {
11+
expect(parseUrl('http://www.baidu.com?')).to.eql({
12+
url: 'http://www.baidu.com',
13+
params: {},
14+
});
15+
expect(parseUrl('http://www.baidu.com?ab=23')).to.eql({
16+
url: 'http://www.baidu.com',
17+
params: {
18+
ab: '23',
19+
},
20+
});
21+
expect(parseUrl('http://www.baidu.com?ab=23&name=123')).to.eql({
22+
url: 'http://www.baidu.com',
23+
params: {
24+
ab: '23',
25+
name: '123',
26+
},
27+
});
28+
});
29+
it('parse error url', async () => {
30+
expect(parseUrl('http://www.baidu.com?a')).to.eql({
31+
url: 'http://www.baidu.com',
32+
params: {},
33+
});
34+
expect(parseUrl('http://www.baidu.com?a??=1')).to.eql({
35+
url: 'http://www.baidu.com',
36+
params: {},
37+
});
38+
expect(parseUrl('http://www.baidu.com?a=1?b=1')).to.eql({
39+
url: 'http://www.baidu.com',
40+
params: {
41+
a: '1',
42+
},
43+
});
44+
});
45+
it('parse encoded url', async () => {
46+
expect(parseUrl('http://www.baidu.com?callback=http%20%201')).to.eql({
47+
url: 'http://www.baidu.com',
48+
params: {
49+
callback: 'http 1'
50+
},
51+
});
52+
});
53+
it('parse request', async () => {
54+
expect(parseRequest('http://www.baidu.com?callback=http%20%201', {
55+
headers: {
56+
'Content-Type': 'application/json',
57+
}
58+
})).to.eql({
59+
method: 'GET',
60+
url: 'http://www.baidu.com',
61+
headers: {
62+
'Content-Type': 'application/json',
63+
},
64+
params: {
65+
callback: 'http 1'
66+
},
67+
});
68+
});
1069
});

0 commit comments

Comments
 (0)