Skip to content

Commit 1d6fbfe

Browse files
Merge pull request #58 from shopware/fix-shop-is-not-reachable-when-getting-401-unauthorized-response
fix: shop is not reachable when getting 401 unauthorized response
2 parents d26bd4c + 7b06aa6 commit 1d6fbfe

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/EventListener/BeforeRegistrationStartsListener.php

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
88
use Shopware\AppBundle\Exception\ShopURLIsNotReachableException;
99
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
10+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
1011
use Symfony\Contracts\HttpClient\HttpClientInterface;
1112

1213
#[AsEventListener]
@@ -38,6 +39,10 @@ public function __invoke(BeforeRegistrationStartsEvent $event): void
3839
'max_redirects' => 0,
3940
]);
4041
} catch (\Throwable $e) {
42+
if (!$e instanceof TransportExceptionInterface) {
43+
return;
44+
}
45+
4146
throw new ShopURLIsNotReachableException($shop->getShopUrl(), $e);
4247
}
4348
}

tests/EventListener/BeforeRegistrationStartsListenerTest.php

+51-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
namespace EventListener;
66

77
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use PHPUnit\Framework\MockObject\MockObject;
910
use PHPUnit\Framework\TestCase;
1011
use Psr\Http\Message\RequestInterface;
1112
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
1213
use Shopware\App\SDK\Shop\ShopInterface;
1314
use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener;
1415
use Shopware\AppBundle\Exception\ShopURLIsNotReachableException;
16+
use Symfony\Component\HttpClient\Exception\ClientException;
17+
use Symfony\Component\HttpClient\Exception\TransportException;
18+
use Symfony\Component\HttpClient\Response\MockResponse;
19+
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
1521
use Symfony\Contracts\HttpClient\HttpClientInterface;
1622

1723
#[CoversClass(BeforeRegistrationStartsListener::class)]
@@ -95,7 +101,7 @@ public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable():
95101
'timeout' => 10,
96102
'max_redirects' => 0,
97103
])
98-
->willThrowException(new \Exception('Shop url is not reachable'));
104+
->willThrowException(new TransportException('Shop is not reachable'));
99105

100106
$listener = new BeforeRegistrationStartsListener(
101107
$this->httpClient,
@@ -109,4 +115,48 @@ public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable():
109115
)
110116
);
111117
}
118+
119+
#[DataProvider('unauthorizedHttpExceptionProvider')]
120+
public function testListenerDoesNotThrowExceptionWhenTheExceptionCodeIsHTTPUnauthorized($exception): void
121+
{
122+
$shop = $this->createMock(ShopInterface::class);
123+
$shop
124+
->expects(self::once())
125+
->method('getShopUrl')
126+
->willReturn('https://shop-url.com');
127+
128+
$this->httpClient
129+
->expects(self::once())
130+
->method('request')
131+
->with('HEAD', 'https://shop-url.com/api/_info/config', [
132+
'timeout' => 10,
133+
'max_redirects' => 0,
134+
])
135+
->willThrowException($exception);
136+
137+
$listener = new BeforeRegistrationStartsListener(
138+
$this->httpClient,
139+
true
140+
);
141+
142+
$listener->__invoke(
143+
new BeforeRegistrationStartsEvent(
144+
$this->createMock(RequestInterface::class),
145+
$shop
146+
)
147+
);
148+
}
149+
150+
public static function unauthorizedHttpExceptionProvider(): \Generator
151+
{
152+
yield 'HttpException' => [
153+
new UnauthorizedHttpException('Unauthorized')
154+
];
155+
156+
yield 'HttpExceptionInterface' => [
157+
new ClientException(new MockResponse('', [
158+
'http_code' => Response::HTTP_UNAUTHORIZED,
159+
]))
160+
];
161+
}
112162
}

0 commit comments

Comments
 (0)