q3-core-responder
Advanced tools
Comparing version 1.0.33 to 1.0.34
@@ -6,8 +6,15 @@ const MockApi = require('q3-test-utils/helpers/apiMock'); | ||
const { req, res } = new MockApi(); | ||
const today = moment(); | ||
const previousYesterday = moment().subtract(2, 'days'); | ||
beforeEach(() => { | ||
req.headers = {}; | ||
res.status = jest.fn().mockReturnValue({ | ||
send: jest.fn(), | ||
}); | ||
}); | ||
describe('Status decorator', () => { | ||
it('should set the last modified date', () => { | ||
Decorator(req, res, jest.fn()); | ||
const today = moment(); | ||
res.set = jest.fn(); | ||
@@ -26,5 +33,3 @@ req.marshal({ | ||
Decorator(req, res, jest.fn()); | ||
const previousYesterday = moment().subtract(2, 'days'); | ||
const yesterday = moment().subtract(1, 'days'); | ||
const today = moment(); | ||
@@ -40,5 +45,40 @@ res.set = jest.fn(); | ||
'Last-Modified', | ||
today, | ||
today.toISOString(), | ||
); | ||
}); | ||
describe('isFresh', () => { | ||
it('should response with 412', () => { | ||
Decorator(req, res, jest.fn()); | ||
req.headers = {}; | ||
res.status = jest.fn().mockReturnValue({ | ||
send: jest.fn(), | ||
}); | ||
req.headers[ | ||
'if-unmodified-since' | ||
] = previousYesterday; | ||
req.isFresh(today); | ||
expect(res.status).toHaveBeenCalledWith(412); | ||
}); | ||
it('should respond as truthy', () => { | ||
Decorator(req, res, jest.fn()); | ||
req.headers = {}; | ||
res.status = jest.fn().mockReturnValue({ | ||
send: jest.fn(), | ||
}); | ||
req.headers['if-unmodified-since'] = today; | ||
expect(req.isFresh(previousYesterday)).toBeTruthy(); | ||
}); | ||
it('should respond as truthy in equal-to situations', () => { | ||
Decorator(req, res, jest.fn()); | ||
req.headers['if-unmodified-since'] = today; | ||
expect(req.isFresh(today)).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
@@ -17,8 +17,10 @@ const etag = require('etag'); | ||
const getLastModifiedDate = (arr) => | ||
arr.reduce((a, c) => { | ||
const d = typeof c === 'object' ? c.updatedAt : null; | ||
moment( | ||
arr.reduce((a, c) => { | ||
const d = typeof c === 'object' ? c.updatedAt : null; | ||
if (!moment(d).isValid()) return a; | ||
return moment(a).isAfter(d) ? a : d; | ||
}, ''); | ||
if (!moment(d).isValid()) return a; | ||
return moment(a).isAfter(d) ? a : d; | ||
}, ''), | ||
).toISOString(); | ||
@@ -65,6 +67,8 @@ const stripMongoDBProps = (i) => { | ||
req.isFresh = (d) => { | ||
const unmod = req.headers['if-unmodified-since']; | ||
return unmod && | ||
moment(unmod).isValid() && | ||
moment(d).isValid() && | ||
const unmod = | ||
req.headers['If-Unmodified-Since'] || | ||
req.headers['if-unmodified-since']; | ||
return moment(unmod, moment.ISO_8601, true).isValid() && | ||
moment(d, moment.ISO_8601, true).isValid() && | ||
moment(d).isAfter(new Date(unmod).toISOString()) | ||
@@ -71,0 +75,0 @@ ? res.status(412).send() |
{ | ||
"name": "q3-core-responder", | ||
"main": "lib/index.js", | ||
"version": "1.0.33", | ||
"version": "1.0.34", | ||
"peerDependencies": { | ||
@@ -9,3 +9,3 @@ "express": "^4.17.1" | ||
"devDependencies": { | ||
"q3-test-utils": "^1.0.44", | ||
"q3-test-utils": "^1.0.45", | ||
"supertest": "^4.0.2" | ||
@@ -25,3 +25,3 @@ }, | ||
}, | ||
"gitHead": "f1afad1cfff5f3d9358fb2de53084626c106bb1e" | ||
"gitHead": "ededcf2a80059e6c76de1479f38bbbea055f985f" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10866
344