【问题标题】:NodeJS HTTPS API testing with mocha and super test -"DEPTH_ZERO_SELF_SIGNED_CERT"使用 mocha 和超级测试的 NodeJS HTTPS API 测试 -“DEPTH_ZERO_SELF_SIGNED_CERT”
【发布时间】:2014-05-04 11:17:31
【问题描述】:

我需要使用mochasuper test 测试通过HTTPS 提供的API (证书未过期)

这是服务器的要点:

...
var app = express();
var _options = {
    key: fs.readFileSync('my-key.pem');,
    cert: fs.readFileSync('my-cert.pem')
};

// Start HTTPS server
https.createServer(_options, app).listen(app.get('port'), app.get('ip'), function () {

 // ok or not logs

});

这是要测试的路线

app.get('/hello',function (req, res) {
   res.json(200);
});

我正在尝试在test/test.js 中使用此代码进行测试

    var supertest = require('supertest'),
        api = supertest('https://localhost:3000');

describe('Hello test', function () {

      it('hello', function (done) {

        api.get('/hello')
               .expect(200)
               .end(function (err, res) {
                                        if (err) {
                                                   done(err);
                                        } else {
                                                   done();
               }
         });
    });
});

但测试FAILs 出现以下错误:

    enter Error: DEPTH_ZERO_SELF_SIGNED_CERT
  at SecurePair.<anonymous> (tls.js:1349:32)
  at SecurePair.EventEmitter.emit (events.js:92:17)
  at SecurePair.maybeInitFinished (tls.js:962:10)
  at CleartextStream.read [as _read] (tls.js:463:15)
  at CleartextStream.Readable.read (_stream_readable.js:320:10)
  at EncryptedStream.write [as _write] (tls.js:366:25)
  at doWrite (_stream_writable.js:219:10)
  at writeOrBuffer (_stream_writable.js:209:5)
  at EncryptedStream.Writable.write (_stream_writable.js:180:11)
  at write (_stream_readable.js:573:24)
  at flow (_stream_readable.js:582:7)
  at Socket.pipeOnReadable (_stream_readable.js:614:5)
  at Socket.EventEmitter.emit (events.js:92:17)
  at emitReadable_ (_stream_readable.js:408:10)
  at emitReadable (_stream_readable.js:404:5)
  at readableAddChunk (_stream_readable.js:165:9)
  at Socket.Readable.push (_stream_readable.js:127:10)
  at TCP.onread (net.js:526:21)

虽然使用普通的HTTP,但测试是PASSING

【问题讨论】:

    标签: javascript node.js https mocha.js supertest


    【解决方案1】:

    仅适用于所有想知道该怎么做的人。 在你的 test.js 顶部添加这个,你会没事的:

    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
    

    【讨论】:

    • 如果为大量测试执行此操作,我建议在 makefile 中添加 NODE_TLS_REJECT_UNAUTHORIZED=0,以便为所有测试设置它。类似NODE_ENV=test NODE_TLS_REJECT_UNAUTHORIZED=0 ./node_modules/.bin/mocha --recursive --timeout 5000 --reporter spec "./**/modules/*/test/**/*.js"
    • 由于某种原因,此设置在 Node12 中不再起作用。
    猜你喜欢
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    相关资源
    最近更新 更多