Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Ensure Next processes the next delegate correctly when an error is present #85

Merged
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
4 changes: 3 additions & 1 deletion src/Next.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ private function dispatchNextDelegate(
ResponseInterface $response = null,
$err = null
) {
if ($nextDelegate instanceof DelegateInterface) {
if ($nextDelegate instanceof DelegateInterface
&& (! $nextDelegate instanceof Next || $err === null)
) {
return $nextDelegate->process($request);
}

Expand Down
21 changes: 21 additions & 0 deletions test/MiddlewarePipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,27 @@ public function testWillDecorateCallableMiddlewareAsInteropMiddlewareIfResponseP
$this->assertAttributeSame($this->response, 'responsePrototype', $test);
}

/**
* @group http-interop
*/
public function testWillNotDecorateCallableMiddlewareAsInteropMiddlewareIfResponsePrototypeIsNotPresent()
{
$pipeline = new MiddlewarePipe();

$middleware = function () {
};
$pipeline->pipe($middleware);

$r = new ReflectionProperty($pipeline, 'pipeline');
$r->setAccessible(true);
$queue = $r->getValue($pipeline);

$route = $queue->dequeue();
$test = $route->handler;
$this->assertNotInstanceOf(CallableMiddlewareWrapper::class, $test);
$this->assertInternalType('callable', $test);
}

/**
* @todo Remove with 2.0.0
*/
Expand Down
35 changes: 35 additions & 0 deletions test/NextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,4 +910,39 @@ public function testEnablingRaiseThrowablesFlagWillCauseInvocationToRaiseMiddlew

$this->assertTrue($triggered, 'Deprecation notice not triggered');
}

/**
* @todo Remove for 2.0.0
* @group error-handling
*/
public function testNestedNextInvocationWithAnErrorShouldDispatchErrorMiddleware()
{
$internalQueue = clone $this->queue;
$internalQueue->enqueue(new Route('/', function ($request, $response, $next) {
return $next($request, $response, 'ERROR');
}));

$nextDelegateQueue = clone $this->queue;
$nextDelegateQueue->enqueue(new Route('/', function ($err, $request, $response, $next) {
$response->getBody()->write('ERROR DETECTED');
return $response->withStatus(599);
}));

$finalDelegate = $this->prophesize(DelegateInterface::class);
$finalDelegate->process(Argument::any())->shouldNotBeCalled();

$nextDelegate = new Next($nextDelegateQueue, $finalDelegate->reveal());
$internalNext = new Next($internalQueue, $nextDelegate);

set_error_handler(function ($errno, $errstr) {
return false !== strstr($errstr, 'error middleware is deprecated');
}, E_USER_DEPRECATED);

$response = $internalNext($this->request, $this->response);

restore_error_handler();

$this->assertEquals(599, $response->getStatusCode());
$this->assertEquals('ERROR DETECTED', (string) $response->getBody());
}
}