Skip to content

Commit

Permalink
Move the check for HEAD requests to ::prepare_item_for_response() met…
Browse files Browse the repository at this point in the history
…hod to preserve BC.
  • Loading branch information
anton-vlasenko committed Dec 19, 2024
1 parent 9bfca1b commit 8fd47ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,6 @@ public function get_item( $request ) {
return $term;
}

if ( $request->is_method( 'HEAD' ) ) {
return new WP_REST_Response();
}

$response = $this->prepare_item_for_response( $term, $request );

return rest_ensure_response( $response );
Expand Down Expand Up @@ -900,6 +896,12 @@ public function prepare_item_for_database( $request ) {
*/
public function prepare_item_for_response( $item, $request ) {

// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
return apply_filters( "rest_prepare_{$this->taxonomy}", new WP_REST_Response(), $item, $request );
}

$fields = $this->get_fields_for_response( $request );
$data = array();

Expand Down
25 changes: 21 additions & 4 deletions tests/phpunit/tests/rest-api/rest-categories-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1325,24 +1325,41 @@ public static function data_readable_http_methods() {
}

/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_with_head_request_should_not_prepare_category_data() {
public function test_get_item_should_allow_adding_headers_via_filter( $method ) {
$category_id = self::factory()->category->create();

$request = new WP_REST_Request( 'HEAD', sprintf( '/wp/v2/categories/%d', $category_id ) );
$request = new WP_REST_Request( $method, sprintf( '/wp/v2/categories/%d', $category_id ) );

$hook_name = 'rest_prepare_category';

$filter = new MockAction();
$callback = array( $filter, 'filter' );
add_filter( $hook_name, $callback );
$header_filter = new class() {
public static function add_custom_header( $response ) {
$response->header( 'X-Test-Header', 'Test' );

return $response;
}
};
add_filter( $hook_name, array( $header_filter, 'add_custom_header' ) );
$response = rest_get_server()->dispatch( $request );
remove_filter( $hook_name, $callback );
remove_filter( $hook_name, array( $header_filter, 'add_custom_header' ) );

$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );

$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertSame( 1, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$headers = $response->get_headers();
$this->assertArrayHasKey( 'X-Test-Header', $headers, 'The "X-Test-Header" header should be present in the response.' );
$this->assertSame( 'Test', $headers['X-Test-Header'], 'The "X-Test-Header" header value should be equal to "Test".' );
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
}

0 comments on commit 8fd47ef

Please sign in to comment.