Skip to content

Commit

Permalink
fix: should support http request mock on node8 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 6, 2017
1 parent 0e818e3 commit ea1194c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,7 @@ language: node_js
node_js:
- '4'
- '6'
- '7'
- '8'
install:
- npm i npminstall && npminstall
script:
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Expand Up @@ -2,7 +2,7 @@ environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'

install:
- ps: Install-Product node $env:nodejs_version
Expand All @@ -11,6 +11,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
31 changes: 31 additions & 0 deletions lib/mm.js
Expand Up @@ -261,6 +261,11 @@ function backupOriginalRequest(mod) {
if (!getAgent(mod).__sourceRequest) {
getAgent(mod).__sourceRequest = getAgent(mod).request;
}
if (mod === http) {
if (!getAgent(mod).__sourceGet) {
getAgent(mod).__sourceGet = getAgent(mod).get;
}
}
}

function _request(mod, url, data, headers, delay) {
Expand All @@ -269,6 +274,16 @@ function _request(mod, url, data, headers, delay) {
delay = parseInt(delay, 10);
}
delay = delay || 0;
// should mock htto.get too on node >= 8.0.0
// https://github.com/nodejs/node/blob/1403d28e7ded280e7582daa6e999164588d2234e/lib/http.js#L42
if (mod === http) {
getAgent(mod).get = function(options, callback) {
const req = getAgent(mod).request(options, callback);
req.end();
return req;
};
}

getAgent(mod).request = function(options, callback) {
let datas = [];
let stream = null; // read stream
Expand Down Expand Up @@ -379,6 +394,16 @@ function _requestError(mod, url, reqError, resError, delay) {
resError = new Error(resError);
resError.name = 'MockHttpResponseError';
}
// should mock htto.get too on node >= 8.0.0
// https://github.com/nodejs/node/blob/1403d28e7ded280e7582daa6e999164588d2234e/lib/http.js#L42
if (mod === http) {
getAgent(mod).get = function(options, callback) {
const req = getAgent(mod).request(options, callback);
req.end();
return req;
};
}

getAgent(mod).request = function(options, callback) {
const match = matchURL(options, url);
if (!match) {
Expand Down Expand Up @@ -445,10 +470,16 @@ exports.restore = function() {
getAgent(http).request = getAgent(http).__sourceRequest;
getAgent(http).__sourceRequest = null;
}
if (getAgent(http).__sourceGet) {
getAgent(http).get = getAgent(http).__sourceGet;
getAgent(http).__sourceGet = null;
}

if (getAgent(https).__sourceRequest) {
getAgent(https).request = getAgent(https).__sourceRequest;
getAgent(https).__sourceRequest = null;
}

muk.restore();
return this;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -50,7 +50,7 @@
"node": ">=4.0.0"
},
"ci": {
"version": "4, 6, 7"
"version": "4, 6, 8"
},
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)",
"license": "MIT"
Expand Down
15 changes: 11 additions & 4 deletions test/mm.test.js
Expand Up @@ -484,16 +484,20 @@ describe('mm.test.js', () => {
}, onResponse);
}
function onResponse(res) {
res.statusCode.should.eql(201);
res.statusCode.should.equal(201);
res.headers.should.eql({ server: 'mock server' });
res.setEncoding('utf8');
let body = '';
res.on('data', function(chunk) {
console.log('data emit: chunk size: %d', chunk.length);
chunk.should.be.a.String;
body += chunk;
});
res.on('end', function() {
body.should.equal(fs.readFileSync(__filename, 'utf8'));
console.log('end emit: body size: %d', body.length);
const content = fs.readFileSync(__filename, 'utf8');
body.length.should.equal(body.length);
body.should.equal(content);
done();
});
}
Expand Down Expand Up @@ -877,7 +881,8 @@ describe('mm.test.js', () => {
};
mm.restore();
try {
http.get({ path: '/foo' }, function() {});
http.request({ path: '/foo' }, function() {});
throw new Error('should not run this');
} catch (e) {
e.message.should.equal('Never want to send request out');
}
Expand All @@ -898,7 +903,9 @@ describe('mm.test.js', () => {

mm.restore();
try {
mod.get({ path: '/baz' }, function() {});
const req = mod.request({ path: '/baz' }, function() {});
req.end();
throw new Error('should not run this');
} catch (e) {
e.message.should.equal('Never want to send request out');
}
Expand Down

0 comments on commit ea1194c

Please sign in to comment.