Skip to content

Commit

Permalink
Improve test_rest_api_availability
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Feb 6, 2025
1 parent 0edd448 commit 5c161b2
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions plugins/optimization-detective/tests/test-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,23 @@ public function test_rest_api_availability( $mocked_response, string $expected_o
$this->filter_rest_api_response( $mocked_response );

$result = od_test_rest_api_availability();
if ( 'nginx_forbidden' === $this->dataName() ) {
$notice = get_echo( 'od_render_rest_api_health_check_admin_notice_in_plugin_row', array( 'optimization-detective/load.php' ) );
$this->assertStringContainsString( '</iframe>', $notice );
$notice = get_echo( 'od_render_rest_api_health_check_admin_notice_in_plugin_row', array( 'optimization-detective/load.php' ) );
if ( $expected_unavailable ) {
$this->assertStringContainsString( '<code>', $notice );
if ( is_array( $mocked_response ) ) {
if ( isset( $mocked_response['headers']['content-type'] ) && str_contains( join( '', (array) $mocked_response['headers']['content-type'] ), 'html' ) ) {
$this->assertStringContainsString( '</iframe>', $notice );
$this->assertStringNotContainsString( '</pre>', $notice );
} else {
$this->assertStringContainsString( '</pre>', $notice );
$this->assertStringNotContainsString( '</iframe>', $notice );
}
} else {
$this->assertStringNotContainsString( '</iframe>', $notice );
$this->assertStringNotContainsString( '</pre>', $notice );
}
} else {
$this->assertSame( '', $notice );
}
$this->assertArrayHasKey( 'label', $result );
$this->assertArrayHasKey( 'status', $result );
Expand Down Expand Up @@ -417,7 +431,7 @@ static function ( $pre, array $args, string $url ) use ( $mocked_response, $obse
}

/**
* Build a mock response.
* Build a mock JSON response.
*
* @param int $status_code HTTP status code.
* @param string $message HTTP status message.
Expand All @@ -431,6 +445,9 @@ protected function build_mock_response( int $status_code, string $message, array
'message' => $message,
),
'body' => wp_json_encode( $body ),
'headers' => array(
'content-type' => 'application/json',
),
);
}
}

0 comments on commit 5c161b2

Please sign in to comment.