-
Notifications
You must be signed in to change notification settings - Fork 3
/
wp-http-testcase.php
440 lines (357 loc) · 8.11 KB
/
wp-http-testcase.php
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* A test case parent for testing HTTP requests.
*
* @package WP_HTTP_Testcase
* @since 1.0.0
*/
/**
* Parent test case for tests involving HTTP requests.
*
* @since 1.0.0
*/
abstract class WP_HTTP_TestCase extends WP_UnitTestCase {
/**
* The HTTP requests caught.
*
* Each of the requests has the following keys:
* {
* @type string $url The URL for the request.
* @type array $request The request arguments.
* }
*
* @since 1.0.0
*
* @var array $http_requests
*/
protected $http_requests;
/**
* A function to simulate responses to requests.
*
* @since 1.0.0
*
* @type callable|false $http_responder
*/
protected $http_responder;
/**
* Whether the class has been initialized.
*
* @since 1.3.0
*
* @var bool
*/
protected static $did_init = false;
/**
* The local host to route requests to in 'local' mode.
*
* @since 1.1.0
*
* @var string
*/
protected static $host;
/**
* Whether to use caching.
*
* @since 1.1.0
*
* @var bool
*/
protected static $use_caching = true;
/**
* The request fields to use when generating the cache key.
*
* Only the keys are used. The values are meaningless and are completely ignored.
*
* @since 1.3.0
*
* @var array
*/
protected static $cache_request_fields = array(
'method' => 1,
'headers' => 1,
'cookies' => 1,
'body' => 1,
);
/**
* The directory the cache files are in.
*
* @since 1.1.0
*
* @var string
*/
protected static $cache_dir;
/**
* The cache group to use.
*
* @since 1.1.0
*
* @var string
*/
protected static $cache_group = 'default';
/**
* The currently loaded cache.
*
* @since 1.1.0
*
* @var array
*/
protected static $cache;
/**
* Whether the cache has changed.
*
* @since 1.1.0
*
* @var bool
*/
protected static $cache_changed;
/**
* Whether to skip just the next cache hit and put the request through.
*
* When true, the cache won't be checked for the next request, but the response
* will still overwrite the existing cache.
*
* @since 1.2.0
*
* @var bool
*/
protected $skip_cache_next = false;
/**
* @since 1.3.0
*/
public static function setUpBeforeClass() {
if ( ! self::$did_init ) {
self::init();
}
parent::setUpBeforeClass();
}
/**
* @since 1.3.1
*/
public static function tearDownAfterClass() {
self::save_cache();
parent::tearDownAfterClass();
}
/**
* Set up for each test.
*
* @since 1.0.0
*/
public function setUp() {
parent::setUp();
$this->http_requests = array();
if ( ! empty( self::$host ) ) {
$this->http_responder = array( $this, 'route_request' );
}
add_filter( 'pre_http_request', array( $this, 'http_request_listner' ), 10, 3 );
}
/**
* Clean up the filters after each test.
*
* @since 1.0.0
*/
public function tearDown() {
parent::tearDown();
remove_filter( 'pre_http_request', array( $this, 'http_request_listner' ) );
$this->skip_cache_next = false;
}
//
// Helpers.
//
/**
* Mock responses to HTTP requests coming from WordPress.
*
* @since 1.0.0
*
* @WordPress\filter pre_http_request Added by self::setUp().
*
* @param mixed $preempt Response to the request, or false to not preempt it.
* @param array $request The request arguments.
* @param string $url The URL the request is being made to.
*
* @return mixed A response, or false.
*/
public function http_request_listner( $preempt, $request, $url ) {
$this->http_requests[] = array(
'url' => $url,
'request' => $request,
);
if ( $this->http_responder ) {
$preempt = call_user_func( $this->http_responder, $request, $url );
}
return $preempt;
}
/**
* Route a request through to a predefined host, with optional caching.
*
* @since 1.1.0
*
* @param array $request The request to route.
* @param string $url The URL the request is for.
*
* @return array|bool|false|WP_Error The response.
*/
protected function route_request( $request, $url ) {
// Check the cache.
$cache_key = $this->get_cache_key( $request, $url );
$cached = $this->get_cached_response( $cache_key );
if ( $cached ) {
return $cached;
}
// Get the URL host.
$host = parse_url( $url, PHP_URL_HOST );
// If the host is already correct, return false so the request continues.
if ( $host === self::$host ) {
return false;
}
$url = str_replace( $host, self::$host, $url );
$response = wp_remote_request( $url, $request );
$this->cache_response( $cache_key, $response );
return $response;
}
/**
* Get the cache key for a request.
*
* @since 1.1.0
*
* @param array $request The request.
* @param string $url The URL the request is for.
*
* @return string|false The cache key for the request. False if not caching.
*/
protected function get_cache_key( $request, $url ) {
if ( ! self::$use_caching ) {
return false;
}
$request = array_intersect_key( $request, self::$cache_request_fields );
return md5( serialize( $request ) . $url );
}
/**
* Get the cached response to a request.
*
* @since 1.1.0
*
* @param string $cache_key The cache key for the request.
*
* @return array|false The cached response, or false if none.
*/
protected function get_cached_response( $cache_key ) {
if ( ! self::$use_caching ) {
return false;
}
// If we're to skip the cache this time, return false.
if ( $this->skip_cache_next ) {
$this->skip_cache_next = false;
return false;
}
if ( ! isset( self::$cache[ $cache_key ] ) ) {
return false;
}
return self::$cache[ $cache_key ];
}
/**
* Save a response to the cache.
*
* @since 1.1.0
*
* @param string $cache_key The cache key for the request.
* @param array $response The response.
*/
protected function cache_response( $cache_key, $response ) {
if ( ! self::$use_caching ) {
return;
}
self::$cache[ $cache_key ] = $response;
self::$cache_changed = true;
}
//
// Static Functions.
//
/**
* Initialize the class.
*
* @since 1.1.0
*/
public static function init() {
self::load_env( 'HOST' );
self::load_env( 'USE_CACHING', true );
self::load_cache();
self::$did_init = true;
}
/**
* Get an environment setting.
*
* @since 1.1.0
*
* @param string $var The name of the setting to get.
* @param mixed $default The default value for this setting.
*
* @return mixed|null|string
*/
protected static function get_env( $var, $default = null ) {
$value = getenv( 'WP_HTTP_TC_' . $var );
if ( false !== $value ) {
return $value;
}
if ( ! defined( 'WP_HTTP_TC_' . $var ) ) {
return $default;
}
return constant( 'WP_HTTP_TC_' . $var );
}
/**
* Get an environment setting and assign it to the corresponding property.
*
* @since 1.2.0
*
* @param string $var The var name.
* @param bool $is_bool Whether this is a boolean property.
*/
protected static function load_env( $var, $is_bool = false ) {
$property = strtolower( $var );
self::${$property} = self::get_env( $var, self::${$property} );
if ( $is_bool ) {
self::${$property} = (bool) self::${$property};
}
}
/**
* Load the cache if caching is in use.
*
* @since 1.1.0
*/
protected static function load_cache() {
if ( ! self::$use_caching ) {
return;
}
$request_fields = self::get_env( 'CACHE_REQUEST_FIELDS' );
if ( null !== $request_fields ) {
self::$cache_request_fields = array_flip(
array_map( 'trim', explode( ',', $request_fields ) )
);
}
self::load_env( 'CACHE_GROUP' );
self::$cache_dir = self::get_env( 'CACHE_DIR', dirname( __FILE__ ) );
$cache_file = self::$cache_dir . '/' . self::$cache_group;
if ( ! file_exists( $cache_file ) ) {
return;
}
$cache = file_get_contents( $cache_file );
self::$cache = unserialize( $cache );
}
/**
* Save the cache.
*
* @since 1.1.0
*/
public static function save_cache() {
if ( ! self::$cache_changed ) {
return;
}
file_put_contents(
self::$cache_dir . '/' . self::$cache_group
, serialize( self::$cache )
);
}
}
if ( ! defined( 'WP_HTTP_TC_NO_BACKPAT' ) ) {
abstract class WP_HTTP_UnitTestCase extends WP_HTTP_TestCase {}
}
// EOF