2

i am trying to write a testcase with frisby.js that checks the status of the certificate. The test should fail n days before the certificate expires with n being defined in:

config.numberOfDaysBeforeTestFails

I tried it with this code:

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();

The problem is, that the expect does not get validated by the jasmine-node testrunner. Executing this code with

jasmine-node ./https_spec.js

will result into:

Finished in 0.358 seconds
1 test, 0 assertions, 0 failures, 0 skipped

So the assertion

expect(todayDate < certificateDate).toBe(true);

is not being executed. I assume, this is because nodejs executes the code async and thus the test ends before the assertion gets executed.

Someone knows, how I can force this assertion to be executed and get into the test-result?

Thanks and greetings, Jo

pfs
  • 21
  • 3

1 Answers1

0

I did not find a solution based on frisby.js but with the standard jasmine-syntax for asynchronous support, it seems to works. And since frisby tests are getting executed by the jasmine-node runner, I can executed this together with my other frisby tests.

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();
    });
  });
});
pfs
  • 21
  • 3