Skip to content

Commit

Permalink
[11.x] Http Client: fake connection exception (#53485)
Browse files Browse the repository at this point in the history
* Support faking connection exception

* Fix linting issues

* Fix another linting issue

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
gdebrauwer and taylorotwell authored Nov 12, 2024
1 parent 24548e7 commit 4eba938
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
25 changes: 22 additions & 3 deletions Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Http\Client;

use Closure;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Middleware;
use GuzzleHttp\Promise\Create;
use GuzzleHttp\Promise\PromiseInterface;
Expand Down Expand Up @@ -164,6 +165,22 @@ public static function response($body = null, $status = 200, $headers = [])
return Create::promiseFor($response);
}

/**
* Create a new connection exception for use during stubbing.
*
* @param string|null $message
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public static function failedConnection($message = null)
{
return function ($request) use ($message) {
return Create::rejectionFor(new ConnectException(
$message ?? "cURL error 6: Could not resolve host: {$request->toPsrRequest()->getUri()->getHost()} (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for {$request->toPsrRequest()->getUri()}.",
$request->toPsrRequest(),
));
};
}

/**
* Get an invokable object that returns a sequence of responses in order for use during stubbing.
*
Expand Down Expand Up @@ -203,9 +220,11 @@ public function fake($callback = null)

$this->stubCallbacks = $this->stubCallbacks->merge(collect([
function ($request, $options) use ($callback) {
$response = $callback instanceof Closure
? $callback($request, $options)
: $callback;
$response = $callback;

while ($response instanceof Closure) {
$response = $response($request, $options);
}

if ($response instanceof PromiseInterface) {
$options['on_stats'](new TransferStats(
Expand Down
23 changes: 20 additions & 3 deletions Client/ResponseSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Http\Client;

use Closure;
use Illuminate\Support\Traits\Macroable;
use OutOfBoundsException;

Expand Down Expand Up @@ -71,7 +72,7 @@ public function pushStatus(int $status, array $headers = [])
}

/**
* Push response with the contents of a file as the body to the sequence.
* Push a response with the contents of a file as the body to the sequence.
*
* @param string $filePath
* @param int $status
Expand All @@ -87,6 +88,19 @@ public function pushFile(string $filePath, int $status = 200, array $headers = [
);
}

/**
* Push a connection exception to the sequence.
*
* @param string|null $message
* @return $this
*/
public function pushFailedConnection($message = null)
{
return $this->pushResponse(
Factory::failedConnection($message)
);
}

/**
* Push a response to the sequence.
*
Expand Down Expand Up @@ -137,11 +151,12 @@ public function isEmpty()
/**
* Get the next response in the sequence.
*
* @param \Illuminate\Http\Client\Request $request
* @return mixed
*
* @throws \OutOfBoundsException
*/
public function __invoke()
public function __invoke($request)
{
if ($this->failWhenEmpty && $this->isEmpty()) {
throw new OutOfBoundsException('A request was made, but the response sequence is empty.');
Expand All @@ -151,6 +166,8 @@ public function __invoke()
return value($this->emptyResponse ?? Factory::response());
}

return array_shift($this->responses);
$response = array_shift($this->responses);

return $response instanceof Closure ? $response($request) : $response;
}
}

0 comments on commit 4eba938

Please sign in to comment.