Ghost/test/unit/api/shared/http.test.js
Hannah Wolfe f08a55c21f
Renamed tests to .test.js & updated commands
refs: https://github.com/TryGhost/Team/issues/856
refs: https://github.com/TryGhost/Team/issues/756

- The .test.js extension is better than _spec.js as it's more obvious that it's an extension
- It also meaans we can use the --extension parameter in mocha, which should result in a better default behaviour for `yarn test`
- It also highlights that some of our tests were named incorrectly and were not (and still will not be) run (see https://github.com/TryGhost/Team/issues/856)
- Note: even with this change, `yarn test` is throwing errors, I believe because of this issue https://github.com/TryGhost/Team/issues/756
2021-07-06 20:45:01 +01:00

89 lines
2.2 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 = sinon.stub();
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);
});
});