Skip to content

Commit

Permalink
Merge pull request #2666 from arif-rh/fix-bug-ResourceController-json…
Browse files Browse the repository at this point in the history
…-response

Fix ResourceController Issue, including Test Unit
  • Loading branch information
MGatner authored Mar 13, 2020
2 parents b135c17 + 8d25689 commit 54d370f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
16 changes: 15 additions & 1 deletion system/API/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ trait ResponseTrait
'not_implemented' => 501,
];

/**
*
* @var string the representation format to return resource data in (json/xml)
*/
protected $format = 'json';

//--------------------------------------------------------------------

/**
Expand Down Expand Up @@ -366,7 +372,15 @@ protected function format($data = null)

// Determine correct response type through content negotiation
$config = new Format();
$format = $this->request->negotiate('media', $config->supportedResponseFormats, false);

if (! in_array($this->format, ['json', 'xml']))
{
$format = $this->request->negotiate('media', $config->supportedResponseFormats, false);
}
else
{
$format = "application/$this->format";
}

$this->response->setContentType($format);

Expand Down
6 changes: 0 additions & 6 deletions system/RESTful/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ class ResourceController extends Controller
*/
protected $model = null;

/**
*
* @var string the representation format to return resource data in (json/xml)
*/
protected $format = 'json';

//--------------------------------------------------------------------

public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
Expand Down
58 changes: 58 additions & 0 deletions tests/system/RESTful/ResourceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,62 @@ public function testFormat()
$this->assertEquals('xml', $resource->getFormat());
}

//--------------------------------------------------------------------
public function testJSONFormatOutput()
{
$resource = new \CodeIgniter\Test\Mock\MockResourceController();

$config = new \Config\App;
$uri = new \CodeIgniter\HTTP\URI;
$agent = new \CodeIgniter\HTTP\UserAgent;

$request = new \CodeIgniter\HTTP\IncomingRequest($config, $uri, '', $agent);
$response = new \CodeIgniter\HTTP\Response($config);
$logger = new \Psr\Log\NullLogger;

$resource->initController($request, $response, $logger);
$resource->setFormat('json');

$data = [
'foo' => 'bar',
];

$the_response = $resource->respond($data);
$result = $the_response->getBody();

$JSONFormatter = new \CodeIgniter\Format\JSONFormatter;
$expected = $JSONFormatter->format($data);

$this->assertEquals($expected, $result);
}

//--------------------------------------------------------------------
public function testXMLFormatOutput()
{
$resource = new \CodeIgniter\Test\Mock\MockResourceController();

$config = new \Config\App;
$uri = new \CodeIgniter\HTTP\URI;
$agent = new \CodeIgniter\HTTP\UserAgent;

$request = new \CodeIgniter\HTTP\IncomingRequest($config, $uri, '', $agent);
$response = new \CodeIgniter\HTTP\Response($config);
$logger = new \Psr\Log\NullLogger;

$resource->initController($request, $response, $logger);
$resource->setFormat('xml');

$data = [
'foo' => 'bar',
];

$the_response = $resource->respond($data);
$result = $the_response->getBody();

$XMLFormatter = new \CodeIgniter\Format\XMLFormatter;
$expected = $XMLFormatter->format($data);

$this->assertEquals($expected, $result);
}

}

0 comments on commit 54d370f

Please sign in to comment.