Refactored scheduling default test to use nock

refs https://github.com/TryGhost/Team/issues/860

- `nock` has been used in other similar unit tests. It was proven to have easy to understand and use API. By not making up custom code through express/httpServer stuff we reduce the amount of boilerplate code significantly - makes tests a lot more readable
- Bonus, removes couplint dependency to "shared" express module
This commit is contained in:
Naz 2021-07-15 14:07:11 +04:00
parent 9870aff8f0
commit 84d0815e47

View File

@ -2,9 +2,7 @@ const should = require('should');
const sinon = require('sinon'); const sinon = require('sinon');
const moment = require('moment'); const moment = require('moment');
const _ = require('lodash'); const _ = require('lodash');
const bodyParser = require('body-parser'); const nock = require('nock');
const http = require('http');
const express = require('../../../../core/shared/express');
const SchedulingDefault = require('../../../../core/server/adapters/scheduling/SchedulingDefault'); const SchedulingDefault = require('../../../../core/server/adapters/scheduling/SchedulingDefault');
describe('Scheduling Default Adapter', function () { describe('Scheduling Default Adapter', function () {
@ -239,163 +237,105 @@ describe('Scheduling Default Adapter', function () {
Object.keys(scope.adapter.deletedJobs).length.should.eql(0); Object.keys(scope.adapter.deletedJobs).length.should.eql(0);
}); });
it('pingUrl (PUT)', function (done) { describe('pingUrl', function () {
const app = express(); it('pingUrl (PUT)', function (done) {
const server = http.createServer(app); const ping = nock('http://localhost:1111')
let wasPinged = false; .put('/ping')
let reqBody; .query({})
.reply(200);
app.use(bodyParser.json()); scope.adapter._pingUrl({
url: 'http://localhost:1111/ping',
time: moment().add(1, 'second').valueOf(),
extra: {
httpMethod: 'PUT'
}
});
app.put('/ping', function (req, res) { (function retry() {
wasPinged = true; if (ping.isDone()) {
reqBody = req.body; done();
res.sendStatus(200); } else {
setTimeout(retry, 100);
}
})();
}); });
server.listen(1111); it('pingUrl (GET)', async function () {
const ping = nock('http://localhost:1111')
.get('/ping')
.query({})
.reply(200);
scope.adapter._pingUrl({ await scope.adapter._pingUrl({
url: 'http://localhost:1111/ping', url: 'http://localhost:1111/ping',
time: moment().add(1, 'second').valueOf(), time: moment().add(1, 'second').valueOf(),
extra: { extra: {
httpMethod: 'PUT' httpMethod: 'GET'
} }
});
ping.isDone().should.be.true();
}); });
(function retry() { it('pingUrl (PUT, and detect publish in the past)', async function () {
if (wasPinged) { const ping = nock('http://localhost:1111')
should.not.exist(reqBody.force); .put('/ping')
return server.close(done); .query({})
} .reply(200);
setTimeout(retry, 100); await scope.adapter._pingUrl({
})(); url: 'http://localhost:1111/ping',
}); time: moment().subtract(10, 'minutes').valueOf(),
extra: {
httpMethod: 'PUT'
}
});
it('pingUrl (GET)', function (done) { ping.isDone().should.be.true();
const app = express();
const server = http.createServer(app);
let wasPinged = false;
let reqQuery;
app.get('/ping', function (req, res) {
wasPinged = true;
reqQuery = req.query;
res.sendStatus(200);
}); });
server.listen(1111); it('pingUrl (GET, and detect publish in the past)', async function () {
const ping = nock('http://localhost:1111')
.get('/ping')
.query({force: true})
.reply(200);
scope.adapter._pingUrl({ await scope.adapter._pingUrl({
url: 'http://localhost:1111/ping', url: 'http://localhost:1111/ping',
time: moment().add(1, 'second').valueOf(), time: moment().subtract(10, 'minutes').valueOf(),
extra: { extra: {
httpMethod: 'GET' httpMethod: 'GET'
} }
}).then(() => { });
wasPinged.should.be.true();
should.not.exist(reqQuery.force);
server.close(done);
});
});
it('pingUrl (PUT, and detect publish in the past)', function (done) { ping.isDone().should.be.true();
const app = express();
const server = http.createServer(app);
let wasPinged = false;
let reqBody;
app.use(bodyParser.json());
app.put('/ping', function (req, res) {
wasPinged = true;
reqBody = req.body;
res.sendStatus(200);
}); });
server.listen(1111); it('pingUrl, but blog returns 503', function (done) {
scope.adapter.retryTimeoutInMs = 50;
scope.adapter._pingUrl({ const ping = nock('http://localhost:1111')
url: 'http://localhost:1111/ping', .put('/ping').reply(503)
time: moment().subtract(10, 'minutes').valueOf(), .put('/ping').reply(503)
extra: { .put('/ping', {force: true}).reply(200);
httpMethod: 'PUT'
} scope.adapter._pingUrl({
}).then(() => { url: 'http://localhost:1111/ping',
wasPinged.should.be.true(); time: moment().valueOf(),
should.exist(reqBody.force); extra: {
server.close(done); httpMethod: 'PUT'
}
});
(function retry() {
if (ping.isDone()) {
return done();
}
setTimeout(retry, 50);
}());
}); });
}); });
it('pingUrl (GET, and detect publish in the past)', function (done) {
const app = express();
const server = http.createServer(app);
let wasPinged = false;
let reqQuery;
app.get('/ping', function (req, res) {
wasPinged = true;
reqQuery = req.query;
res.sendStatus(200);
});
server.listen(1111);
scope.adapter._pingUrl({
url: 'http://localhost:1111/ping',
time: moment().subtract(10, 'minutes').valueOf(),
extra: {
httpMethod: 'GET'
}
}).then(() => {
wasPinged.should.be.true();
should.exist(reqQuery.force);
server.close(done);
});
});
it('pingUrl, but blog returns 503', function (done) {
const app = express();
const server = http.createServer(app);
let returned500Count = 0;
let returned200 = false;
let reqBody;
scope.adapter.retryTimeoutInMs = 10;
app.use(bodyParser.json());
app.put('/ping', function (req, res) {
reqBody = req.body;
if (returned500Count === 5) {
returned200 = true;
return res.sendStatus(200);
}
returned500Count = returned500Count + 1;
res.sendStatus(503);
});
server.listen(1111);
scope.adapter._pingUrl({
url: 'http://localhost:1111/ping',
time: moment().valueOf(),
extra: {
httpMethod: 'PUT'
}
});
(function retry() {
if (returned200) {
should.exist(reqBody.force);
return server.close(done);
}
setTimeout(retry, 50);
})();
});
}); });
}); });