Skip to content

Commit

Permalink
JsonResponse: allow scalar payload [Closes #168]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 19, 2016
1 parent d84a480 commit 25b3a34
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/Application/Responses/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,26 @@ class JsonResponse implements Nette\Application\IResponse
{
use Nette\SmartObject;

/** @var array|\stdClass */
/** @var mixed */
private $payload;

/** @var string */
private $contentType;


/**
* @param array|\stdClass payload
* @param string MIME content type
* @param mixed payload
* @param string MIME content type
*/
public function __construct($payload, $contentType = NULL)
{
if (!is_array($payload) && !is_object($payload)) {
throw new Nette\InvalidArgumentException(sprintf('Payload must be array or object class, %s given.', gettype($payload)));
}
$this->payload = $payload;
$this->contentType = $contentType ? $contentType : 'application/json';
}


/**
* @return array|\stdClass
* @return mixed
*/
public function getPayload()
{
Expand Down
12 changes: 12 additions & 0 deletions tests/Responses/JsonResponse.contentType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ test(function () {
Assert::same($encoded, ob_get_clean());
Assert::same('application/json; charset=utf-8', $response->getHeader('Content-Type'));
});

test(function () {
$data = TRUE;
$encoded = json_encode($data, JSON_UNESCAPED_UNICODE);
$jsonResponse = new JsonResponse($data, 'application/json');

ob_start();
$jsonResponse->send(new Http\Request(new Http\UrlScript), $response = new Http\Response);

Assert::same($encoded, ob_get_clean());
Assert::same('application/json; charset=utf-8', $response->getHeader('Content-Type'));
});

0 comments on commit 25b3a34

Please sign in to comment.