plexorganizrnginxsonarrradarrdashboardcouchpotatonzbgetbookmarkapplication-dashboardmuximuxlandingpagestartpagelandinghtpcserverhomepagesabnzbdheimdallemby
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
7.1 KiB
183 lines
7.1 KiB
7 years ago
|
<?php
|
||
|
|
||
|
/*
|
||
|
* This file is part of the Symfony package.
|
||
|
*
|
||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||
|
*
|
||
|
* For the full copyright and license information, please view the LICENSE
|
||
|
* file that was distributed with this source code.
|
||
|
*/
|
||
|
|
||
|
namespace Symfony\Component\HttpKernel\Tests;
|
||
|
|
||
|
use PHPUnit\Framework\TestCase;
|
||
|
use Symfony\Component\HttpFoundation\Cookie;
|
||
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||
6 years ago
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||
6 years ago
|
use Symfony\Component\HttpKernel\HttpKernelBrowser;
|
||
7 years ago
|
use Symfony\Component\HttpKernel\Tests\Fixtures\TestClient;
|
||
|
|
||
|
/**
|
||
|
* @group time-sensitive
|
||
|
*/
|
||
6 years ago
|
class HttpKernelBrowserTest extends TestCase
|
||
7 years ago
|
{
|
||
|
public function testDoRequest()
|
||
|
{
|
||
6 years ago
|
$client = new HttpKernelBrowser(new TestHttpKernel());
|
||
7 years ago
|
|
||
|
$client->request('GET', '/');
|
||
|
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
|
||
|
$this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
|
||
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $client->getRequest());
|
||
|
$this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getInternalResponse());
|
||
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $client->getResponse());
|
||
|
|
||
|
$client->request('GET', 'http://www.example.com/');
|
||
|
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
|
||
|
$this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
|
||
|
|
||
|
$client->request('GET', 'http://www.example.com/?parameter=http://google.com');
|
||
|
$this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request');
|
||
|
}
|
||
|
|
||
|
public function testGetScript()
|
||
|
{
|
||
|
$client = new TestClient(new TestHttpKernel());
|
||
|
$client->insulate();
|
||
|
$client->request('GET', '/');
|
||
|
|
||
|
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
|
||
|
}
|
||
|
|
||
|
public function testFilterResponseConvertsCookies()
|
||
|
{
|
||
6 years ago
|
$client = new HttpKernelBrowser(new TestHttpKernel());
|
||
7 years ago
|
|
||
|
$r = new \ReflectionObject($client);
|
||
|
$m = $r->getMethod('filterResponse');
|
||
|
$m->setAccessible(true);
|
||
|
|
||
|
$response = new Response();
|
||
6 years ago
|
$response->headers->setCookie($cookie1 = new Cookie('foo', 'bar', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true, false, null));
|
||
7 years ago
|
$domResponse = $m->invoke($client, $response);
|
||
7 years ago
|
$this->assertSame((string) $cookie1, $domResponse->getHeader('Set-Cookie'));
|
||
7 years ago
|
|
||
|
$response = new Response();
|
||
6 years ago
|
$response->headers->setCookie($cookie1 = new Cookie('foo', 'bar', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true, false, null));
|
||
|
$response->headers->setCookie($cookie2 = new Cookie('foo1', 'bar1', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true, false, null));
|
||
7 years ago
|
$domResponse = $m->invoke($client, $response);
|
||
7 years ago
|
$this->assertSame((string) $cookie1, $domResponse->getHeader('Set-Cookie'));
|
||
6 years ago
|
$this->assertSame([(string) $cookie1, (string) $cookie2], $domResponse->getHeader('Set-Cookie', false));
|
||
7 years ago
|
}
|
||
|
|
||
|
public function testFilterResponseSupportsStreamedResponses()
|
||
|
{
|
||
6 years ago
|
$client = new HttpKernelBrowser(new TestHttpKernel());
|
||
7 years ago
|
|
||
|
$r = new \ReflectionObject($client);
|
||
|
$m = $r->getMethod('filterResponse');
|
||
|
$m->setAccessible(true);
|
||
|
|
||
|
$response = new StreamedResponse(function () {
|
||
|
echo 'foo';
|
||
|
});
|
||
|
|
||
|
$domResponse = $m->invoke($client, $response);
|
||
|
$this->assertEquals('foo', $domResponse->getContent());
|
||
|
}
|
||
|
|
||
|
public function testUploadedFile()
|
||
|
{
|
||
|
$source = tempnam(sys_get_temp_dir(), 'source');
|
||
7 years ago
|
file_put_contents($source, '1');
|
||
7 years ago
|
$target = sys_get_temp_dir().'/sf.moved.file';
|
||
|
@unlink($target);
|
||
|
|
||
|
$kernel = new TestHttpKernel();
|
||
6 years ago
|
$client = new HttpKernelBrowser($kernel);
|
||
7 years ago
|
|
||
6 years ago
|
$files = [
|
||
|
['tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => null, 'error' => UPLOAD_ERR_OK],
|
||
6 years ago
|
new UploadedFile($source, 'original', 'mime/original', UPLOAD_ERR_OK, true),
|
||
6 years ago
|
];
|
||
7 years ago
|
|
||
|
$file = null;
|
||
|
foreach ($files as $file) {
|
||
6 years ago
|
$client->request('POST', '/', [], ['foo' => $file]);
|
||
7 years ago
|
|
||
|
$files = $client->getRequest()->files->all();
|
||
|
|
||
|
$this->assertCount(1, $files);
|
||
|
|
||
|
$file = $files['foo'];
|
||
|
|
||
|
$this->assertEquals('original', $file->getClientOriginalName());
|
||
|
$this->assertEquals('mime/original', $file->getClientMimeType());
|
||
6 years ago
|
$this->assertEquals(1, $file->getSize());
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
$file->move(\dirname($target), basename($target));
|
||
7 years ago
|
|
||
|
$this->assertFileExists($target);
|
||
|
unlink($target);
|
||
|
}
|
||
|
|
||
|
public function testUploadedFileWhenNoFileSelected()
|
||
|
{
|
||
|
$kernel = new TestHttpKernel();
|
||
6 years ago
|
$client = new HttpKernelBrowser($kernel);
|
||
7 years ago
|
|
||
6 years ago
|
$file = ['tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE];
|
||
7 years ago
|
|
||
6 years ago
|
$client->request('POST', '/', [], ['foo' => $file]);
|
||
7 years ago
|
|
||
|
$files = $client->getRequest()->files->all();
|
||
|
|
||
|
$this->assertCount(1, $files);
|
||
|
$this->assertNull($files['foo']);
|
||
|
}
|
||
|
|
||
|
public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
|
||
|
{
|
||
|
$source = tempnam(sys_get_temp_dir(), 'source');
|
||
|
|
||
|
$kernel = new TestHttpKernel();
|
||
6 years ago
|
$client = new HttpKernelBrowser($kernel);
|
||
7 years ago
|
|
||
|
$file = $this
|
||
|
->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
|
||
6 years ago
|
->setConstructorArgs([$source, 'original', 'mime/original', UPLOAD_ERR_OK, true])
|
||
|
->setMethods(['getSize', 'getClientSize'])
|
||
7 years ago
|
->getMock()
|
||
|
;
|
||
6 years ago
|
/* should be modified when the getClientSize will be removed */
|
||
|
$file->expects($this->any())
|
||
7 years ago
|
->method('getSize')
|
||
6 years ago
|
->willReturn(INF)
|
||
7 years ago
|
;
|
||
6 years ago
|
$file->expects($this->any())
|
||
|
->method('getClientSize')
|
||
6 years ago
|
->willReturn(INF)
|
||
6 years ago
|
;
|
||
7 years ago
|
|
||
6 years ago
|
$client->request('POST', '/', [], [$file]);
|
||
7 years ago
|
|
||
|
$files = $client->getRequest()->files->all();
|
||
|
|
||
|
$this->assertCount(1, $files);
|
||
|
|
||
|
$file = $files[0];
|
||
|
|
||
|
$this->assertFalse($file->isValid());
|
||
|
$this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
|
||
|
$this->assertEquals('mime/original', $file->getClientMimeType());
|
||
|
$this->assertEquals('original', $file->getClientOriginalName());
|
||
6 years ago
|
$this->assertEquals(0, $file->getSize());
|
||
7 years ago
|
|
||
|
unlink($source);
|
||
|
}
|
||
|
}
|