|
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 <[email protected]>
|
@@ -60,4 +64,101 @@ public function testEmptyTags()
|
60 | 64 | $purger = new VarnishPurger([$clientProphecy1->reveal()]);
|
61 | 65 | $purger->purge([]);
|
62 | 66 | }
|
| 67 | + |
| 68 | + /** |
| 69 | + * @dataProvider provideChunkHeaderCases |
| 70 | + */ |
| 71 | + public function testItChunksHeaderToAvoidHittingVarnishLimit(int $maxHeaderLength, array $iris, array $regexesToSend) |
| 72 | + { |
| 73 | + $client = new class() implements ClientInterface { |
| 74 | + public $sentRegexes = []; |
| 75 | + |
| 76 | + public function send(RequestInterface $request, array $options = []): ResponseInterface |
| 77 | + { |
| 78 | + throw new LogicException('Not implemented'); |
| 79 | + } |
| 80 | + |
| 81 | + public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface |
| 82 | + { |
| 83 | + throw new LogicException('Not implemented'); |
| 84 | + } |
| 85 | + |
| 86 | + public function request($method, $uri, array $options = []): ResponseInterface |
| 87 | + { |
| 88 | + $this->sentRegexes[] = $options['headers']['ApiPlatform-Ban-Regex']; |
| 89 | + |
| 90 | + return new Response(); |
| 91 | + } |
| 92 | + |
| 93 | + public function requestAsync($method, $uri, array $options = []): PromiseInterface |
| 94 | + { |
| 95 | + throw new LogicException('Not implemented'); |
| 96 | + } |
| 97 | + |
| 98 | + public function getConfig($option = null) |
| 99 | + { |
| 100 | + throw new LogicException('Not implemented'); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + $purger = new VarnishPurger([$client], $maxHeaderLength); |
| 105 | + $purger->purge($iris); |
| 106 | + |
| 107 | + self::assertSame($regexesToSend, $client->sentRegexes); |
| 108 | + } |
| 109 | + |
| 110 | + public function provideChunkHeaderCases() |
| 111 | + { |
| 112 | + yield 'few iris' => [ |
| 113 | + 50, |
| 114 | + ['/foo', '/bar'], |
| 115 | + ['((^|\,)/foo($|\,))|((^|\,)/bar($|\,))'], |
| 116 | + ]; |
| 117 | + |
| 118 | + yield 'iris to generate a header with exactly the maximum length' => [ |
| 119 | + 56, |
| 120 | + ['/foo', '/bar', '/baz'], |
| 121 | + ['((^|\,)/foo($|\,))|((^|\,)/bar($|\,))|((^|\,)/baz($|\,))'], |
| 122 | + ]; |
| 123 | + |
| 124 | + yield 'iris to generate a header with exactly the maximum length and a smaller one' => [ |
| 125 | + 37, |
| 126 | + ['/foo', '/bar', '/baz'], |
| 127 | + [ |
| 128 | + '((^|\,)/foo($|\,))|((^|\,)/bar($|\,))', |
| 129 | + '(^|\,)/baz($|\,)', |
| 130 | + ], |
| 131 | + ]; |
| 132 | + |
| 133 | + yield 'with last iri too long to be part of the same header' => [ |
| 134 | + 50, |
| 135 | + ['/foo', '/bar', '/some-longer-tag'], |
| 136 | + [ |
| 137 | + '((^|\,)/foo($|\,))|((^|\,)/bar($|\,))', |
| 138 | + '(^|\,)/some\-longer\-tag($|\,)', |
| 139 | + ], |
| 140 | + ]; |
| 141 | + |
| 142 | + yield 'iris to have five headers' => [ |
| 143 | + 50, |
| 144 | + ['/foo/1', '/foo/2', '/foo/3', '/foo/4', '/foo/5', '/foo/6', '/foo/7', '/foo/8', '/foo/9', '/foo/10'], |
| 145 | + [ |
| 146 | + '((^|\,)/foo/1($|\,))|((^|\,)/foo/2($|\,))', |
| 147 | + '((^|\,)/foo/3($|\,))|((^|\,)/foo/4($|\,))', |
| 148 | + '((^|\,)/foo/5($|\,))|((^|\,)/foo/6($|\,))', |
| 149 | + '((^|\,)/foo/7($|\,))|((^|\,)/foo/8($|\,))', |
| 150 | + '((^|\,)/foo/9($|\,))|((^|\,)/foo/10($|\,))', |
| 151 | + ], |
| 152 | + ]; |
| 153 | + |
| 154 | + yield 'with varnish default limit' => [ |
| 155 | + 8000, |
| 156 | + array_fill(0, 1000, '/foo'), |
| 157 | + [ |
| 158 | + implode('|', array_fill(0, 421, '((^|\,)/foo($|\,))')), |
| 159 | + implode('|', array_fill(0, 421, '((^|\,)/foo($|\,))')), |
| 160 | + implode('|', array_fill(0, 158, '((^|\,)/foo($|\,))')), |
| 161 | + ], |
| 162 | + ]; |
| 163 | + } |
63 | 164 | }
|
0 commit comments