Skip to content

Commit

Permalink
Updated JsonApiResponse for new nette version
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaj committed May 4, 2016
1 parent d339f37 commit 382f5f4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 30 additions & 1 deletion src/Response/JsonApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tomaj\NetteApi\Response;

use Nette\Application\Responses\JsonResponse;
use Nette;

class JsonApiResponse extends JsonResponse
{
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion tests/Handler/DefaultHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Presenters/ApiPresenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 382f5f4

Please sign in to comment.