surrsoft
9/27/2019 - 4:23 PM

nock sample

nock sample

"use strict";

/* jshint esversion: 8 */

const axios = require('axios');
const nock = require('nock');
const TString = require('./xrsu/TString.js');

async function fnRequest(_stComment, _stPath) {
    await axios({
        method: 'get',
        url: _stPath
    })
        .then((res) => {
            console.log(`req-${_stComment} - success\n`, TString.substring_B(res.data, 50)); //del
        })
        .catch((err) => {
            console.log(`req-${_stComment} - error`, err); //del
        });
}

// --- nock
nock.disableNetConnect();
nock('http://example.com/')
    .get('/')
    .reply(200, { resp: 'ok' });

// ---
fnRequest('01', 'http://example.com/')
    .then(() => {
        nock.cleanAll();
        nock.enableNetConnect();
    })
    .then((res) => {
        fnRequest('02', 'https://ya.ru/').then(() => {
            // nock.cleanAll();
            // nock.enableNetConnect();
        });
    });