test(docs-infra): correctly extract sitemap URLs (#19795)
`%%DEPLOYMENT_HOST%%` has been assumed to be the host prefix for sitemap
URLs since bf29936af
, but afaict this was never the case.
PR Close #19795
This commit is contained in:
parent
9bcd8c2425
commit
66ffa360df
|
@ -3,6 +3,8 @@ import { SitePage } from './site.po';
|
||||||
|
|
||||||
describe(browser.baseUrl, () => {
|
describe(browser.baseUrl, () => {
|
||||||
const page = new SitePage();
|
const page = new SitePage();
|
||||||
|
const getCurrentUrl = async () => (await browser.getCurrentUrl()).replace(/\?.*$/, '');
|
||||||
|
const prependBaseUrl = (url: string) => browser.baseUrl.replace(/\/$/, '') + url;
|
||||||
|
|
||||||
beforeAll(done => page.init().then(done));
|
beforeAll(done => page.init().then(done));
|
||||||
|
|
||||||
|
@ -14,8 +16,8 @@ describe(browser.baseUrl, () => {
|
||||||
it(`should not redirect '${url}' (${i + 1}/${page.sitemapUrls.length})`, async () => {
|
it(`should not redirect '${url}' (${i + 1}/${page.sitemapUrls.length})`, async () => {
|
||||||
await page.goTo(url);
|
await page.goTo(url);
|
||||||
|
|
||||||
const expectedUrl = browser.baseUrl + url;
|
const expectedUrl = prependBaseUrl(url);
|
||||||
const actualUrl = (await browser.getCurrentUrl()).replace(/\?.*$/, '');
|
const actualUrl = await getCurrentUrl();
|
||||||
|
|
||||||
expect(actualUrl).toBe(expectedUrl);
|
expect(actualUrl).toBe(expectedUrl);
|
||||||
});
|
});
|
||||||
|
@ -27,8 +29,8 @@ describe(browser.baseUrl, () => {
|
||||||
it(`should redirect '${fromUrl}' to '${toUrl}' (${i + 1}/${page.legacyUrls.length})`, async () => {
|
it(`should redirect '${fromUrl}' to '${toUrl}' (${i + 1}/${page.legacyUrls.length})`, async () => {
|
||||||
await page.goTo(fromUrl);
|
await page.goTo(fromUrl);
|
||||||
|
|
||||||
const expectedUrl = (/^http/.test(toUrl) ? '' : browser.baseUrl.replace(/\/$/, '')) + toUrl;
|
const expectedUrl = /^http/.test(toUrl) ? toUrl : prependBaseUrl(toUrl);
|
||||||
const actualUrl = (await browser.getCurrentUrl()).replace(/\?.*$/, '');
|
const actualUrl = await getCurrentUrl();
|
||||||
|
|
||||||
expect(actualUrl).toBe(expectedUrl);
|
expect(actualUrl).toBe(expectedUrl);
|
||||||
});
|
});
|
||||||
|
|
|
@ -43,9 +43,7 @@ export async function loadRemoteSitemapUrls(host: string) {
|
||||||
.on('error', reject));
|
.on('error', reject));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Currently, all sitemaps use `angular.io` as host in URLs (which is fine since we only use the
|
return extractSitemapUrls(xml);
|
||||||
// sitemap `angular.io`). See also `aio/src/extra-files/*/robots.txt`.
|
|
||||||
return extractSitemapUrls(xml, 'https://angular.io/');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loadSWRoutes() {
|
export function loadSWRoutes() {
|
||||||
|
@ -69,8 +67,22 @@ export function loadSWRoutes() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private functions
|
// Private functions
|
||||||
function extractSitemapUrls(xml: string, host = '%%DEPLOYMENT_HOST%%') {
|
function extractSitemapUrls(xml: string) {
|
||||||
|
// Currently, all sitemaps use `angular.io` as host in URLs (which is fine since we only use the
|
||||||
|
// sitemap in `angular.io`). See also `aio/src/extra-files/*/robots.txt`.
|
||||||
|
const host = 'https://angular.io';
|
||||||
const urls: string[] = [];
|
const urls: string[] = [];
|
||||||
|
|
||||||
xml.replace(/<loc>([^<]+)<\/loc>/g, (_, loc) => urls.push(loc.replace(host, '')) as any);
|
xml.replace(/<loc>([^<]+)<\/loc>/g, (_, loc) => urls.push(loc.replace(host, '')) as any);
|
||||||
|
|
||||||
|
// Ensure none of the URLs contains the scheme/host.
|
||||||
|
// (That would mean that the URL contains a different than expected host, which can in turn lead
|
||||||
|
// to tests passing while they shouldn't).
|
||||||
|
urls.forEach(url => {
|
||||||
|
if (url.includes('://')) {
|
||||||
|
throw new Error(`Sitemap URL (${url}) contains unexpected host. Expected: ${host}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return urls;
|
return urls;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue