Browse Source

Test responses older than max-age are not cached

pull/15/head
Luke Childs 8 years ago
parent
commit
72b2d16c6a
  1. 1
      package.json
  2. 30
      test/cache.js

1
package.json

@ -34,6 +34,7 @@
"devDependencies": {
"ava": "^0.17.0",
"coveralls": "^2.11.15",
"date-fns": "^1.21.1",
"nock": "^9.0.2",
"nyc": "^10.0.0",
"snazzy": "^5.0.0"

30
test/cache.js

@ -1,5 +1,6 @@
import test from 'ava'
import nock from 'nock'
import subSeconds from 'date-fns/sub_seconds'
import data from './fixtures/data'
import Onionoo from '../'
@ -55,3 +56,32 @@ test('Responses with future max-age are cached', async t => {
t.deepEqual(cachedResponse.body, data.dummyResponse)
})
test('Responses older than max-age are not cached', async t => {
const onionoo = new Onionoo()
const defaultEndpoint = data.defaultEndpoints[0]
const responseHeaders = {
date: subSeconds(new Date(), 15).toUTCString(),
age: 0,
'cache-control': 'public, max-age=10'
}
const scope = nock(data.defaultBaseUrl)
.get(`/${defaultEndpoint}`)
.reply(200, data.dummyResponse, responseHeaders)
const response = await onionoo[defaultEndpoint]()
t.deepEqual(response.body, data.dummyResponse)
t.truthy(scope.isDone())
const scope2 = nock(data.defaultBaseUrl)
.get(`/${defaultEndpoint}`)
.reply(200, data.dummyResponse, responseHeaders)
const response2 = await onionoo[defaultEndpoint]()
t.deepEqual(response2.body, data.dummyResponse)
t.truthy(scope2.isDone())
})

Loading…
Cancel
Save