Skip to content

Commit

Permalink
fix: support node 10 on https (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 11, 2018
1 parent 063c179 commit 721dc6b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 48 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -4,6 +4,7 @@ node_js:
- '4'
- '6'
- '8'
- '10'
install:
- npm i npminstall && npminstall
script:
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Expand Up @@ -3,6 +3,7 @@ environment:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '8'
- nodejs_version: '10'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
75 changes: 31 additions & 44 deletions lib/mm.js
Expand Up @@ -5,7 +5,6 @@ const muk = require('muk-prop');
const http = require('http');
const https = require('https');
const cp = require('child_process');
const semver = require('semver');
const thenify = require('thenify').withCallback;
const Readable = require('stream').Readable;
const Duplex = require('stream').Duplex;
Expand Down Expand Up @@ -185,12 +184,6 @@ exports.syncEmpty = function(mod, method) {
exports.http = {};
exports.https = {};

function getAgent(mod) {
// 0.11.12 has change api back to 0.10.x
return semver.satisfies(process.version, '>=0.11.0 <0.11.12') ? mod.globalAgent : mod;
}


function matchURL(options, params) {
const url = params && params.url || params;
const host = params && params.host;
Expand Down Expand Up @@ -227,6 +220,7 @@ function mockRequest() {
req.emit('error', err);
});
};
req.socket = {};
return req;
}

Expand Down Expand Up @@ -261,13 +255,11 @@ exports.https.request = function(url, data, headers, delay) {
};

function backupOriginalRequest(mod) {
if (!getAgent(mod).__sourceRequest) {
getAgent(mod).__sourceRequest = getAgent(mod).request;
if (!mod.__sourceRequest) {
mod.__sourceRequest = mod.request;
}
if (mod === http) {
if (!getAgent(mod).__sourceGet) {
getAgent(mod).__sourceGet = getAgent(mod).get;
}
if (!mod.__sourceGet) {
mod.__sourceGet = mod.get;
}
}

Expand All @@ -277,17 +269,14 @@ 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) {
mod.get = function(options, callback) {
const req = mod.request(options, callback);
req.end();
return req;
};

mod.request = function(options, callback) {
let datas = [];
let stream = null; // read stream
if (typeof data.read === 'function') {
Expand All @@ -302,7 +291,7 @@ function _request(mod, url, data, headers, delay) {

const match = matchURL(options, url);
if (!match) {
return getAgent(mod).__sourceRequest(options, callback);
return mod.__sourceRequest(options, callback);
}

const req = mockRequest();
Expand Down Expand Up @@ -343,6 +332,7 @@ function _request(mod, url, data, headers, delay) {

res.statusCode = headers.statusCode || 200;
res.headers = omit(headers, 'statusCode');
res.socket = req.socket;

function sendResponse() {
if (!req._aborted) {
Expand Down Expand Up @@ -398,20 +388,17 @@ 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) {
mod.get = function(options, callback) {
const req = mod.request(options, callback);
req.end();
return req;
};

mod.request = function(options, callback) {
const match = matchURL(options, url);
if (!match) {
return getAgent(mod).__sourceRequest(options, callback);
return mod.__sourceRequest(options, callback);
}

const req = mockRequest();
Expand Down Expand Up @@ -473,18 +460,18 @@ exports.spawn = function(code, stdout, stderr, timeout) {
* @return {mm} this - mm
*/
exports.restore = function() {
if (getAgent(http).__sourceRequest) {
getAgent(http).request = getAgent(http).__sourceRequest;
getAgent(http).__sourceRequest = null;
if (http.__sourceRequest) {
http.request = http.__sourceRequest;
http.__sourceRequest = null;
}
if (getAgent(http).__sourceGet) {
getAgent(http).get = getAgent(http).__sourceGet;
getAgent(http).__sourceGet = null;
if (http.__sourceGet) {
http.get = http.__sourceGet;
http.__sourceGet = null;
}

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

muk.restore();
Expand Down
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -17,7 +17,6 @@
"is-type-of": "^1.0.0",
"ko-sleep": "^1.0.2",
"muk-prop": "^1.0.0",
"semver": "^5.3.0",
"thenify": "^3.2.1"
},
"devDependencies": {
Expand Down Expand Up @@ -51,8 +50,8 @@
"node": ">=4.0.0"
},
"ci": {
"version": "4, 6, 8"
"version": "4, 6, 8, 10"
},
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)",
"license": "MIT"
}
}
2 changes: 1 addition & 1 deletion test/mm.test.js
Expand Up @@ -14,7 +14,7 @@ const uuid = require('uuid');
const mm = require('../');
const foo = require('./foo');

describe('mm.test.js', () => {
describe('test/mm.test.js', () => {

let port = null;
let sslPort = null;
Expand Down

0 comments on commit 721dc6b

Please sign in to comment.