View source
<?php
namespace Drupal\Tests\remote_stream_wrapper\Kernel;
use Drupal\KernelTests\KernelTestBase;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
class HttpMimeTypeGuesserTest extends KernelTestBase {
protected $guesser;
protected $httpGuesser;
public static $modules = [
'remote_stream_wrapper',
];
public function setUp() {
parent::setUp();
$this->guesser = $this->container
->get('file.mime_type.guesser');
$this->httpGuesser = $this->container
->get('file.mime_type.guesser.http');
}
public function testParseFileNameFromUrl($url, $expected_result) {
$this
->assertEquals($expected_result, $this->httpGuesser
->parseFileNameFromUrl($url));
}
public function testHttpMimeTypeGuessing($url, $expected_result, $head_response = NULL, $get_response = NULL) {
$client = $this
->prepareClient($url, $head_response, $get_response);
$this->httpGuesser
->setHttpClient($client);
$this
->assertEquals($expected_result, $this->guesser
->guess($url));
}
public function dataParseFileNameFromUrl() {
return [
[
'http://www.example.com/file.txt',
'file.txt',
],
[
'http://www.example.com/test/file.txt.pdf?extension=.gif',
'file.txt.pdf',
],
[
'http://www.example.com/FILE.PDF#foo',
'FILE.PDF',
],
[
'http://www.example.com/',
FALSE,
],
[
'http://www.example.com/test',
FALSE,
],
[
'//www.example.com/test.unknown',
'test.unknown',
],
];
}
public function dataHttpMimetypeGuessing() {
$data = [];
$data[] = [
'http://www.drupal.org/file.txt',
'text/plain',
];
$data[] = [
'http://www.drupal.org/test/file.txt?extension=.gif',
'text/plain',
];
$data[] = [
'https://www.drupal.org/FILE.PDF#foo',
'application/pdf',
];
$data[] = [
'http://www.drupal.org/',
'html/get',
new ClientException(405, new Request('HEAD', ''), new Response(405)),
new Response(200, [
'Content-Type' => 'html/get',
]),
];
$data[] = [
'http://www.drupal.org/test',
'html/get',
new Response(200),
new Response(200, [
'Content-Type' => 'html/get',
]),
];
$data[] = [
'//www.drupal.org/test.unknown',
'html/head',
new Response(200, [
'Content-Type' => 'html/head',
]),
new Response(200, [
'Content-Type' => 'html/get',
]),
];
$data[] = [
'https://www.drupal.org/',
'application/octet-stream',
new ClientException(404, new Request('HEAD', ''), new Response(404)),
new ClientException(404, new Request('GET', ''), new Response(404)),
];
$data[] = [
'public://test.unknown',
'application/octet-stream',
new Response(200, [
'Content-Type' => 'html/head',
]),
];
$data[] = [
'core/CHANGELOG.txt',
'text/plain',
new Response(200, [
'Content-Type' => 'html/head',
]),
];
return $data;
}
protected function prepareClient($url, $head_response, $get_response) {
$client = $this
->prophesize('\\GuzzleHttp\\Client');
if ($head_response instanceof Response) {
$client
->request('HEAD', $url, [])
->willReturn($head_response);
}
elseif ($head_response instanceof \Exception) {
$client
->request('HEAD', $url, [])
->willThrow($head_response);
}
if ($get_response instanceof Response) {
$client
->request('GET', $url, [])
->willReturn($get_response);
}
elseif ($get_response instanceof \Exception) {
$client
->request('GET', $url, [])
->willThrow($get_response);
}
return $client
->reveal();
}
}