UNPKG

watchmen-ping-http-head-headers

Version:
63 lines (54 loc) 1.78 kB
var assert = require('assert'); var proxyquire = require('proxyquire'); describe('ping service http-head', function () { var service; beforeEach(function () { service = { url: 'http://apple.com', pingServiceOptions: { 'http-head': { statusCode: { descr: 'expected status code', required: false, value: 200 } } } } }); function loadHttpHeadServiceWithMockedResponse(statusCode) { var HTTPHeadService = proxyquire('../index', { 'request': { get: function (options, cb) { cb(null, {statusCode: statusCode}, ''); } } }); return new HTTPHeadService(); } it('should expect status code of 200 if no configuration is found', function (done) { delete service.pingServiceOptions['http-head']; var pingService = loadHttpHeadServiceWithMockedResponse(200); pingService.ping(service, function (error) { assert.ifError(error); done(); }); }); it('should fail if statusCode does not match', function (done) { service.pingServiceOptions['http-head'].statusCode.value = 200; var pingService = loadHttpHeadServiceWithMockedResponse(201); pingService.ping(service, function (error) { assert.ok(error.indexOf('Invalid status code') === 0); done(); }); }); it('should handle invalid configuration statusCode', function (done) { service.pingServiceOptions['http-head'].statusCode.value = 'notanumber'; var pingService = loadHttpHeadServiceWithMockedResponse(200); pingService.ping(service, function (error) { assert.ok(error.indexOf('Invalid status code') === 0); assert.ok(error.indexOf('Expected: NaN') > -1); done(); }); }); });