<commit_before>import fs from 'fs';
import path from 'path';
import proxyquire from 'proxyquire';
import sinon from 'sinon';
import chai from 'chai';
import sinonChai from 'sinon-chai';
import {
  RequestSoapError,
} from '../../src/Request/RequestErrors';

const expect = chai.expect;
chai.use(sinonChai);

const templates = require('../../src/Services/Air/templates');

const errorXML = fs.readFileSync(path.join(
  __dirname,
  '../FakeResponses/Other/UnableToFareQuoteError.xml'
)).toString();

const serviceParams = [
  'URL',
  {
    username: 'USERNAME',
    password: 'PASSWORD',
  },
  templates.lowFareSearch,
  null,
  () => ({}),
  () => ({}),
  () => ({}),
];

const requestError = proxyquire('../../src/Request/uapi-request', {
  axios: {
    request: () => Promise.reject({ response: { status: 300, data: 3 }}),
  },
});
const requestJsonResponse = proxyquire('../../src/Request/uapi-request', {
  axios: {
    request: () => Promise.resolve({ data: '{"error": "Some error"}' }),
  },
});
const requestXMLResponse = proxyquire('../../src/Request/uapi-request', {
  axios: {
    request: () => Promise.resolve({ data: '<?xml version="1.0" encoding="UTF-8"?><SOAP:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><SOAP:Body><xml>Some xml</xml></SOAP:Body></SOAP:Envelope>' }),
  },
});

const requestXMLError = proxyquire('../../src/Request/uapi-request', {
  axios: {
    request: () => Promise.resolve({ data: errorXML }),
  },
});

describe('#Request', () => {
  describe('Request send', () => {
    beforeEach(() => sinon.spy(console, 'log'));
    afterEach(() => console.log.restore());

    it('should throw an SoapRequestError with underlying caused by Error', () => {
      const request = requestError(...serviceParams.concat(3));
      return request({})
        .catch((err) => {
          expect(err).to.be.an.instanceof(RequestSoapError.SoapRequestError);
          expect(err.data).to.not.equal(null);
          expect(err.data.status).to.be.equal(300);
          expect(err.data.data).to.be.equal(3);
          expect(console.log).to.have.callCount(4);
        });
    });
    it('should throw SoapServerError when JSON received', () => {
      const request = requestJsonResponse(...serviceParams.concat(3));
      return request({})
        .catch((err) => {
          expect(err).to.be.an.instanceof(RequestSoapError.SoapServerError);
          expect(console.log).to.have.callCount(4);
        });
    });
    it('should call XML parse when XML received', () => {
      const request = requestXMLResponse(...serviceParams.concat(3));
      return request({})
        .then((response) => {
          expect(response).to.deep.equal({});
          expect(console.log).to.have.callCount(6);
        });
    });

    it('should test custom log function with success', () => {
      const log = sinon.spy(function(...args) {
        console.log(args);
        return;
      });

      const params = serviceParams.concat([3]).concat([{ logFunction: log }]);

      const request = requestXMLResponse(...params);
      return request({})
        .then(() => {
          expect(log).to.have.callCount(6);
        });
    });

    it('should test custom log function with error', () => {
      const log = sinon.spy(function(...args) {
        console.log(args);
        return;
      });

      const params = serviceParams.concat([3]).concat([{ logFunction: log }]);

      const request = requestXMLError(...params);
      return request({})
        .then(() => {
          expect(log).to.have.callCount(6);
        });
    });
  });
});
<commit_msg>lint warnings<commit_after>import fs from 'fs';
import path from 'path';
import proxyquire from 'proxyquire';
import sinon from 'sinon';
import chai from 'chai';
import sinonChai from 'sinon-chai';
import {
  RequestSoapError,
} from '../../src/Request/RequestErrors';

const expect = chai.expect;
chai.use(sinonChai);

const templates = require('../../src/Services/Air/templates');

const errorXML = fs.readFileSync(path.join(
  __dirname,
  '../FakeResponses/Other/UnableToFareQuoteError.xml'
)).toString();

const serviceParams = [
  'URL',
  {
    username: 'USERNAME',
    password: 'PASSWORD',
  },
  templates.lowFareSearch,
  null,
  () => ({}),
  () => ({}),
  () => ({}),
];

const requestError = proxyquire('../../src/Request/uapi-request', {
  axios: {
    request: () => Promise.reject({ response: { status: 300, data: 3 } }),
  },
});
const requestJsonResponse = proxyquire('../../src/Request/uapi-request', {
  axios: {
    request: () => Promise.resolve({ data: '{"error": "Some error"}' }),
  },
});
const requestXMLResponse = proxyquire('../../src/Request/uapi-request', {
  axios: {
    request: () => Promise.resolve({ data: '<?xml version="1.0" encoding="UTF-8"?><SOAP:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><SOAP:Body><xml>Some xml</xml></SOAP:Body></SOAP:Envelope>' }),
  },
});

const requestXMLError = proxyquire('../../src/Request/uapi-request', {
  axios: {
    request: () => Promise.resolve({ data: errorXML }),
  },
});

describe('#Request', () => {
  describe('Request send', () => {
    beforeEach(() => sinon.spy(console, 'log'));
    afterEach(() => console.log.restore());

    it('should throw an SoapRequestError with underlying caused by Error', () => {
      const request = requestError(...serviceParams.concat(3));
      return request({})
        .catch((err) => {
          expect(err).to.be.an.instanceof(RequestSoapError.SoapRequestError);
          expect(err.data).to.not.equal(null);
          expect(err.data.status).to.be.equal(300);
          expect(err.data.data).to.be.equal(3);
          expect(console.log).to.have.callCount(4);
        });
    });
    it('should throw SoapServerError when JSON received', () => {
      const request = requestJsonResponse(...serviceParams.concat(3));
      return request({})
        .catch((err) => {
          expect(err).to.be.an.instanceof(RequestSoapError.SoapServerError);
          expect(console.log).to.have.callCount(4);
        });
    });
    it('should call XML parse when XML received', () => {
      const request = requestXMLResponse(...serviceParams.concat(3));
      return request({})
        .then((response) => {
          expect(response).to.deep.equal({});
          expect(console.log).to.have.callCount(6);
        });
    });

    it('should test custom log function with success', () => {
      const log = sinon.spy(function (...args) {
        console.log(args);
        return;
      });

      const params = serviceParams.concat([3]).concat([{ logFunction: log }]);

      const request = requestXMLResponse(...params);
      return request({})
        .then(() => {
          expect(log).to.have.callCount(6);
        });
    });

    it('should test custom log function with error', () => {
      const log = sinon.spy(function (...args) {
        console.log(args);
        return;
      });

      const params = serviceParams.concat([3]).concat([{ logFunction: log }]);

      const request = requestXMLError(...params);
      return request({})
        .then(() => {
          expect(log).to.have.callCount(6);
        });
    });
  });
});

{'fraction_non_alphanumeric': 0.19763395963813501, 'fraction_numerical': 0.005845511482254697, 'mean_word_length': 2.8906334596643206, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 2, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '898784', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2230, 'n_words': 395}