Skip to content

Commit 6647406

Browse files
committed
Merge branch '2.8' into 3.2
* 2.8: HttpCache: New test for revalidating responses with an expired TTL [Serializer] [XML] Ignore Process Instruction
2 parents 8cd835e + 3b1143c commit 6647406

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,42 @@ public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
849849
$this->assertTraceNotContains('miss');
850850
}
851851

852+
public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInformation()
853+
{
854+
$time = \DateTime::createFromFormat('U', time());
855+
856+
$this->setNextResponse(200, array(), 'Hello World', function (Request $request, Response $response) use ($time) {
857+
$response->setSharedMaxAge(10);
858+
$response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
859+
});
860+
861+
// prime the cache
862+
$this->request('GET', '/');
863+
864+
// next request before s-maxage has expired: Serve from cache
865+
// without hitting the backend
866+
$this->request('GET', '/');
867+
$this->assertHttpKernelIsNotCalled();
868+
$this->assertEquals(200, $this->response->getStatusCode());
869+
$this->assertEquals('Hello World', $this->response->getContent());
870+
$this->assertTraceContains('fresh');
871+
872+
sleep(15); // expire the cache
873+
874+
$that = $this;
875+
876+
$this->setNextResponse(304, array(), '', function (Request $request, Response $response) use ($time, $that) {
877+
$that->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE'));
878+
});
879+
880+
$this->request('GET', '/');
881+
$this->assertHttpKernelIsCalled();
882+
$this->assertEquals(200, $this->response->getStatusCode());
883+
$this->assertEquals('Hello World', $this->response->getContent());
884+
$this->assertTraceContains('stale');
885+
$this->assertTraceContains('valid');
886+
}
887+
852888
public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
853889
{
854890
$time = \DateTime::createFromFormat('U', time());

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ public function decode($data, $format, array $context = array())
9595
throw new UnexpectedValueException($error->message);
9696
}
9797

98+
$rootNode = null;
9899
foreach ($dom->childNodes as $child) {
99100
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
100101
throw new UnexpectedValueException('Document types are not allowed.');
101102
}
103+
if (!$rootNode && $child->nodeType !== XML_PI_NODE) {
104+
$rootNode = $child;
105+
}
102106
}
103107

104-
$rootNode = $dom->firstChild;
105-
106108
// todo: throw an exception if the root node name is not correctly configured (bc)
107109

108110
if ($rootNode->hasChildNodes()) {
@@ -332,6 +334,10 @@ private function parseXmlValue(\DOMNode $node)
332334
$value = array();
333335

334336
foreach ($node->childNodes as $subnode) {
337+
if ($subnode->nodeType === XML_PI_NODE) {
338+
continue;
339+
}
340+
335341
$val = $this->parseXml($subnode);
336342

337343
if ('item' === $subnode->nodeName && isset($val['@key'])) {

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,44 @@ public function testDecodeArray()
370370
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
371371
}
372372

373+
public function testDecodeXMLWithProcessInstruction()
374+
{
375+
$source = <<<'XML'
376+
<?xml version="1.0"?>
377+
<?xml-stylesheet type="text/xsl" href="/xsl/xmlverbatimwrapper.xsl"?>
378+
<?display table-view?>
379+
<?sort alpha-ascending?>
380+
<response>
381+
<foo>foo</foo>
382+
<?textinfo whitespace is allowed ?>
383+
<bar>a</bar>
384+
<bar>b</bar>
385+
<baz>
386+
<key>val</key>
387+
<key2>val</key2>
388+
<item key="A B">bar</item>
389+
<item>
390+
<title>title1</title>
391+
</item>
392+
<?item ignore-title ?>
393+
<item>
394+
<title>title2</title>
395+
</item>
396+
<Barry>
397+
<FooBar id="1">
398+
<Baz>Ed</Baz>
399+
</FooBar>
400+
</Barry>
401+
</baz>
402+
<qux>1</qux>
403+
</response>
404+
<?instruction <value> ?>
405+
XML;
406+
$obj = $this->getObject();
407+
408+
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
409+
}
410+
373411
public function testDecodeIgnoreWhiteSpace()
374412
{
375413
$source = <<<'XML'

0 commit comments

Comments
 (0)