|
5 | 5 | namespace Hypervel\Tests\Broadcasting; |
6 | 6 |
|
7 | 7 | use Hyperf\Contract\ConfigInterface; |
| 8 | +use Hyperf\HttpServer\Router\DispatcherFactory as RouterDispatcherFactory; |
8 | 9 | use Hypervel\Broadcasting\BroadcastEvent; |
9 | 10 | use Hypervel\Broadcasting\BroadcastManager; |
10 | 11 | use Hypervel\Broadcasting\Channel; |
|
19 | 20 | use Hypervel\Container\DefinitionSource; |
20 | 21 | use Hypervel\Context\ApplicationContext; |
21 | 22 | use Hypervel\Foundation\Application; |
| 23 | +use Hypervel\Foundation\Http\Kernel; |
| 24 | +use Hypervel\Foundation\Http\Middleware\VerifyCsrfToken; |
22 | 25 | use Hypervel\Queue\Contracts\Factory as QueueFactoryContract; |
23 | 26 | use Hypervel\Support\Facades\Broadcast; |
24 | 27 | use Hypervel\Support\Facades\Bus; |
@@ -117,6 +120,66 @@ public function testThrowExceptionWhenUnknownStoreIsUsed() |
117 | 120 |
|
118 | 121 | $broadcastManager->connection('alien_connection'); |
119 | 122 | } |
| 123 | + |
| 124 | + public function testRoutesExcludesCsrfMiddleware(): void |
| 125 | + { |
| 126 | + $capturedAttributes = null; |
| 127 | + |
| 128 | + $router = m::mock('router'); |
| 129 | + $router->shouldReceive('addRoute') |
| 130 | + ->once() |
| 131 | + ->withArgs(function ($methods, $path, $handler, $attributes) use (&$capturedAttributes) { |
| 132 | + $capturedAttributes = $attributes; |
| 133 | + return true; |
| 134 | + }); |
| 135 | + |
| 136 | + $routerFactory = m::mock('routerFactory'); |
| 137 | + $routerFactory->shouldReceive('getRouter') |
| 138 | + ->with('http') |
| 139 | + ->andReturn($router); |
| 140 | + |
| 141 | + $config = m::mock(ConfigInterface::class); |
| 142 | + $config->shouldReceive('get') |
| 143 | + ->with('server.kernels', []) |
| 144 | + ->andReturn(['http' => []]); |
| 145 | + |
| 146 | + $app = m::mock(ContainerInterface::class); |
| 147 | + $app->shouldReceive('has')->with(Kernel::class)->andReturn(true); |
| 148 | + $app->shouldReceive('get')->with(ConfigInterface::class)->andReturn($config); |
| 149 | + $app->shouldReceive('get')->with(RouterDispatcherFactory::class)->andReturn($routerFactory); |
| 150 | + |
| 151 | + $broadcastManager = new BroadcastManager($app); |
| 152 | + $broadcastManager->routes(); |
| 153 | + |
| 154 | + $this->assertSame(['web'], $capturedAttributes['middleware']); |
| 155 | + $this->assertSame([VerifyCsrfToken::class], $capturedAttributes['without_middleware']); |
| 156 | + } |
| 157 | + |
| 158 | + public function testUserRoutesExcludesCsrfMiddleware(): void |
| 159 | + { |
| 160 | + $capturedAttributes = null; |
| 161 | + |
| 162 | + $router = m::mock('router'); |
| 163 | + $router->shouldReceive('addRoute') |
| 164 | + ->once() |
| 165 | + ->withArgs(function ($methods, $path, $handler, $attributes) use (&$capturedAttributes) { |
| 166 | + $capturedAttributes = $attributes; |
| 167 | + return true; |
| 168 | + }); |
| 169 | + |
| 170 | + $routerFactory = m::mock('routerFactory'); |
| 171 | + $routerFactory->shouldReceive('getRouter') |
| 172 | + ->andReturn($router); |
| 173 | + |
| 174 | + $app = m::mock(ContainerInterface::class); |
| 175 | + $app->shouldReceive('get')->with(RouterDispatcherFactory::class)->andReturn($routerFactory); |
| 176 | + |
| 177 | + $broadcastManager = new BroadcastManager($app); |
| 178 | + $broadcastManager->userRoutes(); |
| 179 | + |
| 180 | + $this->assertSame(['web'], $capturedAttributes['middleware']); |
| 181 | + $this->assertSame([VerifyCsrfToken::class], $capturedAttributes['without_middleware']); |
| 182 | + } |
120 | 183 | } |
121 | 184 |
|
122 | 185 | class TestEvent implements ShouldBroadcast |
|
0 commit comments