NodeJS axios请求自签名在浏览器中有效,但在jest超级测试用例中无效

epggiuax  于 2023-05-04  发布在  Jest
关注(0)|答案(1)|浏览(222)

我正在构建一个调用外部API的NodeJS应用程序。外部API使用自签名证书。我尝试设置环境变量process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'。这可以在正常使用应用程序时忽略证书验证。但是,当使用Jest Supertest代理调用NodeJS路由时,对同一端点的请求不起作用。
运行Jest Supertest用例时出现证书验证错误。使用Supertest代理发送请求时,是否有方法接受自签名证书?

npm test

Error: Error: SSL Error: DEPTH_ZERO_SELF_SIGNED_CERT
          at Object.dispatchError (/home/node/app/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:54:19)
          at EventEmitter.<anonymous> (/home/node/app/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:675:20)
          at EventEmitter.emit (events.js:323:22)
          at Request.<anonymous> (/home/node/app/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:384:47)
          at Request.emit (events.js:311:20)
          at Request.onRequestResponse (/home/node/app/node_modules/request/request.js:948:10)
          at ClientRequest.emit (events.js:311:20)
          at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:603:27)
          at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17)
          at TLSSocket.socketOnData (_http_client.js:476:22) undefined

NodeJS内部路由

通过浏览器访问路由时有效,但运行Jest Supertest时无效。内部路由是/internal并且可以正常工作,但是当该代码随后向具有自签名证书的外部API发送请求时,自签名证书会导致500错误消息。

router.get('/internal', (req, res, next) => {

  // Set request values that are specific to this route
  const requestOptionsData = { method: `GET`, endpoint: `/external` };

  try {
    httpCtrl.makeRequest(requestOptionsData).then(result => {
      if (result.error) {
        return res.status(result.status).json(result.error.message || result.error);
      }
      return res.status(result.status).json(result);
    }).catch((error) => {
      console.error(error);
      return res.status(500).send(error);
    });
  } catch (e) {
    console.error(e);
    return res.status(500).send(e);
  }

});

NodeJS控制器

向外部API发出axios请求的 Package 器函数

httpCtrl.makeRequest = async (requestOptionsData) => {

  let result = {};

  // Set request options
  const requestOptions = httpCtrl.setApiRequestOptions(requestOptionsData);

  let response;
  try {
    response = await axios(requestOptions);
  } catch(e) {
    result.error = e.toJSON() || e;
    console.error(result.error);
    result.status = 500;
    return result;
  }

  result.status = response && response.status || 500;
  result.data = response && response.data || {};

  return result;
}

JEST超级测试

导致证书错误的测试

const app = require('../app.js');
const supertest = require('supertest');

describe('API routes', () => {

  it('GET internal NodeJS route', async done => {

      agent
        .get('/internal')
        .set('Accept', 'application/json')
        .send()
        .expect(200)
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          expect(res.status).toBe(200);
          return done();
        });
  });

});

更新:

我尝试删除NODE_TLS_REJECT_UNAUTHORIZED并在axios代理配置中将rejectUnauthorized设置为false,但仍然存在相同的问题。当通过浏览器使用应用程序时,连接工作,但与supertest一起工作。

const agent = new https.Agent({
    rejectUnauthorized: false
  });

  const options = {
    url: url,
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': `Bearer ${requestOptionsData.jwt}`,
      'Host': process.env.ADMIN_API_BASE_URL
    },
    method: requestOptionsData.method || `GET`,
    httpsAgent: agent
  }

以下是此代理配置的错误:

Error: Error: self signed certificate
          at Object.dispatchError (/home/node/app/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:54:19)
          at EventEmitter.<anonymous> (/home/node/app/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:675:20)
          at EventEmitter.emit (events.js:323:22)
          at Request.<anonymous> (/home/node/app/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:384:47)
          at Request.emit (events.js:311:20)
          at Request.onRequestError (/home/node/app/node_modules/request/request.js:877:8)
          at ClientRequest.emit (events.js:311:20)
          at TLSSocket.socketErrorListener (_http_client.js:426:9)
          at TLSSocket.emit (events.js:311:20)
          at emitErrorNT (internal/streams/destroy.js:92:8) undefined
    console.error controllers/http.ctrl.js:50
f87krz0w

f87krz0w1#

我可以用this GitHub issue中的解决方案来解决这个问题。
我通过将testEnvironment: 'node'添加到jest.config.js文件来解决它。

相关问题