diff --git a/aio/tests/deployment/e2e/redirection.e2e-spec.ts b/aio/tests/deployment/e2e/redirection.e2e-spec.ts index 6dccb3f804..032c44f572 100644 --- a/aio/tests/deployment/e2e/redirection.e2e-spec.ts +++ b/aio/tests/deployment/e2e/redirection.e2e-spec.ts @@ -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); }); diff --git a/aio/tests/deployment/shared/helpers.ts b/aio/tests/deployment/shared/helpers.ts index d50a6ae375..3a7f321a61 100644 --- a/aio/tests/deployment/shared/helpers.ts +++ b/aio/tests/deployment/shared/helpers.ts @@ -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>/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; }