lib/mock_httpclient.js
function _request(url, opt) {
      opt = opt || {};
      opt.method = (opt.method || 'GET').toUpperCase();
      opt.headers = opt.headers || {};
      if (matchUrl(url) && matchMethod(opt.method)) {
        const result = extend(true, {}, mockResult);
        const response = {
          status: result.status,
          statusCode: result.status,
          headers: result.headers,
          size: result.responseSize,
          aborted: false,
          rt: 1,
          keepAliveSocket: result.keepAliveSocket || false,
        };

        httpclient.emit('response', {
          error: null,
          ctx: opt.ctx,
          req: {
            url,
            options: opt,
            size: result.requestSize,
          },
          res: response,
        });
        if (opt.dataType === 'json') {
          try {
            result.data = JSON.parse(result.data);
          } catch (err) {
            err.name = 'JSONResponseFormatError';
            throw err;
          }
        } else if (opt.dataType === 'text') {
          result.data = result.data.toString();
        }
        return Promise.resolve(result);
      }
      return rawRequest.call(httpclient, url, opt);
    }
function _request(url, opt) {
      opt = opt || {};
      opt.method = (opt.method || 'GET').toUpperCase();
      opt.headers = opt.headers || {};
      if (matchUrl(url) && matchMethod(opt.method)) {
        const result = extend(true, {}, mockResult);
        const response = {
          status: result.status,
          statusCode: result.status,
          headers: result.headers,
          size: result.responseSize,
          aborted: false,
          rt: 1,
          keepAliveSocket: result.keepAliveSocket || false,
        };

        httpclient.emit('response', {
          error: null,
          ctx: opt.ctx,
          req: {
            url,
            options: opt,
            size: result.requestSize,
          },
          res: response,
        });
        if (opt.dataType === 'json') {
          try {
            result.data = JSON.parse(result.data);
          } catch (err) {
            err.name = 'JSONResponseFormatError';
            throw err;
          }
        } else if (opt.dataType === 'text') {
          result.data = result.data.toString();
        }
        return Promise.resolve(result);
      }
      return rawRequest.call(httpclient, url, opt);
    }
function _request ( url , opt ) { opt = opt || { } ; opt . method = ( opt . method || 'GET' ) . toUpperCase ( ) ; opt . headers = opt . headers || { } ; if ( matchUrl ( url ) && matchMethod ( opt . method ) ) { const result = extend ( true , { } , mockResult ) ; const response = { status : result . status , statusCode : result . status , headers : result . headers , size : result . responseSize , aborted : false , rt : 1 , keepAliveSocket : result . keepAliveSocket || false , } ; httpclient . emit ( 'response' , { error : null , ctx : opt . ctx , req : { url , options : opt , size : result . requestSize , } , res : response , } ) ; if ( opt . dataType === 'json' ) { try { result . data = JSON . parse ( result . data ) ; } catch ( err ) { err . name = 'JSONResponseFormatError' ; throw err ; } } else if ( opt . dataType === 'text' ) { result . data = result . data . toString ( ) ; } return Promise . resolve ( result ) ; } return rawRequest . call ( httpclient , url , opt ) ; }
support generator rather than callback and promise
support generator rather than callback and promise