@ -1,12 +1,10 @@
import test from 'ava' ;
import nock from 'nock' ;
import subSeconds from 'date-fns/sub_seconds' ;
import delay from 'delay' ;
import Onionoo from '../' ;
import data from './fixtures/data' ;
test ( 'Cache can be disabled ' , async t => {
const onionoo = new Onionoo ( { cache : false } ) ;
test ( 'Cache is disabled by default ' , async t => {
const onionoo = new Onionoo ( ) ;
const defaultEndpoint = data . defaultEndpoints [ 0 ] ;
const responseHeaders = {
@ -21,7 +19,7 @@ test('Cache can be disabled', async t => {
const response = await onionoo [ defaultEndpoint ] ( ) ;
t . deepEqual ( response . body , data . dummyRespons e) ;
t . false ( response . fromCach e) ;
t . truthy ( scope . isDone ( ) ) ;
const scope2 = nock ( data . defaultBaseUrl )
@ -30,12 +28,12 @@ test('Cache can be disabled', async t => {
const response2 = await onionoo [ defaultEndpoint ] ( ) ;
t . deepEqual ( response2 . body , data . dummyRespons e) ;
t . false ( response2 . fromCach e) ;
t . truthy ( scope2 . isDone ( ) ) ;
} ) ;
test ( 'Responses with future max-age are cached ' , async t => {
const onionoo = new Onionoo ( ) ;
test ( 'Cache options is passed through to Got ' , async t => {
const onionoo = new Onionoo ( { cache : new Map ( ) } ) ;
const defaultEndpoint = data . defaultEndpoints [ 0 ] ;
const responseHeaders = {
@ -50,84 +48,10 @@ test('Responses with future max-age are cached', async t => {
const response = await onionoo [ defaultEndpoint ] ( ) ;
t . deepEqual ( response . body , data . dummyRespons e) ;
t . false ( response . fromCach e) ;
t . truthy ( scope . isDone ( ) ) ;
const cachedResponse = await onionoo [ defaultEndpoint ] ( ) ;
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 ( ) ) ;
} ) ;
test ( 'When expired, add last-modified date to headers and handle 304' , async t => {
const onionoo = new Onionoo ( ) ;
const defaultEndpoint = data . defaultEndpoints [ 0 ] ;
const initialDate = new Date ( ) . toUTCString ( ) ;
const responseHeaders = {
date : initialDate ,
age : 0 ,
'cache-control' : 'public, max-age=1' ,
'last-modified' : initialDate
} ;
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 requestHeaders = {
'if-modified-since' : initialDate
} ;
const responseHeaders304 = {
date : new Date ( ) . toUTCString ( ) ,
age : 0 ,
'cache-control' : 'public, max-age=10' ,
'last-modified' : initialDate
} ;
const scope2 = nock ( data . defaultBaseUrl , { requestHeaders } )
. get ( ` / ${ defaultEndpoint } ` )
. reply ( 304 , null , responseHeaders304 ) ;
const response2 = await delay ( 2000 ) . then ( onionoo [ defaultEndpoint ] ) ;
t . deepEqual ( response2 . body , data . dummyResponse ) ;
t . truthy ( scope2 . isDone ( ) ) ;
const cachedResponse = await onionoo [ defaultEndpoint ] ( ) ;
t . deepEqual ( cachedResponse . body , data . dummyResponse ) ;
t . true ( response2 . fromCache ) ;
} ) ;