Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: same domain check with www for baseUrl #375

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/canonical/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import { JSDOM } from 'jsdom';
import { composeBaseURL } from '@adobe/spacecat-shared-utils';
import { fetch } from '../support/utils.js';
import { AuditBuilder } from '../common/audit-builder.js';
import { noopUrlResolver } from '../common/audit.js';
Expand Down Expand Up @@ -352,7 +353,7 @@ export function validateCanonicalFormat(canonicalUrl, baseUrl, log) {
}

// Check if the canonical URL has the same domain as the base URL
if (url.hostname !== base.hostname) {
if (composeBaseURL(url.hostname) !== composeBaseURL(base.hostname)) {
checks.push({
check: CANONICAL_CHECKS.CANONICAL_URL_SAME_DOMAIN.check,
success: false,
Expand Down
16 changes: 16 additions & 0 deletions test/audits/canonical.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ describe('Canonical URL Tests', () => {
expect(log.info).to.have.been.calledWith('Canonical URL https://example.com uses a different protocol than base URL http://example.com');
});

it('should pass when canonical URL and base URL are identical, regardless of the www prefix', () => {
const cases = [
{ canonicalUrl: 'https://www.example.com', baseUrl: 'https://example.com' },
{ canonicalUrl: 'https://example.com', baseUrl: 'https://www.example.com' },
];

cases.forEach(({ canonicalUrl, baseUrl }) => {
const result = validateCanonicalFormat(canonicalUrl, baseUrl, log);

expect(result).to.deep.include({
check: 'canonical-url-same-domain',
success: true,
});
});
});

it('should fail if the canonical URL is not absolute', () => {
const canonicalUrl = '/relative/url';
const baseUrl = 'http://example.com';
Expand Down
Loading