Ghost/test/unit/api/shared/http.test.js
Hannah Wolfe c6ae3c30d8 Moved content-version middleware onto api app
closes: https://github.com/TryGhost/Toolbox/issues/319

- at the moment, content-version is only set if one of our endpoints touches the request
  - this was demonstrated in the e2e tests, where many of the tests that set accept-version did not receive accept-version
- by moving the middleware out of the http module and onto the api app we ensure it's always done
- I put the code in the api-version-compatibility service to keep it all co-located
- ideally we will refactor that service slightly so it only exposes middleware
2022-05-02 19:05:14 +01:00

91 lines
2.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/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);
});
});