Skip to content

Commit cd35247

Browse files
committed
feat(*) full mocking of http requests
Using nock to capture all the infos about the requests (responses/headers ...) and re-injecting those infos via superagent-mocker, I can now use the client as if there was a server behind (and mock any request if necessary).
1 parent 064434f commit cd35247

File tree

11 files changed

+48
-118
lines changed

11 files changed

+48
-118
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"webpack-lazy": "./node_modules/.bin/webpack-dev-server --progress --colors --lazy",
2121
"webpack": "./node_modules/.bin/webpack-dev-server --progress --colors --hot --inline",
2222
"webpack-dev": "API_ROOT_URL='http://localhost:8000/github' DEVTOOLS=true npm run webpack",
23-
"webpack-mock": "DEVTOOLS=true NODE_ENV=mock npm run webpack",
23+
"webpack-mock": "DEVTOOLS=true NODE_ENV=mock API_ROOT_URL='http://localhost' npm run webpack",
2424
"webpack-dev-simple": "DEVTOOLS=true npm run webpack",
2525
"generate-http-fixtures": "node ./bin/nock/call-nock.js"
2626
},
@@ -95,7 +95,8 @@
9595
"react-router": "^1.0.0",
9696
"redux": "^3.0.4",
9797
"redux-router": "^1.0.0-beta3",
98-
"superagent": "^1.4.0"
98+
"superagent": "^1.4.0",
99+
"superagent-mocker": "^0.3.0"
99100
},
100101
"private": true
101102
}

src/bootstrap.js

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ else {
8080

8181
if (process.env.NODE_ENV === 'mock') {
8282
console.info('MOCK mode');
83-
console.info('STUB_MOCK_TIMEOUT', process.env.STUB_MOCK_TIMEOUT);
8483
}
8584

8685
ReactDOM.render(rootElement, document.getElementById('app-container'));

src/services/httpService.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
/**
2-
* To avoid having to involve webpack into requiring modules when we'll execute any code server-side
3-
* I dropped the resolve.alias solution in favor of dependency injection.
2+
* This is the singleton of the http service, where you can get the github client (which decorates the response from the github API)
3+
* In your app you may not want such a custom client, you should definitly take a look at
4+
* https://github.com/erikras/react-redux-universal-hot-example/blob/master/src/helpers/ApiClient.js
45
*/
56

67
let instance = null;
78

89
class HttpService {
910
constructor() {
10-
if (process.env.NODE_ENV === 'mock') {
11-
this.service = require('./httpService/http.stub');
12-
}
13-
else {
14-
this.service = require('./httpService/http');
15-
}
11+
this.service = require('./httpService/http');
1612
}
1713
get(relativeUrl, params) {
1814
return this.service.get(relativeUrl, params);

src/services/httpService/http.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import request from 'superagent';
1+
/**
2+
* This is the github client which decorates requests and responses from github.
3+
* It will adapt to the API_ROOT_URL you pass in env var.
4+
* In mock mode, it won't make any http requests, but will use the test/fixtures/http.json mock files (generated via npm run generate-http-fixtures)
5+
*/
6+
7+
const request = require('superagent');
8+
9+
if (process.env.NODE_ENV === 'mock') {
10+
require('./http.mock.js')(request);
11+
}
212

313
export default {
414
get(relativeUrl, params = {}) {

src/services/httpService/http.mock.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This function decorates "request" and adds the mock fixtures generated via npm run generate-http-fixtures
3+
*
4+
* It will be on when you npm run webpack-mock
5+
*/
6+
7+
import fixtures from '../../../test/fixtures/http.json';
8+
9+
export default function mockRequest(request) {
10+
11+
const mock = require('superagent-mocker')(request);
12+
13+
if (fixtures.length === 0) {
14+
throw new Error('Fixtures missing, please npm run generate-http-fixtures');
15+
}
16+
fixtures.forEach((fixture) => {
17+
console.info('mocking', fixture.path);
18+
mock.get(fixture.scope + fixture.path, (req) => {
19+
const res = {
20+
headers: fixture.headers,
21+
body: fixture.response,
22+
status: fixture.status
23+
};
24+
console.log('MOCK', fixture.path, req, res);
25+
return res;
26+
});
27+
});
28+
29+
return mock;
30+
}

src/services/httpService/http.mocks/followers.json

-1
This file was deleted.

src/services/httpService/http.mocks/repos.json

-1
This file was deleted.

src/services/httpService/http.mocks/user.json

-1
This file was deleted.

src/services/httpService/http.mocks/users.search.json

-1
This file was deleted.

src/services/httpService/http.stub.js

-99
This file was deleted.

webpack.config.js

-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ console.log('Launched in ' + (MODE_DEV_SERVER ? 'dev-server' : 'build') + ' mode
1414
const NODE_ENV = process.env.NODE_ENV ? process.env.NODE_ENV.toLowerCase() : 'dev';
1515
const DEVTOOLS = process.env.DEVTOOLS ? JSON.parse(process.env.DEVTOOLS) : false;
1616
const API_ROOT_URL = process.env.API_ROOT_URL ? process.env.API_ROOT_URL : 'https://api.github.com';
17-
const STUB_MOCK_TIMEOUT = process.env.STUB_MOCK_TIMEOUT ? process.env.STUB_MOCK_TIMEOUT : 400;
1817
const DISABLE_LINTER = process.env.DISABLE_LINTER ? JSON.parse(process.env.DISABLE_LINTER) : false;
1918

2019
const SOURCEMAPS_ACTIVE = NODE_ENV !== 'production' || DEVTOOLS === true;
@@ -27,7 +26,6 @@ else if(NODE_ENV === 'test'){
2726
}
2827
else if(NODE_ENV === 'mock'){
2928
console.log('MOCK mode');
30-
console.log('STUB_MOCK_TIMEOUT', STUB_MOCK_TIMEOUT);
3129
}
3230
else{
3331
console.log('DEVELOPMENT mode');
@@ -65,7 +63,6 @@ plugins.push(new webpack.DefinePlugin({
6563
'NODE_ENV': JSON.stringify(NODE_ENV),
6664
'DEVTOOLS': DEVTOOLS, // I rely on the variable bellow to make a bundle with the redux devtools (or not)
6765
'API_ROOT_URL': JSON.stringify(API_ROOT_URL), // The httpClient will rely on that (change it at will)
68-
'STUB_MOCK_TIMEOUT': JSON.parse(STUB_MOCK_TIMEOUT), // The httpStub will rely on that (change it at will)
6966
'DISABLE_LINTER': DISABLE_LINTER // Simply to log in browser console if linting is on or off
7067
}
7168
}));

0 commit comments

Comments
 (0)