Skip to content

Commit

Permalink
Merge pull request #954 from msmakouz/feature/default-auth-transport
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jul 19, 2023
1 parent e79fa4a commit 7ff3cf9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Middleware/AuthTransportMiddleware.php
Expand Up @@ -49,6 +49,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
private function getTransportRegistry(TransportRegistry $registry, string $transportName): TransportRegistry
{
$transports = new TransportRegistry();
$transports->setDefaultTransport($transportName);
$transports->setTransport($transportName, $registry->getTransport($transportName));

return $transports;
Expand Down
24 changes: 24 additions & 0 deletions tests/Middleware/AuthTransportMiddlewareTest.php
Expand Up @@ -4,14 +4,18 @@

namespace Spiral\Tests\Auth\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Spiral\Auth\ActorProviderInterface;
use Spiral\Auth\AuthContextInterface;
use Spiral\Auth\Middleware\AuthTransportMiddleware;
use Spiral\Auth\TokenStorageInterface;
use Spiral\Auth\Transport\CookieTransport;
use Spiral\Auth\Transport\HeaderTransport;
use Spiral\Auth\TransportRegistry;
use Spiral\Core\Container\Autowire;
use Spiral\Core\ScopeInterface;
use Spiral\Testing\Http\Token;
use Spiral\Tests\Auth\BaseTestCase;
use Spiral\Tests\Auth\Stub\TestAuthHttpProvider;
use Spiral\Tests\Auth\Stub\TestAuthHttpStorage;
Expand Down Expand Up @@ -51,6 +55,26 @@ public function testCreateMiddlewareWithOneTransport(): void
$this->assertInstanceOf(HeaderTransport::class, $registry2->getTransport('header'));
}

public function testCloseContextWithAuthContextTransportNull(): void
{
$middleware = new Autowire(AuthTransportMiddleware::class, ['header']);

$auth = $this->getPrivateProperty('authMiddleware', $middleware->resolve($this->container));

$authContext = $this->createMock(AuthContextInterface::class);
$authContext->expects($this->once())->method('getTransport')->willReturn(null);
$authContext->expects($this->once())->method('isClosed')->willReturn(false);
$authContext->expects($this->exactly(3))->method('getToken')->willReturn(new Token('1', []));

$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())->method('withAddedHeader')->willReturn($response);

$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())->method('hasHeader')->willReturn(false);

(new \ReflectionMethod($auth, 'closeContext'))->invoke($auth, $request, $response, $authContext);
}

private function getPrivateProperty(string $property, object $object): mixed
{
$ref = new \ReflectionObject($object);
Expand Down

0 comments on commit 7ff3cf9

Please sign in to comment.