forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
227 lines (169 loc) · 6.57 KB
/
functions.php
File metadata and controls
227 lines (169 loc) · 6.57 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* @group http
* @group external-http
*/
class Tests_HTTP_Functions extends WP_UnitTestCase {
/**
* Mark test as skipped if the HTTP request times out.
*/
function skipTestOnTimeout( $response ) {
if ( ! is_wp_error( $response ) ) {
return;
}
if ( 'connect() timed out!' === $response->get_error_message() ) {
$this->markTestSkipped( 'HTTP timeout' );
}
if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
$this->markTestSkipped( 'HTTP timeout' );
}
if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
$this->markTestSkipped( 'HTTP timeout' );
}
}
public function setUp() {
if ( ! extension_loaded( 'openssl' ) ) {
$this->markTestSkipped( 'Tests_HTTP_Functions requires openssl.' );
}
parent::setUp();
}
function test_head_request() {
// this url give a direct 200 response
$url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
$response = wp_remote_head( $url );
$this->skipTestOnTimeout( $response );
$headers = wp_remote_retrieve_headers( $response );
$this->assertInternalType( 'array', $response );
$this->assertEquals( 'image/jpeg', $headers['content-type'] );
$this->assertEquals( '40148', $headers['content-length'] );
$this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
}
function test_head_redirect() {
// this url will 301 redirect
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
$response = wp_remote_head( $url );
$this->skipTestOnTimeout( $response );
$this->assertEquals( '301', wp_remote_retrieve_response_code( $response ) );
}
function test_head_404() {
$url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg';
$response = wp_remote_head( $url );
$this->skipTestOnTimeout( $response );
$this->assertEquals( '404', wp_remote_retrieve_response_code( $response ) );
}
function test_get_request() {
$url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
$response = wp_remote_get( $url );
$this->skipTestOnTimeout( $response );
$headers = wp_remote_retrieve_headers( $response );
$this->assertInternalType( 'array', $response );
// should return the same headers as a head request
$this->assertEquals( 'image/jpeg', $headers['content-type'] );
$this->assertEquals( '40148', $headers['content-length'] );
$this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
}
function test_get_redirect() {
// this will redirect to asdftestblog1.files.wordpress.com
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
$response = wp_remote_get( $url );
$this->skipTestOnTimeout( $response );
$headers = wp_remote_retrieve_headers( $response );
// should return the same headers as a head request
$this->assertEquals( 'image/jpeg', $headers['content-type'] );
$this->assertEquals( '40148', $headers['content-length'] );
$this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
}
function test_get_redirect_limit_exceeded() {
// this will redirect to asdftestblog1.files.wordpress.com
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
// pretend we've already redirected 5 times
$response = wp_remote_get( $url, array( 'redirection' => -1 ) );
$this->skipTestOnTimeout( $response );
$this->assertWPError( $response );
}
/**
* @ticket 33711
*/
function test_get_response_cookies() {
$url = 'https://login.wordpress.org/wp-login.php';
$response = wp_remote_head( $url );
$this->skipTestOnTimeout( $response );
$cookies = wp_remote_retrieve_cookies( $response );
$this->assertNotEmpty( $cookies );
$cookie = wp_remote_retrieve_cookie( $response, 'wordpress_test_cookie' );
$this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
$this->assertSame( 'wordpress_test_cookie', $cookie->name );
$this->assertSame( 'WP Cookie check', $cookie->value );
$value = wp_remote_retrieve_cookie_value( $response, 'wordpress_test_cookie' );
$this->assertSame( 'WP Cookie check', $value );
$no_value = wp_remote_retrieve_cookie_value( $response, 'not_a_cookie' );
$this->assertSame( '', $no_value );
$no_cookie = wp_remote_retrieve_cookie( $response, 'not_a_cookie' );
$this->assertSame( '', $no_cookie );
}
/**
* @ticket 37437
*/
function test_get_response_cookies_with_wp_http_cookie_object() {
$url = 'http://example.org';
$response = wp_remote_get(
$url,
array(
'cookies' => array(
new WP_Http_Cookie(
array(
'name' => 'test',
'value' => 'foo',
)
),
),
)
);
$this->skipTestOnTimeout( $response );
$cookies = wp_remote_retrieve_cookies( $response );
$this->assertNotEmpty( $cookies );
$cookie = wp_remote_retrieve_cookie( $response, 'test' );
$this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
$this->assertSame( 'test', $cookie->name );
$this->assertSame( 'foo', $cookie->value );
}
/**
* @ticket 37437
*/
function test_get_response_cookies_with_name_value_array() {
$url = 'http://example.org';
$response = wp_remote_get(
$url,
array(
'cookies' => array(
'test' => 'foo',
),
)
);
$this->skipTestOnTimeout( $response );
$cookies = wp_remote_retrieve_cookies( $response );
$this->assertNotEmpty( $cookies );
$cookie = wp_remote_retrieve_cookie( $response, 'test' );
$this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
$this->assertSame( 'test', $cookie->name );
$this->assertSame( 'foo', $cookie->value );
}
/**
* @ticket 43231
*/
function test_get_cookie_host_only() {
// emulate WP_Http::request() internals
$requests_response = new Requests_Response();
$requests_response->cookies['test'] = Requests_Cookie::parse( 'test=foo; domain=.wordpress.org' );
$requests_response->cookies['test']->flags['host-only'] = false; // https://github.com/rmccue/Requests/issues/306
$http_response = new WP_HTTP_Requests_Response( $requests_response );
$response = $http_response->to_array();
// check the host_only flag in the resulting WP_Http_Cookie
$cookie = wp_remote_retrieve_cookie( $response, 'test' );
$this->assertEquals( $cookie->domain, 'wordpress.org' );
$this->assertFalse( $cookie->host_only, 'host-only flag not set' );
// regurgitate (Requests_Cookie -> WP_Http_Cookie -> Requests_Cookie)
$cookies = WP_Http::normalize_cookies( wp_remote_retrieve_cookies( $response ) );
$this->assertFalse( $cookies['test']->flags['host-only'], 'host-only flag data lost' );
}
}