2

我正在尝试使用 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 异步执行代码,因此测试在断言执行之前结束。

有人知道,我怎样才能强制执行这个断言并进入测试结果?

谢谢和问候,乔

4

1 回答 1

0

我没有找到基于 frisby.js 的解决方案,但是使用标准的 jasmine-syntax 来支持异步,它似乎可以工作。而且由于 jasmine-node 运行器正在执行 frisby 测试,因此我可以将其与其他 frisby 测试一起执行。

var request = require('request');    
var config = require('../config');  //load own config-file for global settings

describe("https 2.0", function() {

 it("Certificate Test", function(done) {
    var auth = "Basic " + new Buffer(config.username + ":" + config.passwort).toString("base64");
    var r = request({
        url: config.server,
        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);
        expect(todayDate).toBeLessThan(certificateDate);
        done();
    });
  });
});
于 2015-12-29T15:35:53.693 回答