Skip to content

Commit 5ab7fd1

Browse files
committed
Add test case for route ending in newline
1 parent ba14c36 commit 5ab7fd1

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

plugins/image-prioritizer/helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ function image_prioritizer_filter_rest_request_before_callbacks( $response, arra
288288
if (
289289
$request->get_method() !== 'POST'
290290
||
291-
// The strtolower() is due to \WP_REST_Server::match_request_to_handler() using case-insensitive pattern match.
292-
OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE !== trim( strtolower( trim( $request->get_route(), '/' ) ) )
291+
// 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'.
292+
OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE !== rtrim( strtolower( ltrim( $request->get_route(), '/' ) ) )
293293
) {
294294
return $response;
295295
}

plugins/image-prioritizer/tests/test-helper.php

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -759,17 +759,19 @@ public function data_provider_to_test_image_prioritizer_filter_rest_request_befo
759759
return $request;
760760
};
761761

762+
$bad_origin_data = array(
763+
'url' => 'https://bad-origin.example.com/image.jpg',
764+
'tag' => 'DIV',
765+
'id' => null,
766+
'class' => null,
767+
);
768+
762769
return array(
763-
'invalid_external_bg_image' => array(
764-
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
770+
'invalid_external_bg_image' => array(
771+
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request, $bad_origin_data ): WP_REST_Request {
765772
$url_metric_data = $get_sample_url_metric_data();
766773

767-
$url_metric_data['lcpElementExternalBackgroundImage'] = array(
768-
'url' => 'https://bad-origin.example.com/image.jpg',
769-
'tag' => 'DIV',
770-
'id' => null,
771-
'class' => null,
772-
);
774+
$url_metric_data['lcpElementExternalBackgroundImage'] = $bad_origin_data;
773775
$url_metric_data['viewport']['width'] = 10101;
774776
$url_metric_data['viewport']['height'] = 20202;
775777
return $create_request( $url_metric_data );
@@ -786,7 +788,7 @@ public function data_provider_to_test_image_prioritizer_filter_rest_request_befo
786788
},
787789
),
788790

789-
'valid_external_bg_image' => array(
791+
'valid_external_bg_image' => array(
790792
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
791793
$url_metric_data = $get_sample_url_metric_data();
792794
$image_url = home_url( '/good.jpg' );
@@ -846,17 +848,14 @@ static function ( $pre, $parsed_args, $url ) use ( $image_url ) {
846848
},
847849
),
848850

849-
'invalid_external_bg_image_variant_route' => array(
850-
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
851-
$url_metric_data = $get_sample_url_metric_data();
852-
853-
$url_metric_data['lcpElementExternalBackgroundImage'] = array(
854-
'url' => 'https://bad-origin.example.com/image.jpg',
855-
'tag' => 'DIV',
856-
'id' => null,
857-
'class' => null,
851+
'invalid_external_bg_image_uppercase_route' => array(
852+
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request, $bad_origin_data ): WP_REST_Request {
853+
$request = $create_request(
854+
array_merge(
855+
$get_sample_url_metric_data(),
856+
array( 'lcpElementExternalBackgroundImage' => $bad_origin_data )
857+
)
858858
);
859-
$request = $create_request( $url_metric_data );
860859
$request->set_route( str_replace( 'store', 'STORE', $request->get_route() ) );
861860
return $request;
862861
},
@@ -865,21 +864,40 @@ static function ( $pre, $parsed_args, $url ) use ( $image_url ) {
865864
},
866865
),
867866

868-
'not_store_post_request' => array(
869-
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
870-
$url_metric_data = $get_sample_url_metric_data();
871-
$url_metric_data['lcpElementExternalBackgroundImage'] = 'https://totally-different.example.com/';
872-
$request = $create_request( $url_metric_data );
873-
$request->set_method( 'GET' );
867+
'invalid_external_bg_image_trailing_newline_route' => array(
868+
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request, $bad_origin_data ): WP_REST_Request {
869+
$request = $create_request(
870+
array_merge(
871+
$get_sample_url_metric_data(),
872+
array( 'lcpElementExternalBackgroundImage' => $bad_origin_data )
873+
)
874+
);
875+
$request->set_route( $request->get_route() . "\n" );
874876
return $request;
875877
},
876878
'assert' => function ( WP_REST_Request $request ): void {
879+
$this->assertArrayNotHasKey( 'lcpElementExternalBackgroundImage', $request );
880+
},
881+
),
882+
883+
'not_store_post_request' => array(
884+
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request, $bad_origin_data ): WP_REST_Request {
885+
$request = $create_request(
886+
array_merge(
887+
$get_sample_url_metric_data(),
888+
array( 'lcpElementExternalBackgroundImage' => $bad_origin_data )
889+
)
890+
);
891+
$request->set_method( 'GET' );
892+
return $request;
893+
},
894+
'assert' => function ( WP_REST_Request $request ) use ( $bad_origin_data ): void {
877895
$this->assertArrayHasKey( 'lcpElementExternalBackgroundImage', $request );
878-
$this->assertSame( 'https://totally-different.example.com/', $request['lcpElementExternalBackgroundImage'] );
896+
$this->assertSame( $bad_origin_data, $request['lcpElementExternalBackgroundImage'] );
879897
},
880898
),
881899

882-
'not_store_request' => array(
900+
'not_store_request' => array(
883901
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
884902
$url_metric_data = $get_sample_url_metric_data();
885903
$url_metric_data['lcpElementExternalBackgroundImage'] = 'https://totally-different.example.com/';

0 commit comments

Comments
 (0)