Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions features/http-mocking.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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!
"""
12 changes: 12 additions & 0 deletions src/Context/GivenStepDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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(
Comment on lines +324 to 328
'headers' => \$response->headers->getAll(),
'body' => \$response->body,
Expand Down
Loading