From ea1194c2c5fc34ee74c6a33776495ec2f8545f16 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 6 Sep 2017 23:43:41 +0800 Subject: [PATCH] fix: should support http request mock on node8 (#38) --- .travis.yml | 2 +- appveyor.yml | 4 ++-- lib/mm.js | 31 +++++++++++++++++++++++++++++++ package.json | 2 +- test/mm.test.js | 15 +++++++++++---- 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5ec1467..1651002 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: node_js node_js: - '4' - '6' - - '7' + - '8' install: - npm i npminstall && npminstall script: diff --git a/appveyor.yml b/appveyor.yml index 2efd0fa..56f601f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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 @@ -11,6 +11,6 @@ install: test_script: - node --version - npm --version - - npm run ci + - npm run test build: off diff --git a/lib/mm.js b/lib/mm.js index 3558155..d9bf268 100644 --- a/lib/mm.js +++ b/lib/mm.js @@ -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) { @@ -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 @@ -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) { @@ -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; }; diff --git a/package.json b/package.json index 4b0953f..e661d7c 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "node": ">=4.0.0" }, "ci": { - "version": "4, 6, 7" + "version": "4, 6, 8" }, "author": "fengmk2 (http://fengmk2.com)", "license": "MIT" diff --git a/test/mm.test.js b/test/mm.test.js index 4ceb07f..7643571 100644 --- a/test/mm.test.js +++ b/test/mm.test.js @@ -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(); }); } @@ -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'); } @@ -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'); }