Automatically return allowed HTTP methods for OPTIONS per specific endpoint
This commit is contained in:
parent
71272e07ee
commit
dc46af5ea1
5 changed files with 177 additions and 34 deletions
|
@ -10,7 +10,7 @@ use Friendica\Test\FixtureTest;
|
|||
|
||||
class OptionsTest extends FixtureTest
|
||||
{
|
||||
public function testOptions()
|
||||
public function testOptionsAll()
|
||||
{
|
||||
$this->useHttpMethod(Router::OPTIONS);
|
||||
|
||||
|
@ -25,4 +25,22 @@ class OptionsTest extends FixtureTest
|
|||
], $response->getHeaders());
|
||||
self::assertEquals(implode(',', Router::ALLOWED_METHODS), $response->getHeaderLine('Allow'));
|
||||
}
|
||||
|
||||
public function testOptionsSpecific()
|
||||
{
|
||||
$this->useHttpMethod(Router::OPTIONS);
|
||||
|
||||
$response = (new Options(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], [
|
||||
'AllowedMethods' => [Router::GET, Router::POST],
|
||||
]))->run();
|
||||
|
||||
self::assertEmpty((string)$response->getBody());
|
||||
self::assertEquals(204, $response->getStatusCode());
|
||||
self::assertEquals('No Content', $response->getReasonPhrase());
|
||||
self::assertEquals([
|
||||
'Allow' => [implode(',', [Router::GET, Router::POST])],
|
||||
ICanCreateResponses::X_HEADER => ['html'],
|
||||
], $response->getHeaders());
|
||||
self::assertEquals(implode(',', [Router::GET, Router::POST]), $response->getHeaderLine('Allow'));
|
||||
}
|
||||
}
|
||||
|
|
28
tests/src/Util/Router/FriendicaGroupCountBasedTest.php
Normal file
28
tests/src/Util/Router/FriendicaGroupCountBasedTest.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Util\Router;
|
||||
|
||||
use FastRoute\DataGenerator\GroupCountBased;
|
||||
use FastRoute\RouteCollector;
|
||||
use FastRoute\RouteParser\Std;
|
||||
use Friendica\Module\Special\Options;
|
||||
use Friendica\Test\MockedTest;
|
||||
use Friendica\Util\Router\FriendicaGroupCountBased;
|
||||
|
||||
class FriendicaGroupCountBasedTest extends MockedTest
|
||||
{
|
||||
public function testOptions()
|
||||
{
|
||||
$collector = new RouteCollector(new Std(), new GroupCountBased());
|
||||
$collector->addRoute('GET', '/get', Options::class);
|
||||
$collector->addRoute('POST', '/post', Options::class);
|
||||
$collector->addRoute('GET', '/multi', Options::class);
|
||||
$collector->addRoute('POST', '/multi', Options::class);
|
||||
|
||||
$dispatcher = new FriendicaGroupCountBased($collector->getData());
|
||||
|
||||
self::assertEquals(['GET'], $dispatcher->getOptions('/get'));
|
||||
self::assertEquals(['POST'], $dispatcher->getOptions('/post'));
|
||||
self::assertEquals(['GET', 'POST'], $dispatcher->getOptions('/multi'));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue