|
13 | 13 |
|
14 | 14 | final class MiddlewareRunnerTest extends TestCase |
15 | 15 | { |
| 16 | + /** |
| 17 | + * @expectedException RuntimeException |
| 18 | + * @expectedExceptionMessage No middleware to run |
| 19 | + */ |
16 | 20 | public function testDefaultResponse() |
17 | 21 | { |
18 | | - $this->setExpectedException('\RuntimeException'); |
19 | 22 | $request = new ServerRequest('GET', 'https://example.com/'); |
20 | 23 | $middlewares = array(); |
21 | 24 | $middlewareStack = new MiddlewareRunner($middlewares); |
@@ -90,6 +93,42 @@ public function testProcessStack(array $middlewares, $expectedCallCount) |
90 | 93 | } |
91 | 94 | } |
92 | 95 |
|
| 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 | + |
93 | 132 | public function testMultipleRunsInvokeAllMiddlewareInCorrectOrder() |
94 | 133 | { |
95 | 134 | $requests = array( |
|
0 commit comments