forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.php
More file actions
152 lines (123 loc) · 6.33 KB
/
http.php
File metadata and controls
152 lines (123 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Non-transport-specific WP_HTTP Tests
*
* @group http
*/
class Tests_HTTP_HTTP extends WP_UnitTestCase {
/**
* @dataProvider make_absolute_url_testcases
*/
function test_make_absolute_url( $relative_url, $absolute_url, $expected ) {
if ( ! is_callable( array( 'WP_Http', 'make_absolute_url' ) ) ) {
$this->markTestSkipped( "This version of WP_HTTP doesn't support WP_HTTP::make_absolute_url()" );
return;
}
$actual = WP_Http::make_absolute_url( $relative_url, $absolute_url );
$this->assertEquals( $expected, $actual );
}
function make_absolute_url_testcases() {
// 0: The Location header, 1: The current url, 3: The expected url
return array(
array( 'http://site.com/', 'http://example.com/', 'http://site.com/' ), // Absolute URL provided
array( '/location', '', '/location' ), // No current url provided
array( '', 'http://example.com', 'http://example.com/' ), // No location provided
// Location provided relative to site root
array( '/root-relative-link.ext', 'http://example.com/', 'http://example.com/root-relative-link.ext' ),
array( '/root-relative-link.ext?with=query', 'http://example.com/index.ext?query', 'http://example.com/root-relative-link.ext?with=query' ),
// Location provided relative to current file/directory
array( 'relative-file.ext', 'http://example.com/', 'http://example.com/relative-file.ext' ),
array( 'relative-file.ext', 'http://example.com/filename', 'http://example.com/relative-file.ext' ),
array( 'relative-file.ext', 'http://example.com/directory/', 'http://example.com/directory/relative-file.ext' ),
// Location provided relative to current file/directory but in a parent directory
array( '../file-in-parent.ext', 'http://example.com', 'http://example.com/file-in-parent.ext' ),
array( '../file-in-parent.ext', 'http://example.com/filename', 'http://example.com/file-in-parent.ext' ),
array( '../file-in-parent.ext', 'http://example.com/directory/', 'http://example.com/file-in-parent.ext' ),
array( '../file-in-parent.ext', 'http://example.com/directory/filename', 'http://example.com/file-in-parent.ext' ),
// Location provided in muliple levels higher, including impossible to reach (../ below DOCROOT)
array( '../../file-in-grand-parent.ext', 'http://example.com', 'http://example.com/file-in-grand-parent.ext' ),
array( '../../file-in-grand-parent.ext', 'http://example.com/filename', 'http://example.com/file-in-grand-parent.ext' ),
array( '../../file-in-grand-parent.ext', 'http://example.com/directory/', 'http://example.com/file-in-grand-parent.ext' ),
array( '../../file-in-grand-parent.ext', 'http://example.com/directory/filename/', 'http://example.com/file-in-grand-parent.ext' ),
array( '../../file-in-grand-parent.ext', 'http://example.com/directory1/directory2/filename', 'http://example.com/file-in-grand-parent.ext' ),
// Query strings should attach, or replace existing query string.
array( '?query=string', 'http://example.com', 'http://example.com/?query=string' ),
array( '?query=string', 'http://example.com/file.ext', 'http://example.com/file.ext?query=string' ),
array( '?query=string', 'http://example.com/file.ext?existing=query-string', 'http://example.com/file.ext?query=string' ),
array( 'otherfile.ext?query=string', 'http://example.com/file.ext?existing=query-string', 'http://example.com/otherfile.ext?query=string' ),
// A file with a leading dot
array( '.ext', 'http://example.com/', 'http://example.com/.ext' ),
// URls within URLs
array( '/expected', 'http://example.com/sub/http://site.com/sub/', 'http://example.com/expected' ),
array( '/expected/http://site.com/sub/', 'http://example.com/', 'http://example.com/expected/http://site.com/sub/' ),
// Schemeless URL's (Not valid in HTTP Headers, but may be used elsewhere)
array( '//example.com/sub/', 'https://example.net', 'https://example.com/sub/' ),
);
}
/**
* @dataProvider parse_url_testcases
*/
function test_wp_parse_url( $url, $expected ) {
$actual = wp_parse_url( $url );
$this->assertEquals( $expected, $actual );
}
function parse_url_testcases() {
// 0: The URL, 1: The expected resulting structure
return array(
array( 'http://example.com/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ),
// < PHP 5.4.7: Schemeless URL
array( '//example.com/path/', array( 'host' => 'example.com', 'path' => '/path/' ) ),
array( '//example.com/', array( 'host' => 'example.com', 'path' => '/' ) ),
array( 'http://example.com//path/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '//path/' ) ),
// < PHP 5.4.7: Scheme seperator in the URL
array( 'http://example.com/http://example.net/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/http://example.net/' ) ),
array( '/path/http://example.net/', array( 'path' => '/path/http://example.net/' ) ),
// < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly.
array( '//[::FFFF::127.0.0.1]/', array( 'host' => '[::FFFF::127.0.0.1]', 'path' => '/' ) ),
// PHP's parse_url() calls this an invalid url, we handle it as a path
array( '/://example.com/', array( 'path' => '/://example.com/' ) ),
);
/*
Untestable edge cases in various PHP:
- ///example.com - Fails in PHP >= 5.4.7, assumed path in <5.4.7
- ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7
*/
}
/**
* @ticket 35426
*/
public function test_http_response_code_constants() {
global $wp_header_to_desc;
$ref = new ReflectionClass( 'WP_Http' );
$constants = $ref->getConstants();
// This primes the `$wp_header_to_desc` global:
get_status_header_desc( 200 );
$this->assertEquals( array_keys( $wp_header_to_desc ), array_values( $constants ) );
}
/**
* @ticket 37768
*/
public function test_normalize_cookies_scalar_values() {
$http = _wp_http_get_object();
$cookies = array(
'x' => 'foo',
'y' => 2,
'z' => 0.45,
'foo' => array( 'bar' ),
);
$cookie_jar = $http->normalize_cookies( array(
'x' => 'foo',
'y' => 2,
'z' => 0.45,
'foo' => array( 'bar' ),
) );
$this->assertInstanceOf( 'Requests_Cookie_Jar', $cookie_jar );
foreach( array_keys( $cookies ) as $cookie ) {
if ( 'foo' === $cookie ) {
$this->assertFalse( isset( $cookie_jar[ $cookie ] ) );
} else {
$this->assertInstanceOf( 'Requests_Cookie', $cookie_jar[ $cookie ] );
}
}
}
}