863ad76147fcc4c1e37fc32531f9ed3b1ea41732
[yaffs-website] / vendor / symfony / http-kernel / Tests / HttpCache / EsiTest.php
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\HttpKernel\Tests\HttpCache;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\HttpCache\Esi;
18
19 class EsiTest extends TestCase
20 {
21     public function testHasSurrogateEsiCapability()
22     {
23         $esi = new Esi();
24
25         $request = Request::create('/');
26         $request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
27         $this->assertTrue($esi->hasSurrogateCapability($request));
28
29         $request = Request::create('/');
30         $request->headers->set('Surrogate-Capability', 'foobar');
31         $this->assertFalse($esi->hasSurrogateCapability($request));
32
33         $request = Request::create('/');
34         $this->assertFalse($esi->hasSurrogateCapability($request));
35     }
36
37     public function testAddSurrogateEsiCapability()
38     {
39         $esi = new Esi();
40
41         $request = Request::create('/');
42         $esi->addSurrogateCapability($request);
43         $this->assertEquals('symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
44
45         $esi->addSurrogateCapability($request);
46         $this->assertEquals('symfony="ESI/1.0", symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
47     }
48
49     public function testAddSurrogateControl()
50     {
51         $esi = new Esi();
52
53         $response = new Response('foo <esi:include src="" />');
54         $esi->addSurrogateControl($response);
55         $this->assertEquals('content="ESI/1.0"', $response->headers->get('Surrogate-Control'));
56
57         $response = new Response('foo');
58         $esi->addSurrogateControl($response);
59         $this->assertEquals('', $response->headers->get('Surrogate-Control'));
60     }
61
62     public function testNeedsEsiParsing()
63     {
64         $esi = new Esi();
65
66         $response = new Response();
67         $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
68         $this->assertTrue($esi->needsParsing($response));
69
70         $response = new Response();
71         $this->assertFalse($esi->needsParsing($response));
72     }
73
74     public function testRenderIncludeTag()
75     {
76         $esi = new Esi();
77
78         $this->assertEquals('<esi:include src="/" onerror="continue" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', true));
79         $this->assertEquals('<esi:include src="/" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', false));
80         $this->assertEquals('<esi:include src="/" onerror="continue" />', $esi->renderIncludeTag('/'));
81         $this->assertEquals('<esi:comment text="some comment" />'."\n".'<esi:include src="/" onerror="continue" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', true, 'some comment'));
82     }
83
84     public function testProcessDoesNothingIfContentTypeIsNotHtml()
85     {
86         $esi = new Esi();
87
88         $request = Request::create('/');
89         $response = new Response();
90         $response->headers->set('Content-Type', 'text/plain');
91         $esi->process($request, $response);
92
93         $this->assertFalse($response->headers->has('x-body-eval'));
94     }
95
96     public function testMultilineEsiRemoveTagsAreRemoved()
97     {
98         $esi = new Esi();
99
100         $request = Request::create('/');
101         $response = new Response('<esi:remove> <a href="http://www.example.com">www.example.com</a> </esi:remove> Keep this'."<esi:remove>\n <a>www.example.com</a> </esi:remove> And this");
102         $esi->process($request, $response);
103
104         $this->assertEquals(' Keep this And this', $response->getContent());
105     }
106
107     public function testCommentTagsAreRemoved()
108     {
109         $esi = new Esi();
110
111         $request = Request::create('/');
112         $response = new Response('<esi:comment text="some comment &gt;" /> Keep this');
113         $esi->process($request, $response);
114
115         $this->assertEquals(' Keep this', $response->getContent());
116     }
117
118     public function testProcess()
119     {
120         $esi = new Esi();
121
122         $request = Request::create('/');
123         $response = new Response('foo <esi:comment text="some comment" /><esi:include src="..." alt="alt" onerror="continue" />');
124         $esi->process($request, $response);
125
126         $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'alt\', true) ?>'."\n", $response->getContent());
127         $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
128
129         $response = new Response('foo <esi:comment text="some comment" /><esi:include src="foo\'" alt="bar\'" onerror="continue" />');
130         $esi->process($request, $response);
131
132         $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'foo\\\'\', \'bar\\\'\', true) ?>'."\n", $response->getContent());
133
134         $response = new Response('foo <esi:include src="..." />');
135         $esi->process($request, $response);
136
137         $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
138
139         $response = new Response('foo <esi:include src="..."></esi:include>');
140         $esi->process($request, $response);
141
142         $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
143     }
144
145     public function testProcessEscapesPhpTags()
146     {
147         $esi = new Esi();
148
149         $request = Request::create('/');
150         $response = new Response('<?php <? <% <script language=php>');
151         $esi->process($request, $response);
152
153         $this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent());
154     }
155
156     /**
157      * @expectedException \RuntimeException
158      */
159     public function testProcessWhenNoSrcInAnEsi()
160     {
161         $esi = new Esi();
162
163         $request = Request::create('/');
164         $response = new Response('foo <esi:include />');
165         $esi->process($request, $response);
166     }
167
168     public function testProcessRemoveSurrogateControlHeader()
169     {
170         $esi = new Esi();
171
172         $request = Request::create('/');
173         $response = new Response('foo <esi:include src="..." />');
174         $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
175         $esi->process($request, $response);
176         $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
177
178         $response->headers->set('Surrogate-Control', 'no-store, content="ESI/1.0"');
179         $esi->process($request, $response);
180         $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
181         $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
182
183         $response->headers->set('Surrogate-Control', 'content="ESI/1.0", no-store');
184         $esi->process($request, $response);
185         $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
186         $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
187     }
188
189     public function testHandle()
190     {
191         $esi = new Esi();
192         $cache = $this->getCache(Request::create('/'), new Response('foo'));
193         $this->assertEquals('foo', $esi->handle($cache, '/', '/alt', true));
194     }
195
196     /**
197      * @expectedException \RuntimeException
198      */
199     public function testHandleWhenResponseIsNot200()
200     {
201         $esi = new Esi();
202         $response = new Response('foo');
203         $response->setStatusCode(404);
204         $cache = $this->getCache(Request::create('/'), $response);
205         $esi->handle($cache, '/', '/alt', false);
206     }
207
208     public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
209     {
210         $esi = new Esi();
211         $response = new Response('foo');
212         $response->setStatusCode(404);
213         $cache = $this->getCache(Request::create('/'), $response);
214         $this->assertEquals('', $esi->handle($cache, '/', '/alt', true));
215     }
216
217     public function testHandleWhenResponseIsNot200AndAltIsPresent()
218     {
219         $esi = new Esi();
220         $response1 = new Response('foo');
221         $response1->setStatusCode(404);
222         $response2 = new Response('bar');
223         $cache = $this->getCache(Request::create('/'), array($response1, $response2));
224         $this->assertEquals('bar', $esi->handle($cache, '/', '/alt', false));
225     }
226
227     protected function getCache($request, $response)
228     {
229         $cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(array('getRequest', 'handle'))->disableOriginalConstructor()->getMock();
230         $cache->expects($this->any())
231               ->method('getRequest')
232               ->will($this->returnValue($request))
233         ;
234         if (\is_array($response)) {
235             $cache->expects($this->any())
236                   ->method('handle')
237                   ->will(\call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
238             ;
239         } else {
240             $cache->expects($this->any())
241                   ->method('handle')
242                   ->will($this->returnValue($response))
243             ;
244         }
245
246         return $cache;
247     }
248 }