There's a lot going on in this PR, though it's all interdependent, so the only way I can see to break it up into smaller pieces would be serially.
1. I completely refactored the functions for managing cache headers. These have been added to `services/cache-headers.js`, and in some ways set the stage for the rest of this PR.
- There are ample higher-level test of the functionality via `request-handler`. Refactoring these tests was deferred. Cache headers were previously dealt with in three places:
- `request-handler.js`, for the dynamic badges. This function now calls `setCacheHeaders`.
- `base-static.js`, for the static badges. This method now calls the wordy `serverHasBeenUpSinceResourceCached` and `setCacheHeadersForStaticResource`.
- The bitFlip badge in `server.js`. 👈 This is what set all this in motion. This badge has been refactored to a new-style service based on a new `NoncachingBaseService` which does not use the Shields in-memory cache that the dynamic badges user.
- I'm open to clearer names for `NoncachingBaseService`, which is kind of terrible. Absent alternatives, I wrote a short essay of clarification in the docstring. 😝
2. In the process of writing `NoncachingBaseService`, I discovered it takes several lines of code to instantiate and invoke a service. These would be duplicated in three or four places in production code, and in lots and lots of tests. I kept the line that goes from regex to namedParams (for reasons) and moved the rest into a static method called `invoke()`, which instantiates and invokes the service. This _replaced_ the instance method `invokeHandler`.
- I gently reworked the unit tests to use `invoke` instead of `invokeHandler`– generally for the better.
- I made a small change to `BaseStatic`. Now it invokes `handle()` async as the dynamic badges do. This way it could use `BaseService.invoke()`.
3. There was logic in `request-handler` for processing environment variables, validating them, and setting defaults. This could have been lifted whole-hog to `services/cache-headers.js`, though I didn't do that. Instead I moved it to `server-config.js`. Ideally `server-config` is the only module that should access `process.env`. This puts the defaults and config validation in one place, decouples the config schema from the entire rest of the application, and significantly simplifies our ability to test different configs, particularly on small units of code. (We were doing this well enough before in `request-handler.spec`, though it required mutating the environment, which was kludgy.) Some of the `request-handler` tests could be rewritten at a higher level, with lower-level data-driven tests directly against `cache-headers`.
174 lines
5.3 KiB
JavaScript
174 lines
5.3 KiB
JavaScript
'use strict'
|
|
|
|
const { test, given } = require('sazerac')
|
|
const chai = require('chai')
|
|
const { expect } = require('chai')
|
|
const sinon = require('sinon')
|
|
const httpMocks = require('node-mocks-http')
|
|
const {
|
|
coalesceCacheLength,
|
|
setHeadersForCacheLength,
|
|
setCacheHeaders,
|
|
setCacheHeadersForStaticResource,
|
|
serverHasBeenUpSinceResourceCached,
|
|
} = require('./cache-headers')
|
|
|
|
chai.use(require('chai-datetime'))
|
|
|
|
describe('Cache header functions', function() {
|
|
let res
|
|
beforeEach(function() {
|
|
res = httpMocks.createResponse()
|
|
})
|
|
|
|
describe('coalesceCacheLength', function() {
|
|
const exampleCacheConfig = { defaultCacheLengthSeconds: 777 }
|
|
test(coalesceCacheLength, () => {
|
|
given(exampleCacheConfig, undefined, {}).expect(777)
|
|
given(exampleCacheConfig, 900, {}).expect(900)
|
|
given(exampleCacheConfig, 900, { maxAge: 1000 }).expect(1000)
|
|
given(exampleCacheConfig, 900, { maxAge: 400 }).expect(900)
|
|
given(exampleCacheConfig, 900, { maxAge: '-1000' }).expect(900)
|
|
given(exampleCacheConfig, 900, { maxAge: '' }).expect(900)
|
|
given(exampleCacheConfig, 900, { maxAge: 'not a number' }).expect(900)
|
|
})
|
|
})
|
|
|
|
describe('setHeadersForCacheLength', function() {
|
|
let sandbox
|
|
beforeEach(function() {
|
|
sandbox = sinon.createSandbox()
|
|
sandbox.useFakeTimers()
|
|
})
|
|
afterEach(function() {
|
|
sandbox.restore()
|
|
sandbox = undefined
|
|
})
|
|
|
|
it('should set the correct Date header', function() {
|
|
// Confidence check.
|
|
expect(res._headers.date).to.equal(undefined)
|
|
|
|
// Act.
|
|
setHeadersForCacheLength(res, 123)
|
|
|
|
// Assert.
|
|
const now = new Date().toGMTString()
|
|
expect(res._headers.date).to.equal(now)
|
|
})
|
|
|
|
context('cacheLengthSeconds is zero', function() {
|
|
beforeEach(function() {
|
|
setHeadersForCacheLength(res, 0)
|
|
})
|
|
|
|
it('should set the expected Cache-Control header', function() {
|
|
expect(res._headers['cache-control']).to.equal(
|
|
'no-cache, no-store, must-revalidate'
|
|
)
|
|
})
|
|
|
|
it('should set the expected Expires header', function() {
|
|
expect(res._headers.expires).to.equal(new Date().toGMTString())
|
|
})
|
|
})
|
|
|
|
context('cacheLengthSeconds is nonzero', function() {
|
|
beforeEach(function() {
|
|
setHeadersForCacheLength(res, 123)
|
|
})
|
|
|
|
it('should set the expected Cache-Control header', function() {
|
|
expect(res._headers['cache-control']).to.equal('max-age=123')
|
|
})
|
|
|
|
it('should set the expected Expires header', function() {
|
|
const expires = new Date(Date.now() + 123 * 1000).toGMTString()
|
|
expect(res._headers.expires).to.equal(expires)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('setCacheHeaders', function() {
|
|
it('sets the expected fields', function() {
|
|
const expectedFields = ['date', 'cache-control', 'expires']
|
|
expectedFields.forEach(field =>
|
|
expect(res._headers[field]).to.equal(undefined)
|
|
)
|
|
|
|
setCacheHeaders({
|
|
cacheHeaderConfig: { defaultCacheLengthSeconds: 1234 },
|
|
serviceCacheLengthSeconds: 567,
|
|
queryParams: { maxAge: 999999 },
|
|
res,
|
|
})
|
|
|
|
expectedFields.forEach(field =>
|
|
expect(res._headers[field])
|
|
.to.be.a('string')
|
|
.and.have.lengthOf.at.least(1)
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('setCacheHeadersForStaticResource', function() {
|
|
beforeEach(function() {
|
|
setCacheHeadersForStaticResource(res)
|
|
})
|
|
|
|
it('should set the expected Cache-Control header', function() {
|
|
expect(res._headers['cache-control']).to.equal(`max-age=${24 * 3600}`)
|
|
})
|
|
|
|
it('should set the expected Last-Modified header', function() {
|
|
const lastModified = res._headers['last-modified']
|
|
expect(new Date(lastModified)).to.be.withinTime(
|
|
// Within the last 60 seconds.
|
|
new Date(Date.now() - 60 * 1000),
|
|
new Date()
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('serverHasBeenUpSinceResourceCached', function() {
|
|
// The stringified req's are hard to understand. I thought Sazerac
|
|
// provided a way to override the describe message, though I can't find it.
|
|
context('when there is no If-Modified-Since header', function() {
|
|
it('returns false', function() {
|
|
const req = httpMocks.createRequest()
|
|
expect(serverHasBeenUpSinceResourceCached(req)).to.equal(false)
|
|
})
|
|
})
|
|
context('when the If-Modified-Since header is invalid', function() {
|
|
it('returns false', function() {
|
|
const req = httpMocks.createRequest({
|
|
headers: { 'If-Modified-Since': 'this-is-not-a-date' },
|
|
})
|
|
expect(serverHasBeenUpSinceResourceCached(req)).to.equal(false)
|
|
})
|
|
})
|
|
context(
|
|
'when the If-Modified-Since header is before the process started',
|
|
function() {
|
|
it('returns false', function() {
|
|
const req = httpMocks.createRequest({
|
|
headers: { 'If-Modified-Since': '2018-02-01T05:00:00.000Z' },
|
|
})
|
|
expect(serverHasBeenUpSinceResourceCached(req)).to.equal(false)
|
|
})
|
|
}
|
|
)
|
|
context(
|
|
'when the If-Modified-Since header is after the process started',
|
|
function() {
|
|
it('returns true', function() {
|
|
const req = httpMocks.createRequest({
|
|
headers: { 'If-Modified-Since': new Date().toGMTString() },
|
|
})
|
|
expect(serverHasBeenUpSinceResourceCached(req)).to.equal(true)
|
|
})
|
|
}
|
|
)
|
|
})
|
|
})
|