Skip to content

Commit 2b2b059

Browse files
committed
Tests: Add coverage for is_allowed_http_origin().
Cover allowed and disallowed origins, explicit empty input, request-header fallback, and filtering of the allowed origin. Developed in: #14646 Props shailu25. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64810 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5bcfa81 commit 2b2b059

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Tests for is_allowed_http_origin().
5+
*
6+
* @group http
7+
*
8+
* @covers ::is_allowed_http_origin
9+
*/
10+
class Tests_HTTP_IsAllowedHttpOrigin extends WP_UnitTestCase {
11+
12+
public function tear_down() {
13+
unset( $_SERVER['HTTP_ORIGIN'] );
14+
parent::tear_down();
15+
}
16+
17+
/**
18+
* @ticket 65819
19+
*/
20+
public function test_returns_origin_when_allowed() {
21+
$allowed = get_allowed_http_origins();
22+
$origin = $allowed[0];
23+
24+
$this->assertSame( $origin, is_allowed_http_origin( $origin ) );
25+
}
26+
27+
/**
28+
* @ticket 65819
29+
*/
30+
public function test_returns_empty_string_when_not_allowed() {
31+
$this->assertSame( '', is_allowed_http_origin( 'https://evil.example' ) );
32+
}
33+
34+
/**
35+
* @ticket 65819
36+
*/
37+
public function test_returns_empty_string_for_empty_origin() {
38+
$allowed = get_allowed_http_origins();
39+
$_SERVER['HTTP_ORIGIN'] = $allowed[0];
40+
41+
// Explicit empty string must not fall back to the request Origin header.
42+
$this->assertSame( '', is_allowed_http_origin( '' ) );
43+
}
44+
45+
/**
46+
* @ticket 65819
47+
*/
48+
public function test_uses_get_http_origin_when_origin_not_provided() {
49+
$allowed = get_allowed_http_origins();
50+
$_SERVER['HTTP_ORIGIN'] = $allowed[0];
51+
52+
$this->assertSame( $allowed[0], is_allowed_http_origin() );
53+
}
54+
55+
/**
56+
* @ticket 65819
57+
*/
58+
public function test_allowed_http_origin_filter() {
59+
add_filter(
60+
'allowed_http_origin',
61+
static function () {
62+
return 'https://filtered.example';
63+
},
64+
10,
65+
2
66+
);
67+
68+
$this->assertSame( 'https://filtered.example', is_allowed_http_origin( 'https://evil.example' ) );
69+
}
70+
}

0 commit comments

Comments
 (0)