2023-10-24 14:22:28 -04:00
|
|
|
const defaults = require('lodash.defaults');
|
|
|
|
const fastlyConfig = require('../../bin/lib/fastly-config-methods');
|
|
|
|
const routeJson = require('../../src/routes.json');
|
|
|
|
const tap = require('tap');
|
2019-07-17 14:56:48 -04:00
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
const testRoutes = [
|
2019-07-17 14:56:48 -04:00
|
|
|
{
|
|
|
|
name: 'less-traveled',
|
|
|
|
pattern: '^/?$',
|
|
|
|
routeAlias: '/?$',
|
|
|
|
view: 'less-traveled/less-traveled',
|
|
|
|
title: 'Robert Frost Goes Here'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'more-traveled',
|
|
|
|
pattern: '^/more?$',
|
|
|
|
routeAlias: '/more?$',
|
|
|
|
view: 'more-traveled/more-traveled',
|
|
|
|
title: 'Robert Frost Does Not Go Here'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
const routes = routeJson.map(route =>
|
|
|
|
defaults({}, {pattern: fastlyConfig.expressPatternToRegex(route.pattern)}, route)
|
|
|
|
);
|
|
|
|
const extraAppRoutes = [
|
2019-07-17 14:56:48 -04:00
|
|
|
// Homepage with querystring.
|
|
|
|
// TODO: Should this be added for every route?
|
|
|
|
'/\\?',
|
|
|
|
// View html
|
|
|
|
'/[^/]*.html$'
|
|
|
|
];
|
|
|
|
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
tap.test('getStaticPaths', t => {
|
|
|
|
const staticPaths = fastlyConfig.getStaticPaths(__dirname, '../../build/*');
|
2019-07-17 14:56:48 -04:00
|
|
|
t.type(staticPaths, 'object');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
tap.test('getViewPaths', t => {
|
|
|
|
const viewPaths = fastlyConfig.getViewPaths(testRoutes);
|
2019-07-17 14:56:48 -04:00
|
|
|
t.type(viewPaths, 'object');
|
|
|
|
t.equal(viewPaths[0], '/?$');
|
|
|
|
t.equal(viewPaths[1], '/more?$');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
tap.test('pathsToCondition', t => {
|
|
|
|
const condition = fastlyConfig.pathsToCondition(['/?$', '/more?$']);
|
2019-07-17 14:56:48 -04:00
|
|
|
t.type(condition, 'string');
|
|
|
|
t.equal(condition, 'req.url~"^(/?$|/more?$)"');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
tap.test('getAppRouteCondition', t => {
|
|
|
|
const condition = fastlyConfig.getAppRouteCondition('../../build/*', routes, extraAppRoutes, __dirname);
|
2019-07-17 14:56:48 -04:00
|
|
|
t.type(condition, 'string');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
tap.test('testSetTTL', t => {
|
|
|
|
const ttl = fastlyConfig.setResponseTTL('itsactuallyttyl');
|
2019-07-17 14:56:48 -04:00
|
|
|
t.equal(ttl, '' +
|
|
|
|
'if (itsactuallyttyl) {\n' +
|
|
|
|
' if (req.url ~ "^(/projects/|/fragment/account-nav.json|/session/)" && ' +
|
|
|
|
'!req.http.Cookie:scratchsessionsid) {\n' +
|
|
|
|
' set beresp.http.Vary = "Accept-Encoding, Accept-Language";\n' +
|
|
|
|
' unset beresp.http.set-cookie;\n' +
|
|
|
|
' return(deliver);\n' +
|
|
|
|
' } else {\n' +
|
|
|
|
' set beresp.ttl = 0s;\n' +
|
|
|
|
' set beresp.grace = 0s;\n' +
|
|
|
|
' return(pass);\n' +
|
|
|
|
' }\n' +
|
|
|
|
'}\n'
|
|
|
|
);
|
|
|
|
t.end();
|
|
|
|
});
|