|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSRestBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\RestBundle\Tests\Functional; |
| 13 | + |
| 14 | +use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; |
| 15 | + |
| 16 | +class AccessDeniedListenerTest extends WebTestCase |
| 17 | +{ |
| 18 | + private static $client; |
| 19 | + |
| 20 | + public static function setUpBeforeClass() |
| 21 | + { |
| 22 | + parent::setUpBeforeClass(); |
| 23 | + static::$client = static::createClient(['test_case' => 'AccessDeniedListener']); |
| 24 | + } |
| 25 | + |
| 26 | + public static function tearDownAfterClass() |
| 27 | + { |
| 28 | + self::deleteTmpDir('AccessDeniedListener'); |
| 29 | + parent::tearDownAfterClass(); |
| 30 | + } |
| 31 | + |
| 32 | + protected function setUp() |
| 33 | + { |
| 34 | + if (!interface_exists(ErrorRendererInterface::class)) { |
| 35 | + $this->markTestSkipped(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public function testBundleListenerHandlesExceptionsInRestZonesWithoutLogin() |
| 40 | + { |
| 41 | + static::$client->request('GET', '/api/comments'); |
| 42 | + |
| 43 | + $this->assertEquals(401, static::$client->getResponse()->getStatusCode()); |
| 44 | + $this->assertEquals('application/json', static::$client->getResponse()->headers->get('Content-Type')); |
| 45 | + } |
| 46 | + |
| 47 | + public function testBundleListenerHandlesExceptionsInRestZonesWithLogin() |
| 48 | + { |
| 49 | + $credentials = [ |
| 50 | + 'PHP_AUTH_USER' => 'restapi', |
| 51 | + 'PHP_AUTH_PW' => 'secretpw', |
| 52 | + ]; |
| 53 | + |
| 54 | + static::$client->request('GET', '/api/comments', [], [], $credentials); |
| 55 | + |
| 56 | + $this->assertEquals(200, static::$client->getResponse()->getStatusCode()); |
| 57 | + $this->assertEquals('application/json', static::$client->getResponse()->headers->get('Content-Type')); |
| 58 | + } |
| 59 | + |
| 60 | + public function testBundleListenerHandlesExceptionsInRestZonesWrongLogin() |
| 61 | + { |
| 62 | + $credentials = [ |
| 63 | + 'PHP_AUTH_USER' => 'admin', |
| 64 | + 'PHP_AUTH_PW' => 'secretpw', |
| 65 | + ]; |
| 66 | + |
| 67 | + static::$client->request('GET', '/api/comments', [], [], $credentials); |
| 68 | + |
| 69 | + $this->assertEquals(403, static::$client->getResponse()->getStatusCode()); |
| 70 | + $this->assertEquals('application/json', static::$client->getResponse()->headers->get('Content-Type')); |
| 71 | + } |
| 72 | + |
| 73 | + public function testBundleListenerHandlesExceptionsInRestZonesWithIncorrectLogin() |
| 74 | + { |
| 75 | + $credentials = [ |
| 76 | + 'PHP_AUTH_USER' => 'restapi', |
| 77 | + 'PHP_AUTH_PW' => 'foobar', |
| 78 | + ]; |
| 79 | + |
| 80 | + static::$client->request('GET', '/api/comments', [], [], $credentials); |
| 81 | + |
| 82 | + $this->assertEquals(401, static::$client->getResponse()->getStatusCode()); |
| 83 | + $this->assertEquals('application/json', static::$client->getResponse()->headers->get('Content-Type')); |
| 84 | + } |
| 85 | + |
| 86 | + public function testSymfonyListenerHandlesExceptionsOutsideRestZones() |
| 87 | + { |
| 88 | + static::$client->request('GET', '/admin/comments'); |
| 89 | + |
| 90 | + $this->assertEquals(302, static::$client->getResponse()->getStatusCode()); |
| 91 | + $this->assertEquals('text/html; charset=UTF-8', static::$client->getResponse()->headers->get('Content-Type')); |
| 92 | + } |
| 93 | +} |
0 commit comments