Merge branch '4.4' into 5.1
* 4.4: [FrameworkBundle] adopt src/.preload.php [Cache] Fix key encoding issue in Memcached adapter [HttpClient] Fix Array to string conversion notice when parsing JSON error body with non-scalar detail property
This commit is contained in:
commit
4409a30dee
@ -179,7 +179,7 @@ EOF
|
||||
$preloadFile = $fs->makePathRelative(\dirname($containerFile, 2), $kernelDir);
|
||||
$preloadFile .= substr_replace(basename($containerFile), '.preload', -4, 0);
|
||||
$preloadFile = var_export('/'.$preloadFile, true);
|
||||
@file_put_contents($kernelDir.'/preload.php', <<<EOPHP
|
||||
@file_put_contents($kernelDir.'/.preload.php', <<<EOPHP
|
||||
<?php
|
||||
|
||||
if (file_exists(__DIR__.$preloadFile)) {
|
||||
|
@ -42,11 +42,11 @@ class MemcachedAdapterTest extends AdapterTestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
|
||||
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null, string $namespace = null): CacheItemPoolInterface
|
||||
{
|
||||
$client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST')) : self::$client;
|
||||
|
||||
return new MemcachedAdapter($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
|
||||
return new MemcachedAdapter($client, $namespace ?? str_replace('\\', '.', __CLASS__), $defaultLifetime);
|
||||
}
|
||||
|
||||
public function testOptions()
|
||||
@ -248,4 +248,36 @@ class MemcachedAdapterTest extends AdapterTestCase
|
||||
];
|
||||
$this->assertSame($expected, $client->getServerList());
|
||||
}
|
||||
|
||||
public function testKeyEncoding()
|
||||
{
|
||||
$reservedMemcachedCharacters = " \n\r\t\v\f\0";
|
||||
|
||||
$namespace = $reservedMemcachedCharacters.random_int(0, \PHP_INT_MAX);
|
||||
$pool = $this->createCachePool(0, null, $namespace);
|
||||
|
||||
/**
|
||||
* Choose a key that is below {@see \Symfony\Component\Cache\Adapter\MemcachedAdapter::$maxIdLength} so that
|
||||
* {@see \Symfony\Component\Cache\Traits\AbstractTrait::getId()} does not shorten the key but choose special
|
||||
* characters that would be encoded and therefore increase the key length over the Memcached limit.
|
||||
*/
|
||||
// 250 is Memcached’s max key length, 7 bytes for prefix seed
|
||||
$key = str_repeat('%', 250 - 7 - \strlen($reservedMemcachedCharacters) - \strlen($namespace)).$reservedMemcachedCharacters;
|
||||
|
||||
self::assertFalse($pool->hasItem($key));
|
||||
|
||||
$item = $pool->getItem($key);
|
||||
self::assertFalse($item->isHit());
|
||||
self::assertSame($key, $item->getKey());
|
||||
|
||||
self::assertTrue($pool->save($item->set('foobar')));
|
||||
|
||||
self::assertTrue($pool->hasItem($key));
|
||||
$item = $pool->getItem($key);
|
||||
self::assertTrue($item->isHit());
|
||||
self::assertSame($key, $item->getKey());
|
||||
|
||||
self::assertTrue($pool->deleteItem($key));
|
||||
self::assertFalse($pool->hasItem($key));
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,8 @@ trait HttpExceptionTrait
|
||||
// see http://www.hydra-cg.com/spec/latest/core/#description-of-http-status-codes-and-errors
|
||||
$separator = isset($body['hydra:title'], $body['hydra:description']) ? "\n\n" : '';
|
||||
$message = ($body['hydra:title'] ?? '').$separator.($body['hydra:description'] ?? '');
|
||||
} elseif (isset($body['title']) || isset($body['detail'])) {
|
||||
} elseif ((isset($body['title']) || isset($body['detail']))
|
||||
&& (is_scalar($body['title'] ?? '') && is_scalar($body['detail'] ?? ''))) {
|
||||
// see RFC 7807 and https://jsonapi.org/format/#error-objects
|
||||
$separator = isset($body['title'], $body['detail']) ? "\n\n" : '';
|
||||
$message = ($body['title'] ?? '').$separator.($body['detail'] ?? '');
|
||||
|
@ -22,15 +22,24 @@ class HttpExceptionTraitTest extends TestCase
|
||||
{
|
||||
public function provideParseError(): iterable
|
||||
{
|
||||
yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}'];
|
||||
yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}'];
|
||||
yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}'];
|
||||
$errorWithoutMessage = 'HTTP/1.1 400 Bad Request returned for "http://example.com".';
|
||||
|
||||
$errorWithMessage = <<<ERROR
|
||||
An error occurred
|
||||
|
||||
Some details
|
||||
ERROR;
|
||||
|
||||
yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}', $errorWithMessage];
|
||||
yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}', $errorWithMessage];
|
||||
yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}', $errorWithMessage];
|
||||
yield ['application/json', '{"title": "An error occurred", "detail": {"field_name": ["Some details"]}}', $errorWithoutMessage];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideParseError
|
||||
*/
|
||||
public function testParseError(string $mimeType, string $json): void
|
||||
public function testParseError(string $mimeType, string $json, string $expectedMessage): void
|
||||
{
|
||||
$response = $this->createMock(ResponseInterface::class);
|
||||
$response
|
||||
@ -47,12 +56,7 @@ class HttpExceptionTraitTest extends TestCase
|
||||
|
||||
$e = new TestException($response);
|
||||
$this->assertSame(400, $e->getCode());
|
||||
$this->assertSame(<<<ERROR
|
||||
An error occurred
|
||||
|
||||
Some details
|
||||
ERROR
|
||||
, $e->getMessage());
|
||||
$this->assertSame($expectedMessage, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user