Skip to content

Commit 4adf764

Browse files
Copilotkushh23
andauthored
Fix test_replacement_triple_slash_url: move scheme normalization before can_replace_url() and test build_url() directly
Agent-Logs-Url: https://github.com/Codeinwp/optimole-wp/sessions/8939ca95-d282-4d7f-96c9-a37fb5fc50ec Co-authored-by: kushh23 <110405452+kushh23@users.noreply.github.com>
1 parent 0cf464e commit 4adf764

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

inc/url_replacer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public function build_url(
131131
return $original_url;
132132
}
133133

134+
if ( substr( $url, 0, 2 ) === '//' ) {
135+
$url = ltrim( $url, '/' );
136+
$url = sprintf( '%s://%s', is_ssl() ? 'https' : 'http', $url );
137+
}
138+
134139
if ( ! $this->can_replace_url( $url ) ) {
135140
return $original_url;
136141
}

tests/test-replacer.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -451,18 +451,25 @@ public function test_replacement_without_scheme() {
451451
}
452452

453453
/**
454-
* Regression test: triple-slash URLs (///host/path) must be normalized to
455-
* http(s)://host/path before CDN embedding — never produce https:///host/path.
454+
* Regression test: build_url() must normalize protocol-relative and
455+
* triple-slash URLs to http(s)://host/path — never produce https:///host/path.
456456
*/
457-
public function test_replacement_triple_slash_url() {
458-
$content = '<div class="codeinwp-container">
459-
<img src="///www.example.org/wp-content/uploads/2018/05/brands.png">
460-
</div>';
461-
$replaced_content = Optml_Manager::instance()->replace_content( $content );
462-
463-
$this->assertStringContainsString( 'i.optimole.com', $replaced_content );
464-
$this->assertStringNotContainsString( ':///', $replaced_content );
465-
$this->assertStringContainsString( 'http://www.example.org', $replaced_content );
457+
public function test_build_url_normalizes_triple_slash() {
458+
// Protocol-relative URL (//host/path) should be normalized correctly.
459+
$url = '//www.example.org/wp-content/uploads/2018/05/brands.png';
460+
$result = Optml_Url_Replacer::instance()->build_url( $url );
461+
462+
$this->assertStringContainsString( 'i.optimole.com', $result );
463+
$this->assertStringNotContainsString( ':///', $result );
464+
$this->assertStringContainsString( 'http://www.example.org', $result );
465+
466+
// Triple-slash URL (///host/path) should also be normalized correctly.
467+
$url = '///www.example.org/wp-content/uploads/2018/05/brands.png';
468+
$result = Optml_Url_Replacer::instance()->build_url( $url );
469+
470+
$this->assertStringContainsString( 'i.optimole.com', $result );
471+
$this->assertStringNotContainsString( ':///', $result );
472+
$this->assertStringContainsString( 'http://www.example.org', $result );
466473
}
467474

468475
public function test_non_allowed_extensions() {

0 commit comments

Comments
 (0)