Skip to content

Commit

Permalink
Merge pull request #207 from Sammyjo20/fix/response-middleware-not-re…
Browse files Browse the repository at this point in the history
…turning-new-instance

Fix | Fixed bug with responses not returning the new instance
  • Loading branch information
Sammyjo20 committed Apr 30, 2023
2 parents 35e1243 + 22618c3 commit c2e4eee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/Http/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,7 @@ protected function executeRequestPipeline(): static
*/
public function executeResponsePipeline(ResponseContract $response): ResponseContract
{
$this->middleware()->executeResponsePipeline($response);

return $response;
return $this->middleware()->executeResponsePipeline($response);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/SaloonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

declare(strict_types=1);

use GuzzleHttp\Psr7\Utils;
use GuzzleHttp\Psr7\Response;
use Saloon\Http\PendingRequest;
use Saloon\Contracts\ArrayStore;
use Illuminate\Support\Collection;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Symfony\Component\DomCrawler\Crawler;
use Saloon\Http\Response as SaloonResponse;
use Saloon\Exceptions\Request\RequestException;
use Saloon\Tests\Fixtures\Requests\UserRequest;
use Saloon\Tests\Fixtures\Connectors\TestConnector;

test('you can get the original pending request', function () {
$mockClient = new MockClient([
Expand Down Expand Up @@ -250,3 +253,25 @@
expect($response->body())->toEqual('{"foo":"bar"}');
expect($response->object())->toEqual((object)['foo' => 'bar']);
});

test('if a response is changed through middleware the new instance is used', function () {

$mockClient = new MockClient([
MockResponse::make(['foo' => 'bar'], 200, ['X-Custom-Header' => 'Howdy']),
]);

$connector = new TestConnector;

$connector->middleware()->onResponse(function (SaloonResponse $response) {
// Let's modify the body while sending!
$psrResponse = $response->getPsrResponse();
$newPsrResponse = $psrResponse->withBody(Utils::streamFor('Hello World!'));

return $response::fromPsrResponse($newPsrResponse, $response->getPendingRequest());
});

$response = $connector->send(new UserRequest, $mockClient);

expect($response->body())->toEqual('Hello World!');
expect($response->headers()->all())->toEqual(['X-Custom-Header' => 'Howdy']);
});

0 comments on commit c2e4eee

Please sign in to comment.