From 5ab7fd1a1ca07413795332a89fcf6bb3995360bb Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 17 Dec 2024 11:15:02 -0800 Subject: [PATCH] Add test case for route ending in newline --- plugins/image-prioritizer/helper.php | 4 +- .../image-prioritizer/tests/test-helper.php | 72 ++++++++++++------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/plugins/image-prioritizer/helper.php b/plugins/image-prioritizer/helper.php index 201f03a96..75c3e18d4 100644 --- a/plugins/image-prioritizer/helper.php +++ b/plugins/image-prioritizer/helper.php @@ -288,8 +288,8 @@ function image_prioritizer_filter_rest_request_before_callbacks( $response, arra if ( $request->get_method() !== 'POST' || - // The strtolower() is due to \WP_REST_Server::match_request_to_handler() using case-insensitive pattern match. - OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE !== trim( strtolower( trim( $request->get_route(), '/' ) ) ) + // The strtolower() and outer trim are due to \WP_REST_Server::match_request_to_handler() using case-insensitive pattern match and using '$' instead of '\z'. + OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE !== rtrim( strtolower( ltrim( $request->get_route(), '/' ) ) ) ) { return $response; } diff --git a/plugins/image-prioritizer/tests/test-helper.php b/plugins/image-prioritizer/tests/test-helper.php index f1a7c0d1e..dfd05b854 100644 --- a/plugins/image-prioritizer/tests/test-helper.php +++ b/plugins/image-prioritizer/tests/test-helper.php @@ -759,17 +759,19 @@ public function data_provider_to_test_image_prioritizer_filter_rest_request_befo return $request; }; + $bad_origin_data = array( + 'url' => 'https://bad-origin.example.com/image.jpg', + 'tag' => 'DIV', + 'id' => null, + 'class' => null, + ); + return array( - 'invalid_external_bg_image' => array( - 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request { + 'invalid_external_bg_image' => array( + 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request, $bad_origin_data ): WP_REST_Request { $url_metric_data = $get_sample_url_metric_data(); - $url_metric_data['lcpElementExternalBackgroundImage'] = array( - 'url' => 'https://bad-origin.example.com/image.jpg', - 'tag' => 'DIV', - 'id' => null, - 'class' => null, - ); + $url_metric_data['lcpElementExternalBackgroundImage'] = $bad_origin_data; $url_metric_data['viewport']['width'] = 10101; $url_metric_data['viewport']['height'] = 20202; return $create_request( $url_metric_data ); @@ -786,7 +788,7 @@ public function data_provider_to_test_image_prioritizer_filter_rest_request_befo }, ), - 'valid_external_bg_image' => array( + 'valid_external_bg_image' => array( 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request { $url_metric_data = $get_sample_url_metric_data(); $image_url = home_url( '/good.jpg' ); @@ -846,17 +848,14 @@ static function ( $pre, $parsed_args, $url ) use ( $image_url ) { }, ), - 'invalid_external_bg_image_variant_route' => array( - 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request { - $url_metric_data = $get_sample_url_metric_data(); - - $url_metric_data['lcpElementExternalBackgroundImage'] = array( - 'url' => 'https://bad-origin.example.com/image.jpg', - 'tag' => 'DIV', - 'id' => null, - 'class' => null, + 'invalid_external_bg_image_uppercase_route' => array( + 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request, $bad_origin_data ): WP_REST_Request { + $request = $create_request( + array_merge( + $get_sample_url_metric_data(), + array( 'lcpElementExternalBackgroundImage' => $bad_origin_data ) + ) ); - $request = $create_request( $url_metric_data ); $request->set_route( str_replace( 'store', 'STORE', $request->get_route() ) ); return $request; }, @@ -865,21 +864,40 @@ static function ( $pre, $parsed_args, $url ) use ( $image_url ) { }, ), - 'not_store_post_request' => array( - 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request { - $url_metric_data = $get_sample_url_metric_data(); - $url_metric_data['lcpElementExternalBackgroundImage'] = 'https://totally-different.example.com/'; - $request = $create_request( $url_metric_data ); - $request->set_method( 'GET' ); + 'invalid_external_bg_image_trailing_newline_route' => array( + 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request, $bad_origin_data ): WP_REST_Request { + $request = $create_request( + array_merge( + $get_sample_url_metric_data(), + array( 'lcpElementExternalBackgroundImage' => $bad_origin_data ) + ) + ); + $request->set_route( $request->get_route() . "\n" ); return $request; }, 'assert' => function ( WP_REST_Request $request ): void { + $this->assertArrayNotHasKey( 'lcpElementExternalBackgroundImage', $request ); + }, + ), + + 'not_store_post_request' => array( + 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request, $bad_origin_data ): WP_REST_Request { + $request = $create_request( + array_merge( + $get_sample_url_metric_data(), + array( 'lcpElementExternalBackgroundImage' => $bad_origin_data ) + ) + ); + $request->set_method( 'GET' ); + return $request; + }, + 'assert' => function ( WP_REST_Request $request ) use ( $bad_origin_data ): void { $this->assertArrayHasKey( 'lcpElementExternalBackgroundImage', $request ); - $this->assertSame( 'https://totally-different.example.com/', $request['lcpElementExternalBackgroundImage'] ); + $this->assertSame( $bad_origin_data, $request['lcpElementExternalBackgroundImage'] ); }, ), - 'not_store_request' => array( + 'not_store_request' => array( 'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request { $url_metric_data = $get_sample_url_metric_data(); $url_metric_data['lcpElementExternalBackgroundImage'] = 'https://totally-different.example.com/';