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

时间:2014-03-25 11:56:27

标签: javascript node.js api mocha supertest

我需要使用HTTPSmocha

来测试通过super test投放的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出现以下错误:

 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)

使用普通HTTP时,测试为PASSING

1 个答案:

答案 0 :(得分:1)

您是否已在面值处获取错误消息?您用于测试的证书是否已过期?机器上的时钟是否关闭?另请注意,TLS证书与主机名相关联,并且该主机名通常不是&#34; localhost&#34;如果你想做任何有用的事情。假设证书不是明显的过期错误,请在连接到服务器时尝试使用正确的主机名。