https://github.com/pronamic/wp-http
WordPress HTTP library.
https://github.com/pronamic/wp-http
api faking-responses http library unit-testing wordpress
Last synced: 3 months ago
JSON representation
WordPress HTTP library.
- Host: GitHub
- URL: https://github.com/pronamic/wp-http
- Owner: pronamic
- License: gpl-2.0
- Created: 2021-03-09T13:28:32.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-12-17T12:45:41.000Z (10 months ago)
- Last Synced: 2025-05-29T23:05:03.533Z (5 months ago)
- Topics: api, faking-responses, http, library, unit-testing, wordpress
- Language: PHP
- Homepage: https://pronamic.github.io/wp-http/
- Size: 224 KB
- Stars: 6
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# WordPress HTTP
WordPress HTTP library.
## Usage
```php
$response = Http::request( $url, $args );$data = $response->json();
```## Testing
### Faking Responses
```php
$url = 'https://www.pronamic.nl/wp-json/wp/v2/types/post';Http::fake( $url, __DIR__ . '/../http/pronamic-nl-wp-json-types-post.http' );
$response = \wp_remote_get( $url );
// or
$response = Http::get( $url );
```### PHPUnit
```php
factory = new Factory();
}/**
* Test request.
*/
public function test_request() {
$this->factory->fake( 'http://example.com/', __DIR__ . '/../http/example-com.http' );$result = \wp_remote_get( 'http://example.com/' );
// asserts
}
}```
### CR LF
To store fake HTTP responses in `*.http` files and Git, keep the following in mind:
> HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements
https://tools.ietf.org/html/rfc2616#section-2.2
**`.gitattributes`**
```
*.http text eol=crlf
```You can use a tool like `unix2dos` to convert the line endings to CR LF:
```
unix2dos *.http
```If the line endings are not correct this can result in the following error:
```
Undefined offset: 2wordpress/wp-includes/class-http.php:732
src/Factory.php:97
wordpress/wp-includes/class-wp-hook.php:292
wordpress/wp-includes/plugin.php:212
wordpress/wp-includes/class-http.php:257
wordpress/wp-includes/class-http.php:626
wordpress/wp-includes/http.php:162
src/Facades/Http.php:71
```## Inspiration
- https://github.com/WordPress/WordPress/blob/5.7/wp-includes/class-http.php
- https://www.php-fig.org/psr/psr-7/
- https://github.com/guzzle/psr7
- https://github.com/jdgrimes/wp-http-testcase
- https://docs.guzzlephp.org/en/stable/testing.html
- https://github.com/guzzle/guzzle
- https://github.com/namshi/cuzzle
- https://docs.guzzlephp.org/en/stable/handlers-and-middleware.html
- https://laravel.com/docs/8.x/http-client#faking-responses
- https://www.php-fig.org/psr/psr-18/[](https://www.pronamic.eu/contact/)