Skip to content

Commit fb1cd2d

Browse files
committed
Support exclude path
1 parent 7c986cf commit fb1cd2d

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ So, I create one by myself.
1212

1313
## Roadmap
1414
- [x] Combined with Mock.js
15+
- [x] Support exclude for some other path
1516
- [ ] Proxy for other api server
1617
- [ ] Support RAP system
1718

@@ -59,7 +60,13 @@ index.js
5960
import FetchMock from 'react-native-fetch-mock';
6061
6162
if (__dev__) {
62-
global.fetch = new FetchMock(require('path/to/mocks/directory')).fetch;
63+
global.fetch = new FetchMock(require('path/to/mocks/directory'), {
64+
fetch: global.fetch,
65+
exclude: [
66+
'http://www.google.com',
67+
/^foo(bar)?$/i,
68+
],
69+
}).fetch;
6370
}
6471
6572
// if __dev__ is true, it will back the data you defined in mock directory

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"babel-preset-react-native": "^1.9.1",
3939
"eslint": "^3.19.0",
4040
"expect.js": "^0.3.1",
41+
"isomorphic-fetch": "^2.2.1",
4142
"mocha": "^3.2.0"
4243
},
4344
"dependencies": {

src/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ import { parseRequest, matchUrl, isNull } from './util';
22
import Response from './response';
33

44
class FetchMock {
5-
constructor(required) {
5+
constructor(required, options = {
6+
fetch: () => {},
7+
exclude: [],
8+
}) {
69
if ('object' !== typeof required) {
710
throw new Error('There is no required defined.');
811
}
912

1013
this.urls = [];
14+
this.raw = options.fetch;
15+
this.exclude = options.exclude;
16+
1117
this.loadMocks = this.loadMocks.bind(this);
1218
this.loadMock = this.loadMock.bind(this);
1319
this.matchReqUrl = this.matchReqUrl.bind(this);
20+
this.isExclude = this.isExclude.bind(this);
1421
this.fetch = this.fetch.bind(this);
1522

1623
this.loadMocks(required);
@@ -62,7 +69,21 @@ class FetchMock {
6269
};
6370
}
6471

72+
isExclude(url) {
73+
for (let i = 0; i < this.exclude.length; i++) {
74+
const excludeUrl = this.exclude[i];
75+
return excludeUrl === url || (excludeUrl instanceof RegExp && excludeUrl.exec(url) !== null);
76+
}
77+
return false;
78+
}
79+
6580
fetch(url, options) {
81+
// using raw fetch while match exclude
82+
if (this.isExclude(url)) {
83+
// using raw fetch
84+
return this.raw(url, options);
85+
}
86+
6687
const { request, mock } = this.matchReqUrl(parseRequest(url, options));
6788
if ('function' !== typeof mock.func) {
6889
throw new Error('There is no url defined in __mocks__');

test/index.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import 'babel-polyfill';
22
import expect from 'expect.js';
33
import FetchMock, { Mock } from '../';
44

5-
const fetch = new FetchMock(require('../__mocks__')).fetch;
5+
const fetch = new FetchMock(require('../__mocks__'), {
6+
fetch: require('isomorphic-fetch'),
7+
exclude: [
8+
'http://www.baidu.com',
9+
/^foo(bar)?$/i,
10+
],
11+
}).fetch;
612
describe('test fetch mock', () => {
713
it('fetch /api/users data', async () => {
814
const response = await fetch('/api/users');
@@ -99,6 +105,16 @@ describe('test fetch mock', () => {
99105
expect(data).to.be.property('userId', '121');
100106
});
101107

108+
it('fetch exclude path', async () => {
109+
const response = await fetch('http://www.baidu.com');
110+
const { status } = response;
111+
expect(status).to.be.eql(200);
112+
const html = await response.text();
113+
expect(html).not.to.be(undefined);
114+
expect(html).not.to.be.empty();
115+
expect(html).to.be.an('string');
116+
});
117+
102118
it('post /api/users', async () => {
103119
const { status } = await fetch('/api/users', {
104120
method: 'POST',

yarn.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ ecc-jsbn@~0.1.1:
807807
dependencies:
808808
jsbn "~0.1.0"
809809

810+
encoding@^0.1.11:
811+
version "0.1.12"
812+
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
813+
dependencies:
814+
iconv-lite "~0.4.13"
815+
810816
es5-ext@^0.10.14, es5-ext@^0.10.9, es5-ext@~0.10.14:
811817
version "0.10.15"
812818
resolved "http://registry.npm.taobao.org/es5-ext/download/es5-ext-0.10.15.tgz#c330a5934c1ee21284a7c081a86e5fd937c91ea6"
@@ -1224,6 +1230,10 @@ http-signature@~1.1.0:
12241230
jsprim "^1.2.2"
12251231
sshpk "^1.7.0"
12261232

1233+
iconv-lite@~0.4.13:
1234+
version "0.4.19"
1235+
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"
1236+
12271237
ignore@^3.2.0:
12281238
version "3.2.6"
12291239
resolved "http://registry.npm.taobao.org/ignore/download/ignore-3.2.6.tgz#26e8da0644be0bb4cb39516f6c79f0e0f4ffe48c"
@@ -1374,6 +1384,10 @@ is-resolvable@^1.0.0:
13741384
dependencies:
13751385
tryit "^1.0.1"
13761386

1387+
is-stream@^1.0.1:
1388+
version "1.1.0"
1389+
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
1390+
13771391
is-typedarray@~1.0.0:
13781392
version "1.0.0"
13791393
resolved "http://registry.npm.taobao.org/is-typedarray/download/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"
@@ -1388,6 +1402,13 @@ isobject@^2.0.0:
13881402
dependencies:
13891403
isarray "1.0.0"
13901404

1405+
isomorphic-fetch@^2.2.1:
1406+
version "2.2.1"
1407+
resolved "https://registry.yarnpkg.com/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9"
1408+
dependencies:
1409+
node-fetch "^1.0.1"
1410+
whatwg-fetch ">=0.10.0"
1411+
13911412
isstream@~0.1.2:
13921413
version "0.1.2"
13931414
resolved "http://registry.npm.taobao.org/isstream/download/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
@@ -1618,6 +1639,13 @@ natural-compare@^1.4.0:
16181639
version "1.4.0"
16191640
resolved "http://registry.npm.taobao.org/natural-compare/download/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
16201641

1642+
node-fetch@^1.0.1:
1643+
version "1.7.3"
1644+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
1645+
dependencies:
1646+
encoding "^0.1.11"
1647+
is-stream "^1.0.1"
1648+
16211649
node-pre-gyp@^0.6.29:
16221650
version "0.6.34"
16231651
resolved "http://registry.npm.taobao.org/node-pre-gyp/download/node-pre-gyp-0.6.34.tgz#94ad1c798a11d7fc67381b50d47f8cc18d9799f7"
@@ -2195,6 +2223,10 @@ verror@1.3.6:
21952223
dependencies:
21962224
extsprintf "1.0.2"
21972225

2226+
whatwg-fetch@>=0.10.0:
2227+
version "2.0.3"
2228+
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz#9c84ec2dcf68187ff00bc64e1274b442176e1c84"
2229+
21982230
wide-align@^1.1.0:
21992231
version "1.1.0"
22002232
resolved "http://registry.npm.taobao.org/wide-align/download/wide-align-1.1.0.tgz#40edde802a71fea1f070da3e62dcda2e7add96ad"

0 commit comments

Comments
 (0)