我正在尝试使用 frisby.js 编写一个测试用例来检查证书的状态。测试应该在证书到期前 n 天失败,其中 n 定义在:
config.numberOfDaysBeforeTestFails
我用这段代码试了一下:
var frisby = require('frisby');
var config = require('../config'); //load own config-file
var request = require('request');
frisby.create('https2.0 - Perperation')
.get(config.server + '/testData')
.auth(config.username, config.passwort)
.after(function(err, res, body){
var auth = "Basic " + new Buffer(config.username + ":" + config.passwort).toString("base64");
var r = request({
url: '<serverURL>',
requestCert: true,
rejectUnauthorized: false,
headers : {
"Authorization" : auth
}
});
r.on('response', function(res) {
var certificateInformation = res.req.connection.getPeerCertificate();
var certificateDate = new Date(Date.parse(certificateInformation.valid_to));
var todayDate = new Date();
todayDate.setDate(todayDate.getDate() + config.numberOfDaysBeforeTestFails);
//Below does not get executed
expect(todayDate < certificateDate).toBe(true);
});
})
.toss();
问题是,期望没有得到 jasmine-node 测试运行程序的验证。执行此代码
jasmine-node ./https_spec.js
将导致:
Finished in 0.358 seconds
1 test, 0 assertions, 0 failures, 0 skipped
所以断言
expect(todayDate < certificateDate).toBe(true);
没有被执行。我假设,这是因为 nodejs 异步执行代码,因此测试在断言执行之前结束。
有人知道,我怎样才能强制执行这个断言并进入测试结果?
谢谢和问候,乔