mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-04 08:54:36 +03:00
a4a9ba7940
refs: https://github.com/TryGhost/Toolbox/issues/229 - we are getting rid of the concept of having multiple api versions in a single ghost install - removed all the code for multiple api versions & left canary wired up, but without the version in the URL - TODO: reorganise the folders so there's no canary folder when we're closer to shipping we need to minimise the pain of merging changes across from main for now
114 lines
3.0 KiB
JavaScript
114 lines
3.0 KiB
JavaScript
const should = require('should');
|
|
const sinon = require('sinon');
|
|
const shared = require('../../../../core/server/api/shared');
|
|
|
|
describe('Unit: api/shared/http', function () {
|
|
let req;
|
|
let res;
|
|
let next;
|
|
|
|
beforeEach(function () {
|
|
req = sinon.stub();
|
|
res = sinon.stub();
|
|
next = sinon.stub();
|
|
|
|
req.body = {
|
|
a: 'a'
|
|
};
|
|
req.vhost = {
|
|
host: 'example.com'
|
|
};
|
|
req.url = 'https://example.com/ghost/api/content/',
|
|
|
|
res.status = sinon.stub();
|
|
res.json = sinon.stub();
|
|
res.set = (headers) => {
|
|
res.headers = headers;
|
|
};
|
|
res.send = sinon.stub();
|
|
|
|
sinon.stub(shared.headers, 'get').resolves();
|
|
});
|
|
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
it('check options', function () {
|
|
const apiImpl = sinon.stub().resolves();
|
|
shared.http(apiImpl)(req, res, next);
|
|
|
|
Object.keys(apiImpl.args[0][0]).should.eql([
|
|
'original',
|
|
'options',
|
|
'data',
|
|
'user',
|
|
'file',
|
|
'files',
|
|
'apiType'
|
|
]);
|
|
|
|
apiImpl.args[0][0].data.should.eql({a: 'a'});
|
|
apiImpl.args[0][0].options.should.eql({
|
|
context: {
|
|
api_key: null,
|
|
integration: null,
|
|
user: null,
|
|
member: null
|
|
}
|
|
});
|
|
});
|
|
|
|
it('api response is fn', function (done) {
|
|
const response = sinon.stub().callsFake(function (_req, _res, _next) {
|
|
should.exist(_req);
|
|
should.exist(_res);
|
|
should.exist(_next);
|
|
apiImpl.calledOnce.should.be.true();
|
|
_res.json.called.should.be.false();
|
|
done();
|
|
});
|
|
|
|
const apiImpl = sinon.stub().resolves(response);
|
|
shared.http(apiImpl)(req, res, next);
|
|
});
|
|
|
|
it('api response is fn', function (done) {
|
|
const apiImpl = sinon.stub().resolves('data');
|
|
|
|
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);
|
|
});
|
|
|
|
it('adds content-version header to the response when accept-version header is present in the request', function (done) {
|
|
const apiImpl = sinon.stub().resolves('data');
|
|
req.headers = {
|
|
'accept-version': 'v5.1'
|
|
};
|
|
apiImpl.headers = {
|
|
'Content-Type': 'application/json'
|
|
};
|
|
res.locals = {
|
|
safeVersion: '5.4'
|
|
};
|
|
next.callsFake(done);
|
|
|
|
res.json.callsFake(function () {
|
|
shared.headers.get.calledOnce.should.be.true();
|
|
res.status.calledOnce.should.be.true();
|
|
res.headers['Content-Version'].should.equal('v5.4');
|
|
done();
|
|
});
|
|
|
|
shared.http(apiImpl)(req, res, next);
|
|
});
|
|
});
|