简体   繁体   English

使用Mocha和超级测试进行NodeJS HTTPS API测试-“ CERT_HAS_EXPIRED”

[英]NodeJS HTTPS API testing with mocha and super test -“CERT_HAS_EXPIRED”

I need to test an API served via HTTPS with mocha and super test 我需要使用mochasuper test来测试通过HTTPS服务的API

This is a gist of the server : 这是服务器的要点:

...
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

});

and this is the route to be tested 这是要测试的路线

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

I'm trying to test with this code in test/test.js 我正在尝试在test/test.js使用此代码进行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 : 但是测试FAILs并出现以下错误:

 Error: CERT_HAS_EXPIRED
  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)

While using plain HTTP the test is PASSING 尽管使用普通HTTP该测试PASSING

Have you taken the error message at face value already? 您是否已按面值提取了错误消息? Is the certificate you are using to test expired? 您用于测试的证书是否已过期? Is the clock way off on the machine? 机器上的时钟是否关闭? Also note that TLS certificates are associated with a host name and that host name is generally not "localhost" if you want to do anything useful. 还要注意,TLS证书与主机名关联,并且如果您想做任何有用的事情,则主机名通常不是“ localhost”。 Assuming it's not the obvious error of the certificate being expired, try using exactly the correct hostname when connecting to your server. 假设这不是证书过期的明显错误,请在连接到服务器时尝试使用正确的主机名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM