2018-10-05 01:50:45 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
2022-08-11 17:39:37 +03:00
|
|
|
const shared = require('../');
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
describe('Unit: api/shared/http', function () {
|
|
|
|
let req;
|
|
|
|
let res;
|
|
|
|
let next;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
req = sinon.stub();
|
|
|
|
res = sinon.stub();
|
|
|
|
next = sinon.stub();
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
req.body = {
|
|
|
|
a: 'a'
|
|
|
|
};
|
2020-09-14 13:33:37 +03:00
|
|
|
req.vhost = {
|
|
|
|
host: 'example.com'
|
|
|
|
};
|
2022-04-06 15:36:12 +03:00
|
|
|
req.url = 'https://example.com/ghost/api/content/',
|
2018-10-05 01:50:45 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
res.status = sinon.stub();
|
|
|
|
res.json = sinon.stub();
|
2022-04-06 11:12:20 +03:00
|
|
|
res.set = (headers) => {
|
|
|
|
res.headers = headers;
|
|
|
|
};
|
2019-01-21 19:53:44 +03:00
|
|
|
res.send = sinon.stub();
|
2018-10-05 01:50:45 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(shared.headers, 'get').resolves();
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('check options', function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
const apiImpl = sinon.stub().resolves();
|
2018-10-05 01:50:45 +03:00
|
|
|
shared.http(apiImpl)(req, res, next);
|
|
|
|
|
|
|
|
Object.keys(apiImpl.args[0][0]).should.eql([
|
|
|
|
'original',
|
|
|
|
'options',
|
|
|
|
'data',
|
|
|
|
'user',
|
|
|
|
'file',
|
2019-02-13 18:59:10 +03:00
|
|
|
'files',
|
|
|
|
'apiType'
|
2018-10-05 01:50:45 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
apiImpl.args[0][0].data.should.eql({a: 'a'});
|
|
|
|
apiImpl.args[0][0].options.should.eql({
|
|
|
|
context: {
|
2019-01-24 17:19:34 +03:00
|
|
|
api_key: null,
|
2019-01-28 12:08:21 +03:00
|
|
|
integration: null,
|
2018-11-07 14:01:27 +03:00
|
|
|
user: null,
|
|
|
|
member: null
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('api response is fn', function (done) {
|
2020-10-19 07:45:26 +03:00
|
|
|
const response = sinon.stub().callsFake(function (_req, _res, _next) {
|
|
|
|
should.exist(_req);
|
|
|
|
should.exist(_res);
|
|
|
|
should.exist(_next);
|
2018-10-05 01:50:45 +03:00
|
|
|
apiImpl.calledOnce.should.be.true();
|
2020-10-19 07:45:26 +03:00
|
|
|
_res.json.called.should.be.false();
|
2018-10-05 01:50:45 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
const apiImpl = sinon.stub().resolves(response);
|
2018-10-05 01:50:45 +03:00
|
|
|
shared.http(apiImpl)(req, res, next);
|
|
|
|
});
|
|
|
|
|
2022-08-11 17:39:37 +03:00
|
|
|
it('api response is fn (data)', function (done) {
|
2019-01-21 19:53:44 +03:00
|
|
|
const apiImpl = sinon.stub().resolves('data');
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
next.callsFake(done);
|
|
|
|
|
|
|
|
res.json.callsFake(function () {
|
|
|
|
shared.headers.get.calledOnce.should.be.true();
|
|
|
|
res.status.calledOnce.should.be.true();
|
|
|
|
res.send.called.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
shared.http(apiImpl)(req, res, next);
|
|
|
|
});
|
|
|
|
});
|