|
16 | 16 | use ApiPlatform\Core\HttpCache\VarnishPurger;
|
17 | 17 | use ApiPlatform\Core\Tests\ProphecyTrait;
|
18 | 18 | use GuzzleHttp\ClientInterface;
|
| 19 | +use GuzzleHttp\Promise\PromiseInterface; |
19 | 20 | use GuzzleHttp\Psr7\Response;
|
| 21 | +use LogicException; |
20 | 22 | use PHPUnit\Framework\TestCase;
|
| 23 | +use Psr\Http\Message\RequestInterface; |
| 24 | +use Psr\Http\Message\ResponseInterface; |
21 | 25 |
|
22 | 26 | /**
|
23 | 27 | * @author Kévin Dunglas <dunglas@gmail.com>
|
@@ -49,4 +53,101 @@ public function testEmptyTags()
|
49 | 53 | $purger = new VarnishPurger([$clientProphecy1->reveal()]);
|
50 | 54 | $purger->purge([]);
|
51 | 55 | }
|
| 56 | + |
| 57 | + /** |
| 58 | + * @dataProvider provideChunkHeaderCases |
| 59 | + */ |
| 60 | + public function testItChunksHeaderToAvoidHittingVarnishLimit(int $maxHeaderLength, array $iris, array $regexesToSend) |
| 61 | + { |
| 62 | + $client = new class() implements ClientInterface { |
| 63 | + public $sentRegexes = []; |
| 64 | + |
| 65 | + public function send(RequestInterface $request, array $options = []): ResponseInterface |
| 66 | + { |
| 67 | + throw new LogicException('Not implemented'); |
| 68 | + } |
| 69 | + |
| 70 | + public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface |
| 71 | + { |
| 72 | + throw new LogicException('Not implemented'); |
| 73 | + } |
| 74 | + |
| 75 | + public function request($method, $uri, array $options = []): ResponseInterface |
| 76 | + { |
| 77 | + $this->sentRegexes[] = $options['headers']['ApiPlatform-Ban-Regex']; |
| 78 | + |
| 79 | + return new Response(); |
| 80 | + } |
| 81 | + |
| 82 | + public function requestAsync($method, $uri, array $options = []): PromiseInterface |
| 83 | + { |
| 84 | + throw new LogicException('Not implemented'); |
| 85 | + } |
| 86 | + |
| 87 | + public function getConfig($option = null) |
| 88 | + { |
| 89 | + throw new LogicException('Not implemented'); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + $purger = new VarnishPurger([$client], $maxHeaderLength); |
| 94 | + $purger->purge($iris); |
| 95 | + |
| 96 | + self::assertSame($regexesToSend, $client->sentRegexes); |
| 97 | + } |
| 98 | + |
| 99 | + public function provideChunkHeaderCases() |
| 100 | + { |
| 101 | + yield 'few iris' => [ |
| 102 | + 50, |
| 103 | + ['/foo', '/bar'], |
| 104 | + ['((^|\,)/foo($|\,))|((^|\,)/bar($|\,))'], |
| 105 | + ]; |
| 106 | + |
| 107 | + yield 'iris to generate a header with exactly the maximum length' => [ |
| 108 | + 56, |
| 109 | + ['/foo', '/bar', '/baz'], |
| 110 | + ['((^|\,)/foo($|\,))|((^|\,)/bar($|\,))|((^|\,)/baz($|\,))'], |
| 111 | + ]; |
| 112 | + |
| 113 | + yield 'iris to generate a header with exactly the maximum length and a smaller one' => [ |
| 114 | + 37, |
| 115 | + ['/foo', '/bar', '/baz'], |
| 116 | + [ |
| 117 | + '((^|\,)/foo($|\,))|((^|\,)/bar($|\,))', |
| 118 | + '(^|\,)/baz($|\,)', |
| 119 | + ], |
| 120 | + ]; |
| 121 | + |
| 122 | + yield 'with last iri too long to be part of the same header' => [ |
| 123 | + 50, |
| 124 | + ['/foo', '/bar', '/some-longer-tag'], |
| 125 | + [ |
| 126 | + '((^|\,)/foo($|\,))|((^|\,)/bar($|\,))', |
| 127 | + '(^|\,)/some\-longer\-tag($|\,)', |
| 128 | + ], |
| 129 | + ]; |
| 130 | + |
| 131 | + yield 'iris to have five headers' => [ |
| 132 | + 50, |
| 133 | + ['/foo/1', '/foo/2', '/foo/3', '/foo/4', '/foo/5', '/foo/6', '/foo/7', '/foo/8', '/foo/9', '/foo/10'], |
| 134 | + [ |
| 135 | + '((^|\,)/foo/1($|\,))|((^|\,)/foo/2($|\,))', |
| 136 | + '((^|\,)/foo/3($|\,))|((^|\,)/foo/4($|\,))', |
| 137 | + '((^|\,)/foo/5($|\,))|((^|\,)/foo/6($|\,))', |
| 138 | + '((^|\,)/foo/7($|\,))|((^|\,)/foo/8($|\,))', |
| 139 | + '((^|\,)/foo/9($|\,))|((^|\,)/foo/10($|\,))', |
| 140 | + ], |
| 141 | + ]; |
| 142 | + |
| 143 | + yield 'with varnish default limit' => [ |
| 144 | + 8000, |
| 145 | + array_fill(0, 1000, '/foo'), |
| 146 | + [ |
| 147 | + implode('|', array_fill(0, 421, '((^|\,)/foo($|\,))')), |
| 148 | + implode('|', array_fill(0, 421, '((^|\,)/foo($|\,))')), |
| 149 | + implode('|', array_fill(0, 158, '((^|\,)/foo($|\,))')), |
| 150 | + ], |
| 151 | + ]; |
| 152 | + } |
52 | 153 | }
|
0 commit comments