diff --git a/composer.json b/composer.json index 4651140..98783c8 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "php": ">= 5.4.0", - "nette/application": "^2.0", + "nette/application": "^2.3.12", "nette/http": "^2.0", "tracy/tracy": "^2.0", "league/fractal": "^0.12.0", diff --git a/src/Response/JsonApiResponse.php b/src/Response/JsonApiResponse.php index 7e96c00..3ba140d 100644 --- a/src/Response/JsonApiResponse.php +++ b/src/Response/JsonApiResponse.php @@ -3,6 +3,7 @@ namespace Tomaj\NetteApi\Response; use Nette\Application\Responses\JsonResponse; +use Nette; class JsonApiResponse extends JsonResponse { @@ -11,6 +12,11 @@ class JsonApiResponse extends JsonResponse */ private $code; + /** + * @var string + */ + private $charset; + /** * Create JsonApiResponse * This class only wrap JsonResponse from Nette and add possibility @@ -19,10 +25,12 @@ class JsonApiResponse extends JsonResponse * @param integer $code * @param mixed $data * @param string $contentType + * @param string $charset */ - public function __construct($code, $data, $contentType = 'application/json; charset=utf-8') + public function __construct($code, $data, $contentType = 'application/json', $charset ='utf-8') { parent::__construct($data, $contentType); + $this->charset = $charset; $this->code = $code; } @@ -35,4 +43,25 @@ public function getCode() { return $this->code; } + + /** + * Return encoding charset for http response + * + * @return string + */ + public function getCharset() + { + return $this->charset; + } + + /** + * Sends response to output. + * @return void + */ + public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) + { + $httpResponse->setContentType($this->contentType, $this->charset); + $httpResponse->setExpiration(FALSE); + echo Nette\Utils\Json::encode($this->payload); + } } diff --git a/tests/Handler/DefaultHandlerTest.php b/tests/Handler/DefaultHandlerTest.php index e3b1dba..c528b7b 100644 --- a/tests/Handler/DefaultHandlerTest.php +++ b/tests/Handler/DefaultHandlerTest.php @@ -16,7 +16,8 @@ public function testResponse() $defaultHandler = new DefaultHandler(); $result = $defaultHandler->handle([]); $this->assertEquals(500, $result->getCode()); - $this->assertEquals('application/json; charset=utf-8', $result->getContentType()); + $this->assertEquals('application/json', $result->getContentType()); + $this->assertEquals('utf-8', $result->getCharset()); $this->assertEquals(['status' => 'error', 'message' => 'Unknown api endpoint'], $result->getPayload()); } diff --git a/tests/Presenters/ApiPresenterTest.php b/tests/Presenters/ApiPresenterTest.php index 74943da..2a741c4 100644 --- a/tests/Presenters/ApiPresenterTest.php +++ b/tests/Presenters/ApiPresenterTest.php @@ -34,7 +34,8 @@ public function testSimpleResponse() $this->assertEquals(200, $result->getCode()); $this->assertEquals(['status' => 'ok'], $result->getPayload()); - $this->assertEquals('application/json; charset=utf-8', $result->getContentType()); + $this->assertEquals('application/json', $result->getContentType()); + $this->assertEquals('utf-8', $result->getCharset()); } public function testWithAuthorization()