From 2c8329a2b81d1d6b9a828ab123df91d24a83490e Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sat, 4 May 2024 18:01:02 +0100 Subject: [PATCH 1/2] Standardise the assertions for remote HTTP request tests. --- tests/phpunit/tests/http/base.php | 4 ++++ tests/phpunit/tests/http/functions.php | 21 +++++++++++++-------- tests/phpunit/tests/readme.php | 1 + 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 8a6ce3b3b001a..2e73b0de94431 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -219,6 +219,7 @@ public function test_no_redirection_on_PUT() { ); $this->skipTestOnTimeout( $res ); + $this->assertNotWPError( $res ); $this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) ); $this->assertNotEmpty( $res['headers']['location'] ); } @@ -356,6 +357,7 @@ public function test_post_redirect_to_method_300( $response_code, $method ) { $res = wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) ); $this->skipTestOnTimeout( $res ); + $this->assertNotWPError( $res ); $this->assertSame( $method, wp_remote_retrieve_body( $res ) ); } @@ -406,6 +408,7 @@ public function test_ip_url_with_host_header() { $res = wp_remote_get( $url, $args ); $this->skipTestOnTimeout( $res ); + $this->assertNotWPError( $res ); $this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) ); } @@ -447,6 +450,7 @@ public function test_cookie_handling() { $res = wp_remote_get( $url ); $this->skipTestOnTimeout( $res ); + $this->assertNotWPError( $res ); $this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) ); } diff --git a/tests/phpunit/tests/http/functions.php b/tests/phpunit/tests/http/functions.php index 1da190997db06..cacec5e8789b1 100644 --- a/tests/phpunit/tests/http/functions.php +++ b/tests/phpunit/tests/http/functions.php @@ -15,14 +15,14 @@ public function test_head_request() { $response = wp_remote_head( $url ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $headers = wp_remote_retrieve_headers( $response ); $this->assertIsArray( $response ); - + $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) ); $this->assertSame( 'image/png', $headers['Content-Type'] ); $this->assertSame( '153204', $headers['Content-Length'] ); - $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) ); } /** @@ -34,6 +34,7 @@ public function test_head_redirect() { $response = wp_remote_head( $url ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $this->assertSame( 301, wp_remote_retrieve_response_code( $response ) ); } @@ -45,6 +46,7 @@ public function test_head_404() { $response = wp_remote_head( $url ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $this->assertSame( 404, wp_remote_retrieve_response_code( $response ) ); } @@ -59,15 +61,14 @@ public function test_get_request() { $response = wp_remote_get( $url ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $headers = wp_remote_retrieve_headers( $response ); - $this->assertIsArray( $response ); - // Should return the same headers as a HEAD request. - $this->assertSame( 'image/png', $headers['Content-Type'] ); - $this->assertSame( '153204', $headers['Content-Length'] ); $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) ); + $this->assertSame( 'image/jpeg', $headers['Content-Type'] ); + $this->assertSame( '153204', $headers['Content-Length'] ); } /** @@ -82,13 +83,14 @@ public function test_get_redirect() { $response = wp_remote_get( $url ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $headers = wp_remote_retrieve_headers( $response ); // Should return the same headers as a HEAD request. - $this->assertSame( 'image/png', $headers['Content-Type'] ); - $this->assertSame( '153204', $headers['Content-Length'] ); $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) ); + $this->assertSame( 'image/jpeg', $headers['Content-Type'] ); + $this->assertSame( '153204', $headers['Content-Length'] ); } /** @@ -119,6 +121,7 @@ public function test_get_response_cookies() { $response = wp_remote_head( $url ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $cookies = wp_remote_retrieve_cookies( $response ); @@ -164,6 +167,7 @@ public function test_get_response_cookies_with_wp_http_cookie_object() { ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $cookies = wp_remote_retrieve_cookies( $response ); @@ -195,6 +199,7 @@ public function test_get_response_cookies_with_name_value_array() { ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $cookies = wp_remote_retrieve_cookies( $response ); diff --git a/tests/phpunit/tests/readme.php b/tests/phpunit/tests/readme.php index 7ec762b665345..04faecbb368bf 100644 --- a/tests/phpunit/tests/readme.php +++ b/tests/phpunit/tests/readme.php @@ -94,6 +94,7 @@ public function get_response_body( $url ) { $response = wp_remote_get( $url ); $this->skipTestOnTimeout( $response ); + $this->assertNotWPError( $response ); $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); From 6e28790e88287f891b5f9c1d15a5e435a2cd756a Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sat, 4 May 2024 18:17:56 +0100 Subject: [PATCH 2/2] Fix a copy paste. --- tests/phpunit/tests/http/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/http/functions.php b/tests/phpunit/tests/http/functions.php index cacec5e8789b1..9c3e54a8b8911 100644 --- a/tests/phpunit/tests/http/functions.php +++ b/tests/phpunit/tests/http/functions.php @@ -67,7 +67,7 @@ public function test_get_request() { // Should return the same headers as a HEAD request. $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) ); - $this->assertSame( 'image/jpeg', $headers['Content-Type'] ); + $this->assertSame( 'image/png', $headers['Content-Type'] ); $this->assertSame( '153204', $headers['Content-Length'] ); } @@ -89,7 +89,7 @@ public function test_get_redirect() { // Should return the same headers as a HEAD request. $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) ); - $this->assertSame( 'image/jpeg', $headers['Content-Type'] ); + $this->assertSame( 'image/png', $headers['Content-Type'] ); $this->assertSame( '153204', $headers['Content-Length'] ); }