Last active
June 2, 2020 06:08
-
-
Save westonruter/d34f49b6288ed52604f7e9b6926810a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* AMP Validation Request Error Simulator | |
* | |
* @package AMP_Validation_Request_Error_Simulator | |
* @author Weston Ruter, Google | |
* @license GPL-2.0-or-later | |
* @copyright 2019 Google Inc. | |
* | |
* @wordpress-plugin | |
* Plugin Name: AMP Validation Request Error Simulator | |
* Description: Simulate error scenarios for requesting AMP validation. | |
* Plugin URI: https://gist.github.com/westonruter/d34f49b6288ed52604f7e9b6926810a9 | |
* Version: 0.1.0 | |
* Author: Weston Ruter, Google | |
* Author URI: https://weston.ruter.net/ | |
* License: GNU General Public License v2 (or later) | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* Gist Plugin URI: https://gist.github.com/westonruter/d34f49b6288ed52604f7e9b6926810a9 | |
*/ | |
namespace AMP_Validation_Request_Error_Simulator; | |
const QUERY_ARG = 'amp_simulate_validate_request_error'; | |
add_filter( | |
'query_vars', | |
function( $query_vars ) { | |
$query_vars[] = QUERY_ARG; | |
return $query_vars; | |
} | |
); | |
add_filter( | |
'pre_http_request', | |
function( $pre, $request_args, $url ) { | |
unset( $request_args ); | |
if ( ! current_user_can( 'manage_options' ) ) { | |
return $pre; | |
} | |
$query = trim( wp_parse_url( $url, PHP_URL_QUERY ), '?' ); | |
if ( ! $query ) { | |
return $pre; | |
} | |
$query_args = wp_parse_args( $query ); | |
if ( ! isset( $query_args['amp_validate'], $query_args[ QUERY_ARG ] ) ) { | |
return $pre; | |
} | |
switch ( $query_args[ QUERY_ARG ] ) { | |
case 'bad_host': | |
return new \WP_Error( | |
'http_request_failed', | |
sprintf( 'cURL error 6: Could not resolve host: %s', wp_parse_url( home_url(), PHP_URL_HOST ) ) | |
); | |
case 'timeout': | |
return new \WP_Error( | |
'http_request_failed', | |
'cURL error 28: Operation timed out after 15001 milliseconds with 0 bytes received' | |
); | |
case 'wsod': | |
return [ | |
'headers' => [], | |
'body' => '', | |
'response' => [ | |
'code' => 500, | |
'message' => 'Internal Server Error', | |
], | |
'http_response' => null, | |
]; | |
case 'response_comment_absent': | |
return [ | |
'headers' => [], | |
'body' => '<html></html>', | |
'response' => [ | |
'code' => 200, | |
'message' => 'OK', | |
], | |
'http_response' => null, | |
]; | |
default: | |
return $pre; | |
} | |
}, | |
10, | |
3 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c