sayuprc / http-test-case
Library for HTTP testing
Installs: 84
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/sayuprc/http-test-case
Requires
- php: ^8.2
- phpunit/phpunit: ^11.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^2.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.8.0
- guzzlehttp/psr7: ^2.6.1
- phpstan/phpstan: ^2.0
- symplify/easy-coding-standard: ^12.5
README
English | 日本語
sayuprc/http-test-case
A library for HTTP testing.
Requirements
| name | version |
|---|---|
| PHP | ^8.3 |
| PHPUnit | ^12.0 |
Installation
composer require --dev sayuprc/http-test-case
Usage
Extend the HttpTestCase class and implement the following methods:
getClient()getRequestFactory()getUriFactory()getStreamFactory()
<?php use GuzzleHttp\Client; use GuzzleHttp\Psr7\HttpFactory; use HttpTest\HttpTestCase; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UriFactoryInterface; class SampleTest extends HttpTestCase { protected function getClient(): ClientInterface { return new Client(); } protected function getRequestFactory(): RequestFactoryInterface { return new HttpFactory(); } protected function getUriFactory(): UriFactoryInterface { return new HttpFactory(); } protected function getStreamFactory(): StreamFactoryInterface { return new HttpFactory(); } }
Testing a GET Request
<?php use HttpTest\HttpTestCase; class SampleTest extends HttpTestCase { public function testGet() { $response = $this->get('https://example.com'); $response->assertStatusCode(200); } }
Testing a POST Request
<?php use HttpTest\HttpTestCase; class SampleTest extends HttpTestCase { public function testPost() { $response = $this->post( 'https://example.com', [ 'headers' => [ 'Content-Type' => 'application/json', ], 'json' => [ 'key' => 'value', ], ] ); $response->assertStatusCode(200); } }
Documents
Please refer to the here for the methods and assertions of HttpTestCase.