|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\HttpFoundation\Tests\Test\Constraint; |
| 13 | + |
| 14 | +use PHPUnit\Framework\ExpectationFailedException; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpFoundation\Response; |
| 18 | +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderLocationSame; |
| 19 | + |
| 20 | +class ResponseHeaderLocationSameTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @dataProvider provideSuccessCases |
| 24 | + */ |
| 25 | + public function testConstraintSuccess(string $requestUrl, ?string $location, string $expectedLocation) |
| 26 | + { |
| 27 | + $request = Request::create($requestUrl); |
| 28 | + |
| 29 | + $response = new Response(); |
| 30 | + if (null !== $location) { |
| 31 | + $response->headers->set('Location', $location); |
| 32 | + } |
| 33 | + |
| 34 | + $constraint = new ResponseHeaderLocationSame($request, $expectedLocation); |
| 35 | + |
| 36 | + self::assertTrue($constraint->evaluate($response, '', true)); |
| 37 | + } |
| 38 | + |
| <
F438
code>39 | + public function provideSuccessCases(): iterable |
| 40 | + { |
| 41 | + yield ['http://example.com', 'http://example.com', 'http://example.com']; |
| 42 | + yield ['http://example.com', 'http://example.com', '//example.com']; |
| 43 | + yield ['http://example.com', 'http://example.com', '/']; |
| 44 | + yield ['http://example.com', '//example.com', 'http://example.com']; |
| 45 | + yield ['http://example.com', '//example.com', '//example.com']; |
| 46 | + yield ['http://example.com', '//example.com', '/']; |
| 47 | + yield ['http://example.com', '/', 'http://example.com']; |
| 48 | + yield ['http://example.com', '/', '//example.com']; |
| 49 | + yield ['http://example.com', '/', '/']; |
| 50 | + |
| 51 | + yield ['http://example.com/', 'http://example.com/', 'http://example.com/']; |
| 52 | + yield ['http://example.com/', 'http://example.com/', '//example.com/']; |
| 53 | + yield ['http://example.com/', 'http://example.com/', '/']; |
| 54 | + yield ['http://example.com/', '//example.com/', 'http://example.com/']; |
| 55 | + yield ['http://example.com/', '//example.com/', '//example.com/']; |
| 56 | + yield ['http://example.com/', '//example.com/', '/']; |
| 57 | + yield ['http://example.com/', '/', 'http://example.com/']; |
| 58 | + yield ['http://example.com/', '/', '//example.com/']; |
| 59 | + yield ['http://example.com/', '/', '/']; |
| 60 | + |
| 61 | + yield ['http://example.com/foo', 'http://example.com/', 'http://example.com/']; |
| 62 | + yield ['http://example.com/foo', 'http://example.com/', '//example.com/']; |
| 63 | + yield ['http://example.com/foo', 'http://example.com/', '/']; |
| 64 | + yield ['http://example.com/foo', '//example.com/', 'http://example.com/']; |
| 65 | + yield ['http://example.com/foo', '//example.com/', '//example.com/']; |
| 66 | + yield ['http://example.com/foo', '//example.com/', '/']; |
| 67 | + yield ['http://example.com/foo', '/', 'http://example.com/']; |
| 68 | + yield ['http://example.com/foo', '/', '//example.com/']; |
| 69 | + yield ['http://example.com/foo', '/', '/']; |
| 70 | + |
| 71 | + yield ['http://example.com/foo', 'http://example.com/bar', 'http://example.com/bar']; |
| 72 | + yield ['http://example.com/foo', 'http://example.com/bar', '//example.com/bar']; |
| 73 | + yield ['http://example.com/foo', 'http://example.com/bar', '/bar']; |
| 74 | + yield ['http://example.com/foo', '//example.com/bar', 'http://example.com/bar']; |
| 75 | + yield ['http://example.com/foo', '//example.com/bar', '//example.com/bar']; |
| 76 | + yield ['http://example.com/foo', '//example.com/bar', '/bar']; |
| 77 | + yield ['http://example.com/foo', '/bar', 'http://example.com/bar']; |
| 78 | + yield ['http://example.com/foo', '/bar', '//example.com/bar']; |
| 79 | + yield ['http://example.com/foo', '/bar', '/bar']; |
| 80 | + |
| 81 | + yield ['http://example.com', 'http://example.com/bar', 'http://example.com/bar']; |
| 82 | + yield ['http://example.com', 'http://example.com/bar', '//example.com/bar']; |
| 83 | + yield ['http://example.com', 'http://example.com/bar', '/bar']; |
| 84 | + yield ['http://example.com', '//example.com/bar', 'http://example.com/bar']; |
| 85 | + yield ['http://example.com', '//example.com/bar', '//example.com/bar']; |
| 86 | + yield ['http://example.com', '//example.com/bar', '/bar']; |
| 87 | + yield ['http://example.com', '/bar', 'http://example.com/bar']; |
| 88 | + yield ['http://example.com', '/bar', '//example.com/bar']; |
| 89 | + yield ['http://example.com', '/bar', '/bar']; |
| 90 | + |
| 91 | + yield ['http://example.com/', 'http://another-example.com', 'http://another-example.com']; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @dataProvider provideFailureCases |
| 96 | + */ |
| 97 | + public function testConstraintFailure(string $requestUrl, ?string $location, string $expectedLocation) |
| 98 | + { |
| 99 | + $request = Request::create($requestUrl); |
| 100 | + |
| 101 | + $response = new Response(); |
| 102 | + if (null !== $location) { |
| 103 | + $response->headers->set('Location', $location); |
| 104 | + } |
| 105 | + |
| 106 | + $constraint = new ResponseHeaderLocationSame($request, $expectedLocation); |
| 107 | + |
| 108 | + self::assertFalse($constraint->evaluate($response, '', true)); |
| 109 | + |
| 110 | + $this->expectException(ExpectationFailedException::class); |
| 111 | + |
| 112 | + $constraint->evaluate($response); |
| 113 | + } |
| 114 | + |
| 115 | + public function provideFailureCases(): iterable |
| 116 | + { |
| 117 | + yield ['http://example.com', null, 'http://example.com']; |
| 118 | + yield ['http://example.com', null, '//example.com']; |
| 119 | + yield ['http://example.com', null, '/']; |
| 120 | + |
| 121 | + yield ['http://example.com', 'http://another-example.com', 'http://example.com']; |
| 122 | + yield ['http://example.com', 'http://another-example.com', '//example.com']; |
| 123 | + yield ['http://example.com', 'http://another-example.com', '/']; |
| 124 | + |
| 125 | + yield ['http://example.com', 'http://example.com/bar', 'http://example.com']; |
| 126 | + yield ['http://example.com', 'http://example.com/bar', '//example.com']; |
| 127 | + yield ['http://example.com', 'http://example.com/bar', '/']; |
| 128 | + |
| 129 | + yield ['http://example.com/foo', 'http://example.com/bar', 'http://example.com']; |
| 130 | + yield ['http://example.com/foo', 'http://example.com/bar', '//example.com']; |
| 131 | + yield ['http://example.com/foo', 'http://example.com/bar', '/']; |
| 132 | + |
| 133 | + yield ['http://example.com/foo', 'http://example.com/bar', 'http://example.com/foo']; |
| 134 | + yield ['http://example.com/foo', 'http://example.com/bar', '//example.com/foo']; |
| 135 | + yield ['http://example.com/foo', 'http://example.com/bar', '/foo']; |
| 136 | + } |
| 137 | +} |
0 commit comments