ResponseTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Http;
  23. use OCP\AppFramework\Http\Response;
  24. use OCP\AppFramework\Http;
  25. class ResponseTest extends \Test\TestCase {
  26. /**
  27. * @var \OCP\AppFramework\Http\Response
  28. */
  29. private $childResponse;
  30. protected function setUp(){
  31. parent::setUp();
  32. $this->childResponse = new Response();
  33. }
  34. public function testAddHeader(){
  35. $this->childResponse->addHeader(' hello ', 'world');
  36. $headers = $this->childResponse->getHeaders();
  37. $this->assertEquals('world', $headers['hello']);
  38. }
  39. public function testSetHeaders() {
  40. $expected = array(
  41. 'Last-Modified' => 1,
  42. 'ETag' => 3,
  43. 'Something-Else' => 'hi'
  44. );
  45. $this->childResponse->setHeaders($expected);
  46. $headers = $this->childResponse->getHeaders();
  47. $expected['Content-Security-Policy'] = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
  48. $this->assertEquals($expected, $headers);
  49. }
  50. public function testOverwriteCsp() {
  51. $expected = [
  52. 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self';font-src 'self';connect-src 'self';media-src 'self'",
  53. ];
  54. $policy = new Http\ContentSecurityPolicy();
  55. $policy->allowInlineScript(true);
  56. $this->childResponse->setContentSecurityPolicy($policy);
  57. $headers = $this->childResponse->getHeaders();
  58. $this->assertEquals(array_merge($expected, $headers), $headers);
  59. }
  60. public function testGetCsp() {
  61. $policy = new Http\ContentSecurityPolicy();
  62. $policy->allowInlineScript(true);
  63. $this->childResponse->setContentSecurityPolicy($policy);
  64. $this->assertEquals($policy, $this->childResponse->getContentSecurityPolicy());
  65. }
  66. public function testGetCspEmpty() {
  67. $this->assertNull($this->childResponse->getContentSecurityPolicy());
  68. }
  69. public function testAddHeaderValueNullDeletesIt(){
  70. $this->childResponse->addHeader('hello', 'world');
  71. $this->childResponse->addHeader('hello', null);
  72. $this->assertEquals(2, count($this->childResponse->getHeaders()));
  73. }
  74. public function testCacheHeadersAreDisabledByDefault(){
  75. $headers = $this->childResponse->getHeaders();
  76. $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
  77. }
  78. public function testAddCookie() {
  79. $this->childResponse->addCookie('foo', 'bar');
  80. $this->childResponse->addCookie('bar', 'foo', new \DateTime('1970-01-01'));
  81. $expectedResponse = array(
  82. 'foo' => array(
  83. 'value' => 'bar',
  84. 'expireDate' => null,
  85. ),
  86. 'bar' => array(
  87. 'value' => 'foo',
  88. 'expireDate' => new \DateTime('1970-01-01')
  89. )
  90. );
  91. $this->assertEquals($expectedResponse, $this->childResponse->getCookies());
  92. }
  93. function testSetCookies() {
  94. $expected = array(
  95. 'foo' => array(
  96. 'value' => 'bar',
  97. 'expireDate' => null,
  98. ),
  99. 'bar' => array(
  100. 'value' => 'foo',
  101. 'expireDate' => new \DateTime('1970-01-01')
  102. )
  103. );
  104. $this->childResponse->setCookies($expected);
  105. $cookies = $this->childResponse->getCookies();
  106. $this->assertEquals($expected, $cookies);
  107. }
  108. function testInvalidateCookie() {
  109. $this->childResponse->addCookie('foo', 'bar');
  110. $this->childResponse->invalidateCookie('foo');
  111. $expected = array(
  112. 'foo' => array(
  113. 'value' => 'expired',
  114. 'expireDate' => new \DateTime('1971-01-01')
  115. )
  116. );
  117. $cookies = $this->childResponse->getCookies();
  118. $this->assertEquals($expected, $cookies);
  119. }
  120. function testInvalidateCookies() {
  121. $this->childResponse->addCookie('foo', 'bar');
  122. $this->childResponse->addCookie('bar', 'foo');
  123. $expected = array(
  124. 'foo' => array(
  125. 'value' => 'bar',
  126. 'expireDate' => null
  127. ),
  128. 'bar' => array(
  129. 'value' => 'foo',
  130. 'expireDate' => null
  131. )
  132. );
  133. $cookies = $this->childResponse->getCookies();
  134. $this->assertEquals($expected, $cookies);
  135. $this->childResponse->invalidateCookies(array('foo', 'bar'));
  136. $expected = array(
  137. 'foo' => array(
  138. 'value' => 'expired',
  139. 'expireDate' => new \DateTime('1971-01-01')
  140. ),
  141. 'bar' => array(
  142. 'value' => 'expired',
  143. 'expireDate' => new \DateTime('1971-01-01')
  144. )
  145. );
  146. $cookies = $this->childResponse->getCookies();
  147. $this->assertEquals($expected, $cookies);
  148. }
  149. public function testRenderReturnNullByDefault(){
  150. $this->assertEquals(null, $this->childResponse->render());
  151. }
  152. public function testGetStatus() {
  153. $default = $this->childResponse->getStatus();
  154. $this->childResponse->setStatus(Http::STATUS_NOT_FOUND);
  155. $this->assertEquals(Http::STATUS_OK, $default);
  156. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
  157. }
  158. public function testGetEtag() {
  159. $this->childResponse->setEtag('hi');
  160. $this->assertSame('hi', $this->childResponse->getEtag());
  161. }
  162. public function testGetLastModified() {
  163. $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
  164. $lastModified->setTimestamp(1);
  165. $this->childResponse->setLastModified($lastModified);
  166. $this->assertEquals($lastModified, $this->childResponse->getLastModified());
  167. }
  168. public function testCacheSecondsZero() {
  169. $this->childResponse->cacheFor(0);
  170. $headers = $this->childResponse->getHeaders();
  171. $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
  172. }
  173. public function testCacheSeconds() {
  174. $this->childResponse->cacheFor(33);
  175. $headers = $this->childResponse->getHeaders();
  176. $this->assertEquals('max-age=33, must-revalidate',
  177. $headers['Cache-Control']);
  178. }
  179. public function testEtagLastModifiedHeaders() {
  180. $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
  181. $lastModified->setTimestamp(1);
  182. $this->childResponse->setLastModified($lastModified);
  183. $headers = $this->childResponse->getHeaders();
  184. $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
  185. }
  186. public function testChainability() {
  187. $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
  188. $lastModified->setTimestamp(1);
  189. $this->childResponse->setEtag('hi')
  190. ->setStatus(Http::STATUS_NOT_FOUND)
  191. ->setLastModified($lastModified)
  192. ->cacheFor(33)
  193. ->addHeader('hello', 'world');
  194. $headers = $this->childResponse->getHeaders();
  195. $this->assertEquals('world', $headers['hello']);
  196. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
  197. $this->assertEquals('hi', $this->childResponse->getEtag());
  198. $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
  199. $this->assertEquals('max-age=33, must-revalidate',
  200. $headers['Cache-Control']);
  201. }
  202. }