Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/HttpSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ function (HttpRequestException|TransferException $exception) use ($pendingReques

$response = $this->createResponse($exception->response->toPsrResponse(), $pendingRequest, $psrRequest, $exception);

// Throw the exception our way
// Throw the exception our way if there is an exception.
// Otherwise we'll return the response.

throw $response->toException();
return ($exception = $response->toException()) ? throw $exception : $response;
}
);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Feature/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Saloon\HttpSender\Tests\Fixtures\Requests\UserRequest;
use Saloon\HttpSender\Tests\Fixtures\Connectors\HttpSenderConnector;
use Saloon\HttpSender\Tests\Fixtures\Connectors\InvalidConnectionConnector;
use Saloon\HttpSender\Tests\Fixtures\Requests\ErrorRequestThatShouldBeTreatedAsSuccessful;

test('you can create a pool on a connector', function () {
$connector = new HttpSenderConnector;
Expand Down Expand Up @@ -76,3 +77,26 @@

expect($count)->toEqual(5);
});

test('if a pool has a failed response that should be treated as a successful response', function () {
$count = 0;

$pool = (new HttpSenderConnector)->pool([
new ErrorRequestThatShouldBeTreatedAsSuccessful,
]);

$pool->withResponseHandler(function (Response $response) use (&$count) {
expect($response)->toBeInstanceOf(Response::class);
expect($response->status())->toBe(404);

$count++;
});

$promise = $pool->send();

expect($promise)->toBeInstanceOf(PromiseInterface::class);

$promise->wait();

expect($count)->toEqual(1);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Saloon\HttpSender\Tests\Fixtures\Requests;

use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class ErrorRequestThatShouldBeTreatedAsSuccessful extends Request
{
/**
* HTTP Method
*/
protected Method $method = Method::GET;

/**
* Resolve the endpoint
*/
public function resolveEndpoint(): string
{
return '/not-found-error';
}

public function hasRequestFailed(Response $response): ?bool
{
return $response->status() !== 404;
}
}