JSONResponseTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @author Morris Jobke
  7. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  8. * @copyright 2013 Morris Jobke <morris.jobke@gmail.com>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\AppFramework\Http;
  25. use OCP\AppFramework\Http\JSONResponse;
  26. use OCP\AppFramework\Http;
  27. class JSONResponseTest extends \Test\TestCase {
  28. /**
  29. * @var JSONResponse
  30. */
  31. private $json;
  32. protected function setUp() {
  33. parent::setUp();
  34. $this->json = new JSONResponse();
  35. }
  36. public function testHeader() {
  37. $headers = $this->json->getHeaders();
  38. $this->assertEquals('application/json; charset=utf-8', $headers['Content-Type']);
  39. }
  40. public function testSetData() {
  41. $params = array('hi', 'yo');
  42. $this->json->setData($params);
  43. $this->assertEquals(array('hi', 'yo'), $this->json->getData());
  44. }
  45. public function testSetRender() {
  46. $params = array('test' => 'hi');
  47. $this->json->setData($params);
  48. $expected = '{"test":"hi"}';
  49. $this->assertEquals($expected, $this->json->render());
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function renderDataProvider() {
  55. return [
  56. [
  57. ['test' => 'hi'], '{"test":"hi"}',
  58. ],
  59. [
  60. ['<h1>test' => '<h1>hi'], '{"\u003Ch1\u003Etest":"\u003Ch1\u003Ehi"}',
  61. ],
  62. ];
  63. }
  64. /**
  65. * @dataProvider renderDataProvider
  66. * @param array $input
  67. * @param string $expected
  68. */
  69. public function testRender(array $input, $expected) {
  70. $this->json->setData($input);
  71. $this->assertEquals($expected, $this->json->render());
  72. }
  73. /**
  74. * @expectedException \Exception
  75. * @expectedExceptionMessage Could not json_encode due to invalid non UTF-8 characters in the array: array (
  76. */
  77. public function testRenderWithNonUtf8Encoding() {
  78. $params = ['test' => hex2bin('e9')];
  79. $this->json->setData($params);
  80. $this->json->render();
  81. }
  82. public function testConstructorAllowsToSetData() {
  83. $data = array('hi');
  84. $code = 300;
  85. $response = new JSONResponse($data, $code);
  86. $expected = '["hi"]';
  87. $this->assertEquals($expected, $response->render());
  88. $this->assertEquals($code, $response->getStatus());
  89. }
  90. public function testChainability() {
  91. $params = array('hi', 'yo');
  92. $this->json->setData($params)
  93. ->setStatus(Http::STATUS_NOT_FOUND);
  94. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->json->getStatus());
  95. $this->assertEquals(array('hi', 'yo'), $this->json->getData());
  96. }
  97. }