From 82f72932266513e5542ff4055bc98c2531f44efa Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 24 May 2026 11:45:16 +0200 Subject: [PATCH] HTTP mocking: support `filename` option --- features/http-mocking.feature | 17 +++++++++++++++++ src/Context/GivenStepDefinitions.php | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/features/http-mocking.feature b/features/http-mocking.feature index d970936c8..0f0ee9a47 100644 --- a/features/http-mocking.feature +++ b/features/http-mocking.feature @@ -119,3 +119,20 @@ Feature: HTTP request mocking Then STDOUT should be a table containing rows: | version | update_type | package_url | | 999.9.9 | major | https://downloads.wordpress.org/release/wordpress-999.9.9.zip | + + Scenario: Mock HTTP request with filename option writes to disk + Given an empty directory + And that HTTP requests to https://example.com/mocked-file.txt will respond with: + """ + HTTP/1.1 200 OK + Content-Type: text/plain + + Mocked file contents on disk! + """ + + When I try `wp eval 'WP_CLI\Utils\http_request("GET", "https://example.com/mocked-file.txt", null, [], ["filename" => "downloaded.txt"]);' --skip-wordpress` + Then the return code should be 0 + And the downloaded.txt file should contain: + """ + Mocked file contents on disk! + """ diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index d8cbe02a8..f12e10298 100644 --- a/src/Context/GivenStepDefinitions.php +++ b/src/Context/GivenStepDefinitions.php @@ -228,6 +228,14 @@ public function request( \$url, \$headers = array(), \$data = array(), \$options if ( false !== \$pos ) { \$response = substr( \$response, 0, \$pos ) . "\\r\\n\\r\\n" . substr( \$response, \$pos + 2 ); } + if ( ! empty( \$options['filename'] ) ) { + \$body = ''; + \$body_pos = strpos( \$response, "\\r\\n\\r\\n" ); + if ( false !== \$body_pos ) { + \$body = substr( \$response, \$body_pos + 4 ); + } + file_put_contents( \$options['filename'], \$body ); + } return \$response; } } @@ -313,6 +321,10 @@ static function( \$pre, \$parsed_args, \$url ) { ); } + if ( ! empty( \$parsed_args['filename'] ) ) { + file_put_contents( \$parsed_args['filename'], \$response->body ); + } + return array( 'headers' => \$response->headers->getAll(), 'body' => \$response->body,