Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df0c98b

Browse files
committedFeb 20, 2025·
Tests: Add a test to ensure that all response codes error except 200 and 404.
1 parent 4bffe39 commit df0c98b

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed
 

‎tests/phpunit/tests/API_Rewrite/APIRewrite_PreHttpRequestTest.php

+76-2
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,65 @@ static function ( $response, $parsed_args ) use ( &$actual ) {
391391
}
392392

393393
/**
394-
* Test that a WP_Error object is returned for non-200 HTTP responses.
394+
* Test that a WP_Error object is not returned for some response codes.
395+
*
396+
* @dataProvider data_response_codes_that_should_not_error
397+
*
398+
* @param int $response_code The response code.
395399
*/
396-
public function test_should_return_wp_error_for_non_200_responses() {
400+
public function test_should_not_return_wp_error_for_some_response_codes( $response_code ) {
401+
add_filter(
402+
'pre_http_request',
403+
static function () use ( $response_code ) {
404+
return [
405+
'response' => [
406+
'code' => $response_code,
407+
'message' => 'Test response code',
408+
],
409+
];
410+
},
411+
10,
412+
2
413+
);
414+
415+
$api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', true, '' );
416+
$actual = $api_rewrite->pre_http_request(
417+
[],
418+
[],
419+
$this->get_default_host() . '/file.php'
420+
);
421+
422+
$this->assertNotInstanceOf(
423+
'WP_Error',
424+
$actual,
425+
'A WP_Error object was returned.'
426+
);
427+
}
428+
429+
/**
430+
* Data provider.
431+
*
432+
* @return array[]
433+
*/
434+
public function data_response_codes_that_should_not_error() {
435+
return [
436+
'200' => [
437+
'response_code' => 200,
438+
],
439+
'404' => [
440+
'response_code' => 404,
441+
],
442+
];
443+
}
444+
445+
/**
446+
* Test that a WP_Error object is returned for other HTTP response codes.
447+
*
448+
* @dataProvider data_response_codes_that_should_error
449+
*
450+
* @param int $response_code The response code.
451+
*/
452+
public function test_should_return_wp_error_for_other_response_codes( $response_code ) {
397453
add_filter(
398454
'pre_http_request',
399455
static function () {
@@ -428,6 +484,24 @@ static function () {
428484
);
429485
}
430486

487+
/**
488+
* Data provider.
489+
*
490+
* @return array[]
491+
*/
492+
public function data_response_codes_that_should_error() {
493+
$datasets = [];
494+
495+
$exceptions = [ 200, 404 ];
496+
for ( $i = 0; $i < 599; ++$i ) {
497+
if ( ! in_array( $i, $exceptions, true ) ) {
498+
$datasets[ $i ] = [ 'response_code' => $i ];
499+
}
500+
}
501+
502+
return $datasets;
503+
}
504+
431505
/**
432506
* Gets the default host.
433507
*

0 commit comments

Comments
 (0)
Please sign in to comment.