From e9f7046a05c7d92c276c642793cd8f9c78928530 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 6 May 2021 09:26:16 +0300 Subject: [PATCH] fix request get content type --- application/Espo/Core/Api/RequestWrapper.php | 5 ++- tests/unit/Espo/Core/Api/RequestTest.php | 34 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/application/Espo/Core/Api/RequestWrapper.php b/application/Espo/Core/Api/RequestWrapper.php index 7788314099..304922799f 100644 --- a/application/Espo/Core/Api/RequestWrapper.php +++ b/application/Espo/Core/Api/RequestWrapper.php @@ -146,7 +146,10 @@ class RequestWrapper implements ApiRequest return null; } - $contentType = $this->request->getHeader('Content-Type')[0]; + $contentType = explode( + ';', + $this->request->getHeader('Content-Type')[0] + )[0]; return strtolower($contentType); } diff --git a/tests/unit/Espo/Core/Api/RequestTest.php b/tests/unit/Espo/Core/Api/RequestTest.php index 344677c7a3..de55d8c721 100644 --- a/tests/unit/Espo/Core/Api/RequestTest.php +++ b/tests/unit/Espo/Core/Api/RequestTest.php @@ -34,6 +34,8 @@ use Psr\Http\Message\{ StreamInterface, }; +use Slim\Psr7\Factory\RequestFactory; + use Espo\Core\Api\RequestWrapper; class RequestTest extends \PHPUnit\Framework\TestCase @@ -182,4 +184,36 @@ class RequestTest extends \PHPUnit\Framework\TestCase $this->assertNotSame($parsed->key2->key21[2], $anotherParsed->key2->key21[2]); } + + public function testContentType1(): void + { + $request = (new RequestFactory()) + ->createRequest('POST', 'http://localhost/?') + ->withHeader('Content-Type', 'application/json; charset=utf-8'); + + $requestWrapped = new RequestWrapper($request); + + $this->assertEquals('application/json', $requestWrapped->getContentType()); + } + + public function testContentType2(): void + { + $request = (new RequestFactory()) + ->createRequest('POST', 'http://localhost/?') + ->withHeader('Content-Type', 'application/json'); + + $requestWrapped = new RequestWrapper($request); + + $this->assertEquals('application/json', $requestWrapped->getContentType()); + } + + public function testContentTypeEmpty(): void + { + $request = (new RequestFactory()) + ->createRequest('POST', 'http://localhost/?'); + + $requestWrapped = new RequestWrapper($request); + + $this->assertEquals(null, $requestWrapped->getContentType()); + } }