From 2ddab3adf8fbec91b177ac7411163057fd4e0758 Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Thu, 3 Dec 2015 16:34:00 +0000 Subject: [PATCH] Route HEAD API requests through the GET callback method fixes #34837 props danielbachhuber git-svn-id: https://develop.svn.wordpress.org/trunk@35758 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rest-api/class-wp-rest-server.php | 3 ++- tests/phpunit/tests/rest-api/rest-server.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index 62f7343069..2c46fd8d58 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -780,7 +780,8 @@ class WP_REST_Server { $callback = $handler['callback']; $response = null; - if ( empty( $handler['methods'][ $method ] ) ) { + $checked_method = 'HEAD' === $method ? 'GET' : $method; + if ( empty( $handler['methods'][ $checked_method ] ) ) { continue; } diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 248ba56740..c9431f0954 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -121,6 +121,16 @@ class Tests_REST_Server extends WP_Test_REST_TestCase { $this->assertEquals( array( 'foo' => 'bar' ), $request->get_params() ); } + public function test_head_request_handled_by_get() { + register_rest_route( 'head-request', '/test', array( + 'methods' => array( 'GET' ), + 'callback' => '__return_true', + ) ); + $request = new WP_REST_Request( 'HEAD', '/head-request/test' ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + } + /** * Pass a capability which the user does not have, this should * result in a 403 error.