Description
I'm trying to test some HTTP requests generated by WordPress HTTP functions, such as wp_remote_post
and wp_remote_get
.
PHP-VCR is able to successfully record the HTTP request using the curl
library hook and store it in either YAML or JSON format. However, the HTTP request throws an error on the application code, no matter how many times I try to run the test:
WP_Error Object
(
[errors] => Array
(
[http_request_failed] => Array
(
[0] => Missing header/body separator
)
)
[error_data] => Array
(
)
)
The error goes away if I disable PHP-VCR.
I have tested it with endpoints from Google, Facebook, and other sites. It does not seem related to any particular website.
Google search was not very helpful. However, the same error message appeared in #115, which appears to have been fixed a long time ago.
Example
class Example_TestCase extends WP_UnitTestCase {
public function test_should_get_index_from_example_com() {
VCR::turnOn();
VCR::insertCassette( 'get_example_com.yml' );
$response = wp_remote_get( 'http://example.com' );
print_r( $response );
VCR::eject();
VCR::turnOff();
}
}
Here is the cassette recorded:
-
request:
method: GET
url: 'http://example.com/'
headers:
Host: example.com
Connection: close
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Content-Encoding: gzip
Accept-Ranges: bytes
Age: '479592'
Cache-Control: max-age=604800
Content-Type: 'text/html; charset=UTF-8'
Date: 'Wed, 19 Aug 2020 17:01:20 GMT'
Etag: '"3147526947"'
Expires: 'Wed, 26 Aug 2020 17:01:20 GMT'
Last-Modified: 'Thu, 17 Oct 2019 07:18:26 GMT'
Server: 'ECS (mic/9A9D)'
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: '648'
Connection: close
body: "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 2em;\n background-color: #fdfdff;\n border-radius: 0.5em;\n box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n div {\n margin: 0 auto;\n width: auto;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.</p>\n <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n"
Here is the test output:
Starting test 'Example_TestCase::test_should_get_index_from_example_com'.
RWP_Error Object
(
[errors] => Array
(
[http_request_failed] => Array
(
[0] => Missing header/body separator
)
)
[error_data] => Array
(
)
)
Environment
I'm running WordPress 5.2.4 and PHP 7.
I'm using PHP-VCR version 1.4.5 installed through Composer. Unfortunately, I couldn't test it with PHP-VCR version 1.5.0 (latest version) because of the issue reported in #328.
I'm running tests in this Docker container, which is based on the php-unit/php-unit:6.0.6
image. If you look at the Dockerfile, you can see this image has PHP 7 installed through Alpine and all necessary dependencies, including curl
and php7-curl
.