mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 14:17:22 +01:00
add basic test, disable etag on post routes as well, paypal ipn: prevent set headers after response error
This commit is contained in:
31
test/api/unit/middlewares/cache.test.js
Normal file
31
test/api/unit/middlewares/cache.test.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import {
|
||||||
|
generateRes,
|
||||||
|
generateReq,
|
||||||
|
generateNext,
|
||||||
|
} from '../../../helpers/api-unit.helper';
|
||||||
|
import {
|
||||||
|
disableCache,
|
||||||
|
} from '../../../../website/server/middlewares/cache';
|
||||||
|
|
||||||
|
describe('cache middlewares', () => {
|
||||||
|
let res; let req; let
|
||||||
|
next;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
req = generateReq();
|
||||||
|
res = generateRes();
|
||||||
|
next = generateNext();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('disableCache', () => {
|
||||||
|
it('sets the correct headers', () => {
|
||||||
|
disableCache(req, res, next);
|
||||||
|
expect(res.set).to.have.been.calledWith('Cache-Control', 'no-store');
|
||||||
|
expect(next).to.have.been.calledOnce;
|
||||||
|
});
|
||||||
|
|
||||||
|
xit('removes the etag header', () => {
|
||||||
|
// @TODO how to stub onHeaders
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
/* eslint-disable camelcase */
|
/* eslint-disable camelcase */
|
||||||
import paypalPayments from '../../../libs/payments/paypal';
|
import paypalPayments from '../../../libs/payments/paypal';
|
||||||
|
import logger from '../../../libs/logger';
|
||||||
import shared from '../../../../common';
|
import shared from '../../../../common';
|
||||||
import {
|
import {
|
||||||
authWithSession,
|
authWithSession,
|
||||||
@@ -171,7 +172,9 @@ api.ipn = {
|
|||||||
async handler (req, res) {
|
async handler (req, res) {
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
|
|
||||||
await paypalPayments.ipn(req.body);
|
paypalPayments
|
||||||
|
.ipn(req.body)
|
||||||
|
.catch(err => logger.error(err));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ export function readController (router, controller, overrides = []) {
|
|||||||
|
|
||||||
method = method.toLowerCase();
|
method = method.toLowerCase();
|
||||||
|
|
||||||
// all get routes with mandatory or optional authentication
|
// disable caching for all routes with mandatory or optional authentication
|
||||||
if (method === 'get' && authMiddlewareIndex !== -1) {
|
if (authMiddlewareIndex !== -1) {
|
||||||
middlewares.unshift(disableCache);
|
middlewares.unshift(disableCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ export function disableCache (req, res, next) {
|
|||||||
res.header('Cache-Control', 'no-store');
|
res.header('Cache-Control', 'no-store');
|
||||||
|
|
||||||
// Remove the etag header when caching is disabled
|
// Remove the etag header when caching is disabled
|
||||||
// Unfortunately it's not possible to prevent the creation right now
|
// @TODO Unfortunately it's not possible to prevent the creation right now
|
||||||
// See this issue https://github.com/expressjs/express/issues/2472
|
// See this issue https://github.com/expressjs/express/issues/2472
|
||||||
onHeaders(res, function removeEtag () {
|
onHeaders(res, function removeEtag () {
|
||||||
this.removeHeader('ETag');
|
this.removeHeader('ETag');
|
||||||
|
|||||||
Reference in New Issue
Block a user