ResponseTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. use OCP\AppFramework\Utility\ITimeFactory;
  26. class ResponseTest extends \Test\TestCase {
  27. /**
  28. * @var \OCP\AppFramework\Http\Response
  29. */
  30. private $childResponse;
  31. protected function setUp(){
  32. parent::setUp();
  33. $this->childResponse = new Response();
  34. }
  35. public function testAddHeader(){
  36. $this->childResponse->addHeader(' hello ', 'world');
  37. $headers = $this->childResponse->getHeaders();
  38. $this->assertEquals('world', $headers['hello']);
  39. }
  40. public function testSetHeaders() {
  41. $expected = array(
  42. 'Last-Modified' => 1,
  43. 'ETag' => 3,
  44. 'Something-Else' => 'hi'
  45. );
  46. $this->childResponse->setHeaders($expected);
  47. $headers = $this->childResponse->getHeaders();
  48. $expected['Content-Security-Policy'] = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self' data:;connect-src 'self';media-src 'self';frame-ancestors 'self'";
  49. $this->assertEquals($expected, $headers);
  50. }
  51. public function testOverwriteCsp() {
  52. $expected = [
  53. 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-inline';style-src 'self' 'unsafe-inline';img-src 'self';font-src 'self' data:;connect-src 'self';media-src 'self'",
  54. ];
  55. $policy = new Http\ContentSecurityPolicy();
  56. $policy->allowInlineScript(true);
  57. $this->childResponse->setContentSecurityPolicy($policy);
  58. $headers = $this->childResponse->getHeaders();
  59. $this->assertEquals(array_merge($expected, $headers), $headers);
  60. }
  61. public function testGetCsp() {
  62. $policy = new Http\ContentSecurityPolicy();
  63. $policy->allowInlineScript(true);
  64. $this->childResponse->setContentSecurityPolicy($policy);
  65. $this->assertEquals($policy, $this->childResponse->getContentSecurityPolicy());
  66. }
  67. public function testGetCspEmpty() {
  68. $this->assertNull($this->childResponse->getContentSecurityPolicy());
  69. }
  70. public function testAddHeaderValueNullDeletesIt(){
  71. $this->childResponse->addHeader('hello', 'world');
  72. $this->childResponse->addHeader('hello', null);
  73. $this->assertEquals(2, count($this->childResponse->getHeaders()));
  74. }
  75. public function testCacheHeadersAreDisabledByDefault(){
  76. $headers = $this->childResponse->getHeaders();
  77. $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
  78. }
  79. public function testAddCookie() {
  80. $this->childResponse->addCookie('foo', 'bar');
  81. $this->childResponse->addCookie('bar', 'foo', new \DateTime('1970-01-01'));
  82. $expectedResponse = array(
  83. 'foo' => array(
  84. 'value' => 'bar',
  85. 'expireDate' => null,
  86. ),
  87. 'bar' => array(
  88. 'value' => 'foo',
  89. 'expireDate' => new \DateTime('1970-01-01')
  90. )
  91. );
  92. $this->assertEquals($expectedResponse, $this->childResponse->getCookies());
  93. }
  94. function testSetCookies() {
  95. $expected = array(
  96. 'foo' => array(
  97. 'value' => 'bar',
  98. 'expireDate' => null,
  99. ),
  100. 'bar' => array(
  101. 'value' => 'foo',
  102. 'expireDate' => new \DateTime('1970-01-01')
  103. )
  104. );
  105. $this->childResponse->setCookies($expected);
  106. $cookies = $this->childResponse->getCookies();
  107. $this->assertEquals($expected, $cookies);
  108. }
  109. function testInvalidateCookie() {
  110. $this->childResponse->addCookie('foo', 'bar');
  111. $this->childResponse->invalidateCookie('foo');
  112. $expected = array(
  113. 'foo' => array(
  114. 'value' => 'expired',
  115. 'expireDate' => new \DateTime('1971-01-01')
  116. )
  117. );
  118. $cookies = $this->childResponse->getCookies();
  119. $this->assertEquals($expected, $cookies);
  120. }
  121. function testInvalidateCookies() {
  122. $this->childResponse->addCookie('foo', 'bar');
  123. $this->childResponse->addCookie('bar', 'foo');
  124. $expected = array(
  125. 'foo' => array(
  126. 'value' => 'bar',
  127. 'expireDate' => null
  128. ),
  129. 'bar' => array(
  130. 'value' => 'foo',
  131. 'expireDate' => null
  132. )
  133. );
  134. $cookies = $this->childResponse->getCookies();
  135. $this->assertEquals($expected, $cookies);
  136. $this->childResponse->invalidateCookies(array('foo', 'bar'));
  137. $expected = array(
  138. 'foo' => array(
  139. 'value' => 'expired',
  140. 'expireDate' => new \DateTime('1971-01-01')
  141. ),
  142. 'bar' => array(
  143. 'value' => 'expired',
  144. 'expireDate' => new \DateTime('1971-01-01')
  145. )
  146. );
  147. $cookies = $this->childResponse->getCookies();
  148. $this->assertEquals($expected, $cookies);
  149. }
  150. public function testRenderReturnNullByDefault(){
  151. $this->assertEquals(null, $this->childResponse->render());
  152. }
  153. public function testGetStatus() {
  154. $default = $this->childResponse->getStatus();
  155. $this->childResponse->setStatus(Http::STATUS_NOT_FOUND);
  156. $this->assertEquals(Http::STATUS_OK, $default);
  157. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
  158. }
  159. public function testGetEtag() {
  160. $this->childResponse->setEtag('hi');
  161. $this->assertSame('hi', $this->childResponse->getEtag());
  162. }
  163. public function testGetLastModified() {
  164. $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
  165. $lastModified->setTimestamp(1);
  166. $this->childResponse->setLastModified($lastModified);
  167. $this->assertEquals($lastModified, $this->childResponse->getLastModified());
  168. }
  169. public function testCacheSecondsZero() {
  170. $this->childResponse->cacheFor(0);
  171. $headers = $this->childResponse->getHeaders();
  172. $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
  173. $this->assertFalse(isset($headers['Pragma']));
  174. $this->assertFalse(isset($headers['Expires']));
  175. }
  176. public function testCacheSeconds() {
  177. $time = $this->createMock(ITimeFactory::class);
  178. $time->method('getTime')
  179. ->willReturn('1234567');
  180. $this->overwriteService(ITimeFactory::class, $time);
  181. $this->childResponse->cacheFor(33);
  182. $headers = $this->childResponse->getHeaders();
  183. $this->assertEquals('max-age=33, must-revalidate', $headers['Cache-Control']);
  184. $this->assertEquals('public', $headers['Pragma']);
  185. $this->assertEquals('Thu, 15 Jan 1970 06:56:40 +0000', $headers['Expires']);
  186. }
  187. public function testEtagLastModifiedHeaders() {
  188. $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
  189. $lastModified->setTimestamp(1);
  190. $this->childResponse->setLastModified($lastModified);
  191. $headers = $this->childResponse->getHeaders();
  192. $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
  193. }
  194. public function testChainability() {
  195. $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
  196. $lastModified->setTimestamp(1);
  197. $this->childResponse->setEtag('hi')
  198. ->setStatus(Http::STATUS_NOT_FOUND)
  199. ->setLastModified($lastModified)
  200. ->cacheFor(33)
  201. ->addHeader('hello', 'world');
  202. $headers = $this->childResponse->getHeaders();
  203. $this->assertEquals('world', $headers['hello']);
  204. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
  205. $this->assertEquals('hi', $this->childResponse->getEtag());
  206. $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
  207. $this->assertEquals('max-age=33, must-revalidate',
  208. $headers['Cache-Control']);
  209. }
  210. public function testThrottle() {
  211. $this->assertFalse($this->childResponse->isThrottled());
  212. $this->childResponse->throttle();
  213. $this->assertTrue($this->childResponse->isThrottled());
  214. }
  215. public function testGetThrottleMetadata() {
  216. $this->childResponse->throttle(['foo' => 'bar']);
  217. $this->assertSame(['foo' => 'bar'], $this->childResponse->getThrottleMetadata());
  218. }
  219. }