build(aio): test Service Worker "routing" configuration (#21763)
PR Close #21763
This commit is contained in:
parent
bf29936af9
commit
240aed29e0
|
@ -44,7 +44,7 @@
|
|||
"docs-watch": "node tools/transforms/authors-package/watchr.js",
|
||||
"docs-lint": "eslint --ignore-path=\"tools/transforms/.eslintignore\" tools/transforms",
|
||||
"docs-test": "node tools/transforms/test.js",
|
||||
"deployment-config-test": "jasmine-ts tests/deployment/*.spec.ts",
|
||||
"deployment-config-test": "jasmine-ts tests/deployment/**/*.spec.ts",
|
||||
"firebase-utils-test": "jasmine-ts tools/firebase-test-utils/*.spec.ts",
|
||||
"tools-lint": "tslint -c \"tools/tslint.json\" \"tools/firebase-test-utils/**/*.ts\"",
|
||||
"tools-test": "./scripts/deploy-to-firebase.test.sh && yarn docs-test && yarn boilerplate:test && jasmine tools/ng-packages-installer/index.spec.js && yarn firebase-utils-test",
|
||||
|
|
|
@ -27,3 +27,23 @@ export function loadLegacyUrls() {
|
|||
const urls = readFileSync(pathToLegacyUrls, 'utf8').split('\n').map(line => line.split('\t'));
|
||||
return urls;
|
||||
}
|
||||
|
||||
export function loadSWRoutes() {
|
||||
const pathToSWManifest = path.resolve(__dirname, '../../ngsw-manifest.json');
|
||||
const contents = cjson.load(pathToSWManifest);
|
||||
const routes = contents.routing.routes;
|
||||
return Object.keys(routes).map(route => {
|
||||
const routeConfig = routes[route];
|
||||
switch (routeConfig.match) {
|
||||
case 'exact':
|
||||
return (url) => url === route;
|
||||
case 'prefix':
|
||||
return (url) => url.startsWith(route);
|
||||
case 'regex':
|
||||
const regex = new RegExp(route);
|
||||
return (url) => regex.test(url);
|
||||
default:
|
||||
throw new Error(`unknown route config: ${route} - ${routeConfig.match}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
import { loadLegacyUrls, loadSitemapUrls, loadSWRoutes } from './helpers';
|
||||
|
||||
describe('service-worker routes', () => {
|
||||
|
||||
loadSitemapUrls().forEach(url => {
|
||||
it('should process URLs in the Sitemap', () => {
|
||||
const routes = loadSWRoutes();
|
||||
expect(routes.some(test => test(url))).toBeTruthy(url);
|
||||
});
|
||||
});
|
||||
|
||||
loadLegacyUrls().forEach(urlPair => {
|
||||
const url = urlPair[0];
|
||||
it('should ignore legacy URLs that will be redirected', () => {
|
||||
const routes = loadSWRoutes();
|
||||
expect(routes.some(test => test(url))).toBeFalsy(url);
|
||||
});
|
||||
});
|
||||
|
||||
it('should ignore stackblitz URLs', () => {
|
||||
const routes = loadSWRoutes();
|
||||
expect(routes.some(test => test('/generated/live-examples/toh-pt6/stackblitz.html'))).toBeFalsy();
|
||||
expect(routes.some(test => test('/generated/live-examples/toh-pt6/stackblitz'))).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should ignore URLs to files with extensions', () => {
|
||||
const routes = loadSWRoutes();
|
||||
expect(routes.some(test => test('/generated/zips/animations/animations.zip'))).toBeFalsy();
|
||||
expect(routes.some(test => test('/generated/images/guide/animations/animation_auto.gif'))).toBeFalsy();
|
||||
expect(routes.some(test => test('/generated/ie-polyfills.min.js'))).toBeFalsy();
|
||||
expect(routes.some(test => test('/generated/docs/guide/animations.json'))).toBeFalsy();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue