sayuprc / http-test-case
Library for HTTP testing
v4.0.0
2025-02-16 04:14 UTC
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
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.