Skip to content
Snippets Groups Projects
Commit 5cd5a9d0 authored by catch's avatar catch
Browse files

Issue #3002352 by longwave, andypost, gabesullice, googletorp, daffie,...

Issue #3002352 by longwave, andypost, gabesullice, googletorp, daffie, alexpott, borisson_: CacheableHttpException must pass a $headers argument to HttpException
parent bcfecbbf
Branches
Tags
17 merge requests!12227Issue #3181946 by jonmcl, mglaman,!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1896Issue #2940605: Can only intentionally re-render an entity with references 20 times,!1101Issue #2412669 by claudiu.cristea, Julfabre, sidharrell, catch, daffie,...,!1039Issue #2556069 by claudiu.cristea, bnjmnm, lauriii, pfrenssen, Tim Bozeman,...,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!872Draft: Issue #3221319: Race condition when creating menu links and editing content deletes menu links,!594Put each entity type table into a details element on admin/config/regional/content-language,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493,!512Issue #3207771: Menu UI node type form documentation points to non-existent function,!485Sets the autocomplete attribute for username/password input field on login form.,!449Issue #2784233: Allow multiple vocabularies in the taxonomy filter,!231Issue #2671162: summary text wysiwyg patch working fine on 9.2.0-dev,!43Resolve #3173180: Add UI for 'loading' html attribute to images,!30Issue #3182188: Updates composer usage to point at ./vendor/bin/composer
......@@ -16,15 +16,9 @@ class CacheableHttpException extends HttpException implements CacheableDependenc
/**
* {@inheritdoc}
*/
public function __construct(CacheableDependencyInterface $cacheability, $statusCode = 0, $message = NULL, \Exception $previous = NULL, $code = 0) {
public function __construct(CacheableDependencyInterface $cacheability, $statusCode = 0, $message = NULL, \Exception $previous = NULL, array $headers = [], $code = 0) {
$this->setCacheability($cacheability);
// @todo Remove condition in https://www.drupal.org/node/3002352
if (is_array($code)) {
parent::__construct($statusCode, $message, $previous, $code);
}
else {
parent::__construct($statusCode, $message, $previous, [], $code);
}
parent::__construct($statusCode, $message, $previous, $headers, $code);
}
}
......@@ -105,7 +105,7 @@ public function enhance(array $defaults, Request $request) {
$message = 'JSON:API does not yet support resource versioning for this resource type.';
$message .= ' For context, see https://www.drupal.org/project/drupal/issues/2992833#comment-12818258.';
$message .= ' To contribute, see https://www.drupal.org/project/drupal/issues/2350939 and https://www.drupal.org/project/drupal/issues/2809177.';
throw new CacheableHttpException($cacheability, 501, $message, NULL, 0);
throw new CacheableHttpException($cacheability, 501, $message);
}
return $defaults;
}
......@@ -151,7 +151,7 @@ public function enhance(array $defaults, Request $request) {
];
$cacheability = (new CacheableMetadata())->addCacheContexts($cache_contexts);
$message = 'JSON:API does not support filtering on revisions other than the latest version because a secure Drupal core API does not yet exist to do so.';
throw new CacheableHttpException($cacheability, 501, $message, NULL, 0);
throw new CacheableHttpException($cacheability, 501, $message);
}
// 'latest-version' and 'working-copy' are the only acceptable version
// identifiers for a collection resource.
......
<?php
namespace Drupal\Tests\Core\Http;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Http\Exception\CacheableAccessDeniedHttpException;
use Drupal\Core\Http\Exception\CacheableBadRequestHttpException;
use Drupal\Core\Http\Exception\CacheableConflictHttpException;
use Drupal\Core\Http\Exception\CacheableGoneHttpException;
use Drupal\Core\Http\Exception\CacheableHttpException;
use Drupal\Core\Http\Exception\CacheableLengthRequiredHttpException;
use Drupal\Core\Http\Exception\CacheableMethodNotAllowedHttpException;
use Drupal\Core\Http\Exception\CacheableNotAcceptableHttpException;
use Drupal\Core\Http\Exception\CacheableNotFoundHttpException;
use Drupal\Core\Http\Exception\CacheablePreconditionFailedHttpException;
use Drupal\Core\Http\Exception\CacheablePreconditionRequiredHttpException;
use Drupal\Core\Http\Exception\CacheableServiceUnavailableHttpException;
use Drupal\Core\Http\Exception\CacheableTooManyRequestsHttpException;
use Drupal\Core\Http\Exception\CacheableUnauthorizedHttpException;
use Drupal\Core\Http\Exception\CacheableUnprocessableEntityHttpException;
use Drupal\Core\Http\Exception\CacheableUnsupportedMediaTypeHttpException;
use Drupal\Tests\UnitTestCase;
/**
* @group Http
*/
class CacheableExceptionTest extends UnitTestCase {
/**
* @covers \Drupal\Core\Http\Exception\CacheableHttpException
*/
public function testCacheableHttpException() {
$exception = new CacheableHttpException((new CacheableMetadata())->setCacheContexts(['route']), 500, 'test message', NULL, ['X-Drupal-Exception' => 'Test'], 123);
$this->assertSame(['route'], $exception->getCacheContexts());
$this->assertSame(500, $exception->getStatusCode());
$this->assertSame('test message', $exception->getMessage());
$this->assertSame(['X-Drupal-Exception' => 'Test'], $exception->getHeaders());
$this->assertSame(123, $exception->getCode());
}
/**
* @dataProvider providerTestExceptions
*/
public function testExceptions($status_code, $class, $argument = NULL, $expected_headers = []) {
$cacheable_metadata = (new CacheableMetadata())->setCacheContexts(['route']);
$message = "$class test message";
if ($argument) {
$exception = new $class($cacheable_metadata, $argument, $message, NULL, 123);
}
else {
$exception = new $class($cacheable_metadata, $message, NULL, 123);
}
$this->assertSame(['route'], $exception->getCacheContexts());
$this->assertSame($message, $exception->getMessage());
$this->assertSame($status_code, $exception->getStatusCode());
$this->assertSame($expected_headers, $exception->getHeaders());
$this->assertSame(123, $exception->getCode());
}
public function providerTestExceptions() {
return [
[400, CacheableBadRequestHttpException::class],
[401, CacheableUnauthorizedHttpException::class, 'test challenge', ['WWW-Authenticate' => 'test challenge']],
[403, CacheableAccessDeniedHttpException::class],
[404, CacheableNotFoundHttpException::class],
[405, CacheableMethodNotAllowedHttpException::Class, ['POST', 'PUT'], ['Allow' => 'POST, PUT']],
[406, CacheableNotAcceptableHttpException::class],
[409, CacheableConflictHttpException::class],
[410, CacheableGoneHttpException::class],
[411, CacheableLengthRequiredHttpException::class],
[412, CacheablePreconditionFailedHttpException::class],
[415, CacheableUnsupportedMediaTypeHttpException::class],
[422, CacheableUnprocessableEntityHttpException::class],
[428, CacheablePreconditionRequiredHttpException::class],
[429, CacheableTooManyRequestsHttpException::class, 60, ['Retry-After' => 60]],
[503, CacheableServiceUnavailableHttpException::class, 60, ['Retry-After' => 60]],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment