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, () => {
|
||||
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));
|
||||
|
||||
|
@ -14,8 +16,8 @@ describe(browser.baseUrl, () => {
|
|||
it(`should not redirect '${url}' (${i + 1}/${page.sitemapUrls.length})`, async () => {
|
||||
await page.goTo(url);
|
||||
|
||||
const expectedUrl = browser.baseUrl + url;
|
||||
const actualUrl = (await browser.getCurrentUrl()).replace(/\?.*$/, '');
|
||||
const expectedUrl = prependBaseUrl(url);
|
||||
const actualUrl = await getCurrentUrl();
|
||||
|
||||
expect(actualUrl).toBe(expectedUrl);
|
||||
});
|
||||
|
@ -27,8 +29,8 @@ describe(browser.baseUrl, () => {
|
|||
it(`should redirect '${fromUrl}' to '${toUrl}' (${i + 1}/${page.legacyUrls.length})`, async () => {
|
||||
await page.goTo(fromUrl);
|
||||
|
||||
const expectedUrl = (/^http/.test(toUrl) ? '' : browser.baseUrl.replace(/\/$/, '')) + toUrl;
|
||||
const actualUrl = (await browser.getCurrentUrl()).replace(/\?.*$/, '');
|
||||
const expectedUrl = /^http/.test(toUrl) ? toUrl : prependBaseUrl(toUrl);
|
||||
const actualUrl = await getCurrentUrl();
|
||||
|
||||
expect(actualUrl).toBe(expectedUrl);
|
||||
});
|
||||
|
|
|
@ -43,9 +43,7 @@ export async function loadRemoteSitemapUrls(host: string) {
|
|||
.on('error', reject));
|
||||
});
|
||||
|
||||
// Currently, all sitemaps use `angular.io` as host in URLs (which is fine since we only use the
|
||||
// sitemap `angular.io`). See also `aio/src/extra-files/*/robots.txt`.
|
||||
return extractSitemapUrls(xml, 'https://angular.io/');
|
||||
return extractSitemapUrls(xml);
|
||||
}
|
||||
|
||||
export function loadSWRoutes() {
|
||||
|
@ -69,8 +67,22 @@ export function loadSWRoutes() {
|
|||
}
|
||||
|
||||
// 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[] = [];
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue