JSONResponseTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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;
  26. use OCP\AppFramework\Http\JSONResponse;
  27. class JSONResponseTest extends \Test\TestCase {
  28. /**
  29. * @var JSONResponse
  30. */
  31. private $json;
  32. protected function setUp(): void {
  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 = ['hi', 'yo'];
  42. $this->json->setData($params);
  43. $this->assertEquals(['hi', 'yo'], $this->json->getData());
  44. }
  45. public function testSetRender() {
  46. $params = ['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. public function testRenderWithNonUtf8Encoding() {
  74. $this->expectException(\JsonException::class);
  75. $this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
  76. $params = ['test' => hex2bin('e9')];
  77. $this->json->setData($params);
  78. $this->json->render();
  79. }
  80. public function testConstructorAllowsToSetData() {
  81. $data = ['hi'];
  82. $code = 300;
  83. $response = new JSONResponse($data, $code);
  84. $expected = '["hi"]';
  85. $this->assertEquals($expected, $response->render());
  86. $this->assertEquals($code, $response->getStatus());
  87. }
  88. public function testChainability() {
  89. $params = ['hi', 'yo'];
  90. $this->json->setData($params)
  91. ->setStatus(Http::STATUS_NOT_FOUND);
  92. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->json->getStatus());
  93. $this->assertEquals(['hi', 'yo'], $this->json->getData());
  94. }
  95. }