使用mocha和super test进行NodeJS HTTPS API测试 - “DEPTH_ZERO_SELF_SIGNED_CERT” [英] NodeJS HTTPS API testing with mocha and super test -"DEPTH_ZERO_SELF_SIGNED_CERT"
本文介绍了使用mocha和super test进行NodeJS HTTPS API测试 - “DEPTH_ZERO_SELF_SIGNED_CERT”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要测试通过 HTTPS
提供的API,其中 mocha
和 super test
(证书未过期)
I need to test an API served via HTTPS
with mocha
and super test
(the certificate are not expired)
这是服务器的要点:
...
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();
}
});
});
});
但是测试失败
,出现以下错误:
but the test FAILs
with the following error :
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
推荐答案
只为其他人也想知道该怎么做。
在test.js的顶部添加它你就可以了:
just for everyone else who is also wondering what to do. Add thison top of your test.js and you will be fine:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
这篇关于使用mocha和super test进行NodeJS HTTPS API测试 - “DEPTH_ZERO_SELF_SIGNED_CERT”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文