From 970b7c43ab269d289bcbec90bb12264ea787295c Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Wed, 29 Jun 2016 12:46:59 +0000 Subject: [PATCH] HTTP: Add unit tests for `wp_get_http_headers()` and `wp_remote_retrieve_headers()`. Props borgesbruno, jipmoors. Fixes #37090. git-svn-id: https://develop.svn.wordpress.org/trunk@37907 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/http/getHttpHeaders.php | 69 +++++++++++++++++++ .../tests/http/remoteRetrieveHeaders.php | 38 ++++++++++ 2 files changed, 107 insertions(+) create mode 100644 tests/phpunit/tests/http/getHttpHeaders.php create mode 100644 tests/phpunit/tests/http/remoteRetrieveHeaders.php diff --git a/tests/phpunit/tests/http/getHttpHeaders.php b/tests/phpunit/tests/http/getHttpHeaders.php new file mode 100644 index 0000000000..ccb6b4f545 --- /dev/null +++ b/tests/phpunit/tests/http/getHttpHeaders.php @@ -0,0 +1,69 @@ +assertTrue( $result ); + } + + /** + * Test with an invalid URL + */ + public function test_wp_get_http_headers_invalid_url() { + $result = wp_get_http_headers( 'not_an_url' ); + $this->assertFalse( $result ); + } + + /** + * Test to see if the deprecated argument is working + */ + public function test_wp_get_http_headers_deprecated_argument() { + $this->setExpectedDeprecated( 'wp_get_http_headers' ); + + wp_get_http_headers( 'does_not_matter', $deprecated = true ); + } + + /** + * Mock the HTTP request response + * + * @param bool $false False. + * @param array $arguments Request arguments. + * @param string $url Request URL. + * + * @return array|bool + */ + public function fake_http_request( $false, $arguments, $url ) { + if ( 'http://example.com' === $url ) { + return array( 'headers' => true ); + } + + return false; + } +} diff --git a/tests/phpunit/tests/http/remoteRetrieveHeaders.php b/tests/phpunit/tests/http/remoteRetrieveHeaders.php new file mode 100644 index 0000000000..f7785803c4 --- /dev/null +++ b/tests/phpunit/tests/http/remoteRetrieveHeaders.php @@ -0,0 +1,38 @@ + $headers ); + + $result = wp_remote_retrieve_headers( $response ); + $this->assertEquals( $headers, $result ); + } + + /** + * Response is a WP_Error + */ + function test_remote_retrieve_headers_is_error() { + $response = new WP_Error( 'Some error' ); + + $result = wp_remote_retrieve_headers( $response ); + $this->assertEquals( array(), $result ); + } + + /** + * Response does not contain 'headers' + */ + function test_remote_retrieve_headers_invalid_response() { + $response = array( 'no_headers' => 'set'); + + $result = wp_remote_retrieve_headers( $response ); + $this->assertEquals( array(), $result ); + } +}