0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/test/unit/api/shared/http.test.js
Naz 76aa2479f8 Added 'content-version' header response
refs https://github.com/TryGhost/Toolbox/issues/280

- In response to 'Accept-Version' header in the request headers, Ghost will always respond with a content-version header indicating the version of the Ghost install that is responding. This should signal to the client the content version that is bein g served
- This is a bare bones implementation and more logic with edge cases where `content-version` is served with a  version value of "best format API could respond with" will be added later.
2022-04-08 20:41:54 +12:00

113 lines
3 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/canary/',
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);
});
});