Conversation
|
First of all, big fan of this PR 👍 However, it might be worthwhile to take some inspiration from Sjorso/Gobble, specifically how easy Gobble makes it to push a sequence of responses: Gobble::fake()
->pushJson(['fact' => 'Cats are great!'])
->pushFile('tests/Fixtures/huge-google-maps-api-response.json')
->pushEmptyResponse(403);
// vs
Http::fake([
'*' => Http::sequence([
Http::response(['fact' => 'Cats are great!'], 200),
Http::response([file_get_contents('tests/Fixtures/huge-google-maps-api-response.json'), 200),
Http::response('', 403),
]),
]);Gobble uses the normal Guzzle way of mocking responses, by using a mock handler filled with a sequence of responses. If a Guzzle call is made, but the mock handler is empty, an Also, when Gobble records responses, it wraps them in a Either way, I'll most likely be deprecating Gobble when 7.0 is released, and re-write my tests to use this new built-in HTTP client instead. |
|
@SjorsO there may be opportunity down the road to add some syntactic sugar to streamline the stubbing of specific requests. As noted in the PR description, |
I've been using Gobble for the past two years on many client and personal projects, so I am definitely biased, but I feel like the Gobble way of mocking responses is a better developer experience and is worth considering before this PR is merged. |
|
@SjorsO once this is merged you could probably pretty easily PR so that Http::sequence() without arguments returns some kind of SequenceBuilder that you can chain onto. I like your syntax overall. |
This is a port of @adamwathan's ZTTP Guzzle convenience layer. It provides much nicer syntax for the 90% use case of Guzzle where you just need to POST some JSON to an endpoint. In addition, this supplements ZTTP with stubbing / faking inspired by @jasonmccreary's work on that library.
This is not an entirely new client - it is only a UX / DX convenience layer on top of Guzzle. We will not be adding a lot of complicated features to this. If you need very, very robust or complicated logic just use Guzzle directly. We aren't trying to expose every feature of Guzzle through this API.
Basic Usage Overview
By default, the content type is
application/json. You may also easily send form-urlencoded requests:You may also send files as multi-part requests:
Headers / Authentication
Headers may be added using the
withHeadersmethod:Authorization / bearer tokens...
Basic authentication...
Errors
By default, like ZTTP, exceptions are not thrown on client and server errors. If you receive a response that is not successful and you would like to throw an exception, you may call
$response->throw():Testing / Faking
Like some other services in Laravel, the HTTP client supports faking. When calling
Http::fake()without any additional arguments, all responses will be empty 200 level responses:However, you may stub the responses returned by specific endpoints by passing an array to the fake method, any requests that are not to URLs matching the given pattern will actually be executed. In this example, I will use the
Http::responsemethod to quickly generate a stub response that should be returned.A final fallback route could also be provided:
In addition, you could specify a sequence of responses to be returned. These responses will be returned in order as the endpoint is called:
Finally, for maximum flexibility you may pass a Closure to
Http::fakewhich can examine the request, perform conditional logic, and return an appropriate response: