|
| 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\WebLink\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\WebLink\HttpHeaderParser; |
| 17 | + |
| 18 | +class HttpHeaderParserTest extends TestCase |
| 19 | +{ |
| 20 | + public function testParse() |
| 21 | + { |
| 22 | + $parser = new HttpHeaderParser(); |
| 23 | + |
| 24 | + $header = [ |
| 25 | + '</1>; rel="prerender",</2>; rel="dns-prefetch"; pr="0.7",</3>; rel="preload"; as="script"', |
| 26 | + '</4>; rel="preload"; as="image"; nopush,</5>; rel="alternate next"; hreflang="fr"; hreflang="de"; title="Hello"' |
| 27 | + ]; |
| 28 | + $provider = $parser->parse($header); |
| 29 | + $links = $provider->getLinks(); |
| 30 | + |
| 31 | + self::assertCount(5, $links); |
| 32 | + |
| 33 | + self::assertSame(['prerender'], $links[0]->getRels()); |
| 34 | + self::assertSame('/1', $links[0]->getHref()); |
| 35 | + self::assertSame([], $links[0]->getAttributes()); |
| 36 | + |
| 37 | + self::assertSame(['dns-prefetch'], $links[1]->getRels()); |
| 38 | + self::assertSame('/2', $links[1]->getHref()); |
| 39 | + self::assertSame(['pr' => '0.7'], $links[1]->getAttributes()); |
| 40 | + |
| 41 | + self::assertSame(['preload'], $links[2]->getRels()); |
| 42 | + self::assertSame('/3', $links[2]->getHref()); |
| 43 | + self::assertSame(['as' => 'script'], $links[2]->getAttributes()); |
| 44 | + |
| 45 | + self::assertSame(['preload'], $links[3]->getRels()); |
| 46 | + self::assertSame('/4', $links[3]->getHref()); |
| 47 | + self::assertSame(['as' => 'image', 'nopush' => true], $links[3]->getAttributes()); |
| 48 | + |
| 49 | + self::assertSame(['alternate', 'next'], $links[4]->getRels()); |
| 50 | + self::assertSame('/5', $links[4]->getHref()); |
| 51 | + self::assertSame(['hreflang' => ['fr', 'de'], 'title' => 'Hello'], $links[4]->getAttributes()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testParseEmpty() |
| 55 | + { |
| 56 | + $parser = new HttpHeaderParser(); |
| 57 | + $provider = $parser->parse(''); |
| 58 | + self::assertCount(0, $provider->getLinks()); |
| 59 | + } |
| 60 | + |
| 61 | + /** @dataProvider provideHeaderParsingCases */ |
| 62 | + #[DataProvider('provideHeaderParsingCases')] |
| 63 | + public function testParseVariousAttributes(string $header, array $expectedRels, array $expectedAttributes) |
| 64 | + { |
| 65 | + $parser = new HttpHeaderParser(); |
| 66 | + $links = $parser->parse($header)->getLinks(); |
| 67 | + |
| 68 | + self::assertCount(1, $links); |
| 69 | + self::assertSame('/foo', $links[0]->getHref()); |
| 70 | + self::assertSame($expectedRels, $links[0]->getRels()); |
| 71 | + self::assertSame($expectedAttributes, $links[0]->getAttributes()); |
| 72 | + } |
| 73 | + |
| 74 | + public static function provideHeaderParsingCases() |
| 75 | + { |
| 76 | + yield 'double_quotes_in_attribute_value' => [ |
| 77 | + '</foo>; rel="alternate"; title="\"escape me\" \"already escaped\" \"\"\""', |
| 78 | + ['alternate'], |
| 79 | + ['title' => '"escape me" "already escaped" """'], |
| 80 | + ]; |
| 81 | + |
| 82 | + yield 'unquoted_attribute_value' => [ |
| 83 | + '</foo>; rel=alternate; type=text/html', |
| 84 | + ['alternate'], |
| 85 | + ['type' => 'text/html'], |
| 86 | + ]; |
| 87 | + |
| 88 | + yield 'attribute_with_punctuation' => [ |
| 89 | + '</foo>; rel="alternate"; title=">; hello, world; test:case"', |
| 90 | + ['alternate'], |
| 91 | + ['title' => '>; hello, world; test:case'], |
| 92 | + ]; |
| 93 | + |
| 94 | + yield 'no_rel' => [ |
| 95 | + '</foo>; type=text/html', |
| 96 | + [], |
| 97 | + ['type' => 'text/html'], |
| 98 | + ]; |
| 99 | + |
| 100 | + yield 'empty_rel' => [ |
| 101 | + '</foo>; rel', |
| 102 | + [], |
| 103 | + [], |
| 104 | + ]; |
| 105 | + |
| 106 | + yield 'multiple_rel_attributes_get_first' => [ |
| 107 | + '</foo>; rel="alternate" rel="next"', |
| 108 | + ['alternate'], |
| 109 | + [], |
| 110 | + ]; |
| 111 | + } |
| 112 | +} |
0 commit comments