Issue #3202145 by kuldeep_mehra27, phenaproxima, bkosborne, Chris Burge: oEmbed resource fetcher needs to set a reasonable connection timeout
(cherry picked from commit b8abdeb307
)
merge-requests/1102/head
parent
0cf50b1574
commit
e6f76d6bd0
|
@ -7,6 +7,7 @@ use Drupal\Core\Cache\CacheBackendInterface;
|
||||||
use Drupal\Core\Cache\UseCacheBackendTrait;
|
use Drupal\Core\Cache\UseCacheBackendTrait;
|
||||||
use GuzzleHttp\ClientInterface;
|
use GuzzleHttp\ClientInterface;
|
||||||
use GuzzleHttp\Exception\TransferException;
|
use GuzzleHttp\Exception\TransferException;
|
||||||
|
use GuzzleHttp\RequestOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches and caches oEmbed resources.
|
* Fetches and caches oEmbed resources.
|
||||||
|
@ -58,7 +59,9 @@ class ResourceFetcher implements ResourceFetcherInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$response = $this->httpClient->get($url);
|
$response = $this->httpClient->request('GET', $url, [
|
||||||
|
RequestOptions::TIMEOUT => 5,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
catch (TransferException $e) {
|
catch (TransferException $e) {
|
||||||
throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
|
throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
|
||||||
|
|
|
@ -10,6 +10,7 @@ use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Handler\MockHandler;
|
use GuzzleHttp\Handler\MockHandler;
|
||||||
use GuzzleHttp\HandlerStack;
|
use GuzzleHttp\HandlerStack;
|
||||||
use GuzzleHttp\Psr7\Response;
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use GuzzleHttp\RequestOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group media
|
* @group media
|
||||||
|
@ -18,6 +19,34 @@ use GuzzleHttp\Psr7\Response;
|
||||||
*/
|
*/
|
||||||
class ResourceFetcherTest extends UnitTestCase {
|
class ResourceFetcherTest extends UnitTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that resources are fetched with a hard-coded timeout.
|
||||||
|
*/
|
||||||
|
public function testFetchTimeout(): void {
|
||||||
|
$url = 'https://example.com/oembed?url=resource';
|
||||||
|
$headers = [
|
||||||
|
'Content-Type' => ['text/javascript'],
|
||||||
|
];
|
||||||
|
$body = Json::encode([
|
||||||
|
'version' => '1.0',
|
||||||
|
'type' => 'video',
|
||||||
|
'html' => 'test',
|
||||||
|
]);
|
||||||
|
$response = new Response(200, $headers, $body);
|
||||||
|
|
||||||
|
$client = $this->prophesize(Client::class);
|
||||||
|
$client->request('GET', $url, [RequestOptions::TIMEOUT => 5])
|
||||||
|
->shouldBeCalled()
|
||||||
|
->willReturn($response);
|
||||||
|
|
||||||
|
$fetcher = new ResourceFetcher(
|
||||||
|
$client->reveal(),
|
||||||
|
$this->createMock('\Drupal\media\OEmbed\ProviderRepositoryInterface'),
|
||||||
|
new NullBackend('default')
|
||||||
|
);
|
||||||
|
$fetcher->fetchResource($url);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests how the resource fetcher handles unknown Content-Type headers.
|
* Tests how the resource fetcher handles unknown Content-Type headers.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue