Skip to content

Commit

Permalink
[10.x] Fix recorderHandler not recording changes made by middleware (#…
Browse files Browse the repository at this point in the history
…47614)

* Add the recorder handler after the middleware on the stack, otherwise middleware that modifies the request is not recorded

* Add test for middleware that modifies the request
  • Loading branch information
j3j5 authored Jun 29, 2023
1 parent 830efbe commit f03e652
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1155,12 +1155,12 @@ public function pushHandlers($handlerStack)
{
return tap($handlerStack, function ($stack) {
$stack->push($this->buildBeforeSendingHandler());
$stack->push($this->buildRecorderHandler());

$this->middleware->each(function ($middleware) use ($stack) {
$stack->push($middleware);
});

$stack->push($this->buildRecorderHandler());
$stack->push($this->buildStubHandler());
});
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OutOfBoundsException;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Symfony\Component\VarDumper\VarDumper;
Expand Down Expand Up @@ -1760,6 +1761,27 @@ public function testMiddlewareRunsWhenFaked()
$this->assertSame(['hyped-for' => 'laravel-movie'], json_decode(tap($history[0]['request']->getBody())->rewind()->getContents(), true));
}

public function testMiddlewareRunsAndCanChangeRequestOnAssertSent()
{
$this->factory->fake(function (Request $request) {
return $this->factory->response('Fake');
});


$pendingRequest = $this->factory->withMiddleware(
Middleware::mapRequest(fn (RequestInterface $request) => $request->withHeader('X-Test-Header', 'Test'))
);

$pendingRequest->post('https://laravel.example', ['laravel' => 'framework']);

$this->factory->assertSent(function (Request $request) {
return
$request->url() === 'https://laravel.example' &&
$request->hasHeader('X-Test-Header', 'Test');
});

}

public function testRequestExceptionIsNotThrownIfThePendingRequestIsSetToThrowOnFailureButTheResponseIsSuccessful()
{
$this->factory->fake([
Expand Down

0 comments on commit f03e652

Please sign in to comment.