mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
[FEATURE] Added Request::getContentType for content-type header-parsing
- Added unit-tests for Request::getContentType parsing.
This commit is contained in:
@@ -19,6 +19,10 @@ class Request
|
||||
public const REQUEST_TYPE_DELETE = 'delete';
|
||||
public const REQUEST_TYPE_HEAD = 'head';
|
||||
|
||||
public const CONTENT_TYPE_JSON = 'application/json';
|
||||
public const CONTENT_TYPE_FORM_DATA = 'multipart/form-data';
|
||||
public const CONTENT_TYPE_X_FORM_ENCODED = 'application/x-www-form-urlencoded';
|
||||
|
||||
/**
|
||||
* All request-types
|
||||
* @var string[]
|
||||
@@ -57,6 +61,12 @@ class Request
|
||||
*/
|
||||
protected $headers = [];
|
||||
|
||||
/**
|
||||
* Request ContentType
|
||||
* @var string
|
||||
*/
|
||||
protected $contentType;
|
||||
|
||||
/**
|
||||
* Request host
|
||||
* @var string
|
||||
@@ -117,10 +127,10 @@ class Request
|
||||
$this->setHost($this->getHeader('http-host'));
|
||||
|
||||
// Check if special IIS header exist, otherwise use default.
|
||||
|
||||
|
||||
$this->setUrl(new Url($this->getFirstHeader(['unencoded-url', 'request-uri',])));
|
||||
|
||||
$this->setContentType(strtolower($this->getHeader('content-type')));
|
||||
|
||||
$this->method = strtolower($this->getHeader('request-method'));
|
||||
$this->inputHandler = new InputHandler($this);
|
||||
$this->method = strtolower($this->inputHandler->value('_method', $this->getHeader('request-method')));
|
||||
@@ -289,6 +299,31 @@ class Request
|
||||
return $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request content-type
|
||||
* @return string|null
|
||||
*/
|
||||
public function getContentType(): ?string
|
||||
{
|
||||
return $this->contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set request content-type
|
||||
* @param string $contentType
|
||||
* @return $this
|
||||
*/
|
||||
protected function setContentType(string $contentType): self
|
||||
{
|
||||
if(strpos($contentType, ';') > 0) {
|
||||
$this->contentType = substr($contentType, 0, strpos($contentType, ';'));
|
||||
} else {
|
||||
$this->contentType = $contentType;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get input class
|
||||
* @return InputHandler
|
||||
@@ -497,4 +532,4 @@ class Request
|
||||
return $this->data[$name] ?? null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Pecee\Http\Input\InputFile;
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
|
||||
class RequestTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testContentTypeParse()
|
||||
{
|
||||
global $_SERVER;
|
||||
|
||||
$contentType = 'application/x-www-form-urlencoded';
|
||||
$_SERVER['content_type'] = $contentType;
|
||||
|
||||
$router = TestRouter::router();
|
||||
$router->reset();
|
||||
|
||||
$request = $router->getRequest();
|
||||
|
||||
$this->assertEquals($contentType, $request->getContentType());
|
||||
|
||||
// Test special content-types
|
||||
$router->reset();
|
||||
|
||||
$_SERVER['content_type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
|
||||
|
||||
$this->assertEquals($contentType, $request->getContentType());
|
||||
|
||||
$router->reset();
|
||||
}
|
||||
|
||||
// TODO: implement more test-cases
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user