Keep configured auth transports when faking an actor (#91)

Assisted-By: Claude Fable 5.1
Co-authored-by: roxblnfk <roxblnfk@ya.ru>
This commit is contained in:
dualfrozandroxblnfk committed 2026-09-21 18:30:28 +04:00
1 parent fdcc6b7728
commit 0171b86dd8
4 files changed
+176 -3

No files matched your search

+28 -3
View File
@@ -7,6 +7,7 @@ namespace Spiral\Testing\Http;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Stream;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
@@ -421,9 +422,7 @@ class FakeHttp
$bindings[ActorProviderInterface::class] = new FakeActorProvider($this->actor);
$bindings[TokenStorageInterface::class] = new FakeTokenStorage();
$transport = new TransportRegistry();
$transport->setTransport('testing', new HeaderTransport(static::AUTH_TOKEN_HEADER_KEY));
$bindings[TransportRegistry::class] = $transport;
$bindings[TransportRegistry::class] = $this->createFakeTransportRegistry();
}
if ($this->session) {
@@ -461,4 +460,30 @@ class FakeHttp
throw new \InvalidArgumentException('$data should be an array or an object.');
}
}
/**
* The fake token travels in the {@see self::AUTH_TOKEN_HEADER_KEY} header, so every transport the
* application has configured is replaced by one that reads it. The configured names are kept because
* {@see \Spiral\Auth\Middleware\AuthTransportMiddleware} resolves a transport by name.
*/
private function createFakeTransportRegistry(): TransportRegistry
{
$registry = new TransportRegistry();
$transport = new HeaderTransport(static::AUTH_TOKEN_HEADER_KEY);
try {
/** @var TransportRegistry $configured */
$configured = $this->container->get(TransportRegistry::class);
foreach (\array_keys($configured->getTransports()) as $name) {
$registry->setTransport((string) $name, $transport);
}
} catch (NotFoundExceptionInterface) {
// Nothing to copy the names from; the fake transport is all there is.
}
$registry->setTransport('testing', $transport);
$registry->setDefaultTransport('testing');
return $registry;
}
}