Skip to content

Commit

Permalink
Merge pull request #58 from shopware/fix-shop-is-not-reachable-when-g…
Browse files Browse the repository at this point in the history
…etting-401-unauthorized-response

fix: shop is not reachable when getting 401 unauthorized response
  • Loading branch information
MoritzKrafeld authored Jan 20, 2025
2 parents d26bd4c + 7b06aa6 commit 1d6fbfe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/EventListener/BeforeRegistrationStartsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
use Shopware\AppBundle\Exception\ShopURLIsNotReachableException;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsEventListener]
Expand Down Expand Up @@ -38,6 +39,10 @@ public function __invoke(BeforeRegistrationStartsEvent $event): void
'max_redirects' => 0,
]);
} catch (\Throwable $e) {
if (!$e instanceof TransportExceptionInterface) {
return;
}

throw new ShopURLIsNotReachableException($shop->getShopUrl(), $e);
}
}
Expand Down
52 changes: 51 additions & 1 deletion tests/EventListener/BeforeRegistrationStartsListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
namespace EventListener;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
use Shopware\App\SDK\Shop\ShopInterface;
use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener;
use Shopware\AppBundle\Exception\ShopURLIsNotReachableException;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[CoversClass(BeforeRegistrationStartsListener::class)]
Expand Down Expand Up @@ -95,7 +101,7 @@ public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable():
'timeout' => 10,
'max_redirects' => 0,
])
->willThrowException(new \Exception('Shop url is not reachable'));
->willThrowException(new TransportException('Shop is not reachable'));

$listener = new BeforeRegistrationStartsListener(
$this->httpClient,
Expand All @@ -109,4 +115,48 @@ public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable():
)
);
}

#[DataProvider('unauthorizedHttpExceptionProvider')]
public function testListenerDoesNotThrowExceptionWhenTheExceptionCodeIsHTTPUnauthorized($exception): void
{
$shop = $this->createMock(ShopInterface::class);
$shop
->expects(self::once())
->method('getShopUrl')
->willReturn('https://shop-url.com');

$this->httpClient
->expects(self::once())
->method('request')
->with('HEAD', 'https://shop-url.com/api/_info/config', [
'timeout' => 10,
'max_redirects' => 0,
])
->willThrowException($exception);

$listener = new BeforeRegistrationStartsListener(
$this->httpClient,
true
);

$listener->__invoke(
new BeforeRegistrationStartsEvent(
$this->createMock(RequestInterface::class),
$shop
)
);
}

public static function unauthorizedHttpExceptionProvider(): \Generator
{
yield 'HttpException' => [
new UnauthorizedHttpException('Unauthorized')
];

yield 'HttpExceptionInterface' => [
new ClientException(new MockResponse('', [
'http_code' => Response::HTTP_UNAUTHORIZED,
]))
];
}
}

0 comments on commit 1d6fbfe

Please sign in to comment.