diff --git a/features/profile-requests.feature b/features/profile-requests.feature index 3f85d0f..50ddd1f 100644 --- a/features/profile-requests.feature +++ b/features/profile-requests.feature @@ -2,23 +2,37 @@ Feature: Profile HTTP requests Scenario: Profile HTTP requests during WordPress load Given a WP install + And that HTTP requests to https://www.apple.com/ will respond with: + """ + HTTP/1.1 200 + Content-Type: text/plain - When I run `wp profile requests --fields=method,url,status,time` - Then STDOUT should contain: + Hello world """ - method + And that HTTP requests to https://www.example.com/ will respond with: """ + HTTP/1.1 201 + Content-Type: application/json + + {"status":"created"} + """ + And a wp-content/mu-plugins/http-requests.php file: + """ + 'test' ) ); + }); + """ + + When I run `wp profile requests --fields=method,url` + Then STDOUT should be a table containing rows: + | method | url | + | GET | https://www.apple.com/ | + | POST | https://www.example.com/ | And STDOUT should contain: """ - url - """ - And STDOUT should contain: - """ - status - """ - And STDOUT should contain: - """ - time + total (2) """ Scenario: Profile shows no requests when none are made diff --git a/src/Profiler.php b/src/Profiler.php index c572bdb..228d479 100644 --- a/src/Profiler.php +++ b/src/Profiler.php @@ -391,10 +391,15 @@ class Profiler { // For request profiling, capture details of each HTTP request if ( 'request' === $this->type ) { + // Reset properties first to handle cases where previous request was preempted + $this->request_start_time = null; + $this->request_args = null; + + // Now capture the new request details $this->request_start_time = microtime( true ); $this->request_args = array( 'url' => $url, - 'method' => isset( $parsed_args['method'] ) ? $parsed_args['method'] : 'GET', + 'method' => ( is_array( $parsed_args ) && isset( $parsed_args['method'] ) ) ? $parsed_args['method'] : 'GET', ); } @@ -421,7 +426,7 @@ class Profiler { $status = $response['response']['code']; } - $logger = new Logger( + $logger = new Logger( array( 'method' => $this->request_args['method'], 'url' => $this->request_args['url'],