Skip to content

Commit 299e3ae

Browse files
Copilotkushh23
andauthored
Add regression tests for add_schema() SSL operator precedence and triple-slash URL normalization
Agent-Logs-Url: https://github.com/Codeinwp/optimole-wp/sessions/0b947f08-2140-4d1b-9330-9381b891257a Co-authored-by: kushh23 <110405452+kushh23@users.noreply.github.com>
1 parent e50c815 commit 299e3ae

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

tests/test-generic.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,34 @@ function test_get_unoptimized_url_edge_cases() {
169169
$this->assertEquals( $uploaded_url, $result );
170170
}
171171

172+
/**
173+
* Test add_schema() prepends http: for protocol-relative URLs on non-SSL.
174+
*/
175+
function test_add_schema_non_ssl() {
176+
$this->assertEquals( 'http://example.org/image.jpg', $this->add_schema( '//example.org/image.jpg' ) );
177+
}
178+
179+
/**
180+
* Test add_schema() prepends https: for protocol-relative URLs on SSL.
181+
* Regression: operator-precedence bug caused https: to be returned bare instead of https://example.org/...
182+
*/
183+
function test_add_schema_ssl() {
184+
$_SERVER['HTTPS'] = 'on';
185+
$result = $this->add_schema( '//example.org/image.jpg' );
186+
unset( $_SERVER['HTTPS'] );
187+
188+
$this->assertEquals( 'https://example.org/image.jpg', $result );
189+
$this->assertStringNotEquals( 'https:', $result );
190+
}
191+
192+
/**
193+
* Test add_schema() leaves already-schemed URLs unchanged.
194+
*/
195+
function test_add_schema_already_schemed() {
196+
$this->assertEquals( 'http://example.org/image.jpg', $this->add_schema( 'http://example.org/image.jpg' ) );
197+
$this->assertEquals( 'https://example.org/image.jpg', $this->add_schema( 'https://example.org/image.jpg' ) );
198+
}
199+
172200
/**
173201
* Test get_unoptimized_url with custom domain configuration.
174202
*/

tests/test-replacer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,21 @@ public function test_replacement_without_scheme() {
450450
$this->assertStringContainsString( 'http://www.example.org', $replaced_content );
451451
}
452452

453+
/**
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.
456+
*/
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 );
466+
}
467+
453468
public function test_non_allowed_extensions() {
454469
$replaced_content = Optml_Manager::instance()->replace_content( ( self::CSS_STYLE . self::IMG_TAGS . self::WRONG_EXTENSION ) );
455470
$this->assertStringContainsString( 'i.optimole.com', $replaced_content );

0 commit comments

Comments
 (0)