PHPackages                             cspray/http-client-test-interceptor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. cspray/http-client-test-interceptor

ActiveLibrary[Testing &amp; Quality](/categories/testing)

cspray/http-client-test-interceptor
===================================

21161[6 issues](https://github.com/cspray/http-client-test-interceptor/issues)[1 PRs](https://github.com/cspray/http-client-test-interceptor/pulls)PHPCI passing

Since Feb 11Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/cspray/http-client-test-interceptor)[ Packagist](https://packagist.org/packages/cspray/http-client-test-interceptor)[ RSS](/packages/cspray-http-client-test-interceptor/feed)WikiDiscussions main Synced 4w ago

READMEChangelogDependenciesVersions (5)Used By (0)

HttpClient TestInterceptor
==========================

[](#httpclient-testinterceptor)

A framework for testing [amphp/http-client](https://github.com/amphp/http-client) v5 using the application interceptor design pattern it introduces. The primary reason for using this library is in your Unit or Integration Tests to test known HTTP interactions. This library provides 2 mechanisms for testing these interactions:

1. Writing **unit tests** where every Request should be mocked and **no network requests** are made.
2. Writing **integration tests** where a Request will be sent over the network if there isn't a *fixture* stored for that Request. The response received from the network request will be stored and subsequent requests will not go over the network.

Whether you're writing unit or integration tests the same underlying mechanisms are used. You can specify fine-grained pieces of the Request that should be matched against or provide your own strategy for matching Requests against a Fixture or Mock.

There are plenty of reasons ***not*** to use this library:

- You aren't using [amphp/http-client](https://github.com/amphp/http-client) v5 or greater.
- You already have a viable, working solution for testing your API interactions.

Installation
------------

[](#installation)

```
composer require amphp/http-client:v5.0.0-beta.3
composer require --dev cspray/http-client-test-interceptor

```

> The amphp/http-client library is still in beta. You may need to adjust your minimum stability until http-client releases a 5.0 version.

Unit Testing Quick Start
------------------------

[](#unit-testing-quick-start)

Unit testing is meant to test your HttpClient against specific, known HTTP interactions. For example, you may need to test how your code under test responds to different headers, specific error codes, or a precise body structure. These tests can often be hard, or even impossible, to test reliably with integration tests.

Unlike writing integration tests that use fixtures there's no setup steps necessary. The first step is to write your TestCase!

```
