Skip to content

Commit

Permalink
Add test case for route ending in newline
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Dec 17, 2024
1 parent ba14c36 commit 5ab7fd1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
4 changes: 2 additions & 2 deletions plugins/image-prioritizer/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
72 changes: 45 additions & 27 deletions plugins/image-prioritizer/tests/test-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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' );
Expand Down Expand Up @@ -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;
},
Expand All @@ -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/';
Expand Down

0 comments on commit 5ab7fd1

Please sign in to comment.