Skip to content

Commit 001f0a9

Browse files
authored
Merge pull request #238 from WyriHaximus/middleware-runner-correction-tests
Test middleware runner to ensure next handler can be called more than once
2 parents 7c6c705 + 345b28e commit 001f0a9

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

tests/MiddlewareRunnerTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,42 @@ public function testProcessStack(array $middlewares, $expectedCallCount)
9393
}
9494
}
9595

96+
public function testNextCanBeRunMoreThanOnceWithoutCorruptingTheMiddlewareStack()
97+
{
98+
$exception = new \RuntimeException('exception');
99+
$retryCalled = 0;
100+
$error = null;
101+
$retry = function ($request, $next) use (&$error, &$retryCalled) {
102+
return $next($request)->then(null, function ($et) use (&$error, $request, $next, &$retryCalled) {
103+
$retryCalled++;
104+
$error = $et;
105+
// the $next failed. discard $error and retry once again:
106+
return $next($request);
107+
});
108+
};
109+
110+
$response = new Response();
111+
$called = 0;
112+
$runner = new MiddlewareRunner(array(
113+
$retry,
114+
function () use (&$called, $response, $exception) {
115+
$called++;
116+
if ($called === 1) {
117+
throw $exception;
118+
}
119+
120+
return $response;
121+
}
122+
));
123+
124+
$request = new ServerRequest('GET', 'https://example.com/');
125+
126+
$this->assertSame($response, Block\await($runner($request), Factory::create()));
127+
$this->assertSame(1, $retryCalled);
128+
$this->assertSame(2, $called);
129+
$this->assertSame($exception, $error);
130+
}
131+
96132
public function testMultipleRunsInvokeAllMiddlewareInCorrectOrder()
97133
{
98134
$requests = array(

0 commit comments

Comments
 (0)