SapiTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\HTTP;
  4. class SapiTest extends \PHPUnit\Framework\TestCase
  5. {
  6. public function testConstructFromServerArray()
  7. {
  8. $request = Sapi::createFromServerArray([
  9. 'REQUEST_URI' => '/foo',
  10. 'REQUEST_METHOD' => 'GET',
  11. 'HTTP_USER_AGENT' => 'Evert',
  12. 'CONTENT_TYPE' => 'text/xml',
  13. 'CONTENT_LENGTH' => '400',
  14. 'SERVER_PROTOCOL' => 'HTTP/1.0',
  15. ]);
  16. $this->assertEquals('GET', $request->getMethod());
  17. $this->assertEquals('/foo', $request->getUrl());
  18. $this->assertEquals([
  19. 'User-Agent' => ['Evert'],
  20. 'Content-Type' => ['text/xml'],
  21. 'Content-Length' => ['400'],
  22. ], $request->getHeaders());
  23. $this->assertEquals('1.0', $request->getHttpVersion());
  24. $this->assertEquals('400', $request->getRawServerValue('CONTENT_LENGTH'));
  25. $this->assertNull($request->getRawServerValue('FOO'));
  26. }
  27. public function testConstructFromServerArrayOnNullUrl()
  28. {
  29. $this->expectException(\InvalidArgumentException::class);
  30. $this->expectExceptionMessage('The _SERVER array must have a REQUEST_URI key');
  31. $request = Sapi::createFromServerArray([
  32. 'REQUEST_METHOD' => 'GET',
  33. 'HTTP_USER_AGENT' => 'Evert',
  34. 'CONTENT_TYPE' => 'text/xml',
  35. 'CONTENT_LENGTH' => '400',
  36. 'SERVER_PROTOCOL' => 'HTTP/1.0',
  37. ]);
  38. }
  39. public function testConstructFromServerArrayOnNullMethod()
  40. {
  41. $this->expectException(\InvalidArgumentException::class);
  42. $this->expectExceptionMessage('The _SERVER array must have a REQUEST_METHOD key');
  43. $request = Sapi::createFromServerArray([
  44. 'REQUEST_URI' => '/foo',
  45. 'HTTP_USER_AGENT' => 'Evert',
  46. 'CONTENT_TYPE' => 'text/xml',
  47. 'CONTENT_LENGTH' => '400',
  48. 'SERVER_PROTOCOL' => 'HTTP/1.0',
  49. ]);
  50. }
  51. public function testConstructPHPAuth()
  52. {
  53. $request = Sapi::createFromServerArray([
  54. 'REQUEST_URI' => '/foo',
  55. 'REQUEST_METHOD' => 'GET',
  56. 'PHP_AUTH_USER' => 'user',
  57. 'PHP_AUTH_PW' => 'pass',
  58. ]);
  59. $this->assertEquals('GET', $request->getMethod());
  60. $this->assertEquals('/foo', $request->getUrl());
  61. $this->assertEquals([
  62. 'Authorization' => ['Basic '.base64_encode('user:pass')],
  63. ], $request->getHeaders());
  64. }
  65. public function testConstructPHPAuthDigest()
  66. {
  67. $request = Sapi::createFromServerArray([
  68. 'REQUEST_URI' => '/foo',
  69. 'REQUEST_METHOD' => 'GET',
  70. 'PHP_AUTH_DIGEST' => 'blabla',
  71. ]);
  72. $this->assertEquals('GET', $request->getMethod());
  73. $this->assertEquals('/foo', $request->getUrl());
  74. $this->assertEquals([
  75. 'Authorization' => ['Digest blabla'],
  76. ], $request->getHeaders());
  77. }
  78. public function testConstructRedirectAuth()
  79. {
  80. $request = Sapi::createFromServerArray([
  81. 'REQUEST_URI' => '/foo',
  82. 'REQUEST_METHOD' => 'GET',
  83. 'REDIRECT_HTTP_AUTHORIZATION' => 'Basic bla',
  84. ]);
  85. $this->assertEquals('GET', $request->getMethod());
  86. $this->assertEquals('/foo', $request->getUrl());
  87. $this->assertEquals([
  88. 'Authorization' => ['Basic bla'],
  89. ], $request->getHeaders());
  90. }
  91. /**
  92. * @runInSeparateProcess
  93. *
  94. * Unfortunately we have no way of testing if the HTTP response code got
  95. * changed.
  96. */
  97. public function testSend()
  98. {
  99. if (!function_exists('xdebug_get_headers')) {
  100. $this->markTestSkipped('XDebug needs to be installed for this test to run');
  101. }
  102. $response = new Response(204, ['Content-Type' => 'text/xml;charset=UTF-8']);
  103. // Second Content-Type header. Normally this doesn't make sense.
  104. $response->addHeader('Content-Type', 'application/xml');
  105. $response->setBody('foo');
  106. ob_start();
  107. Sapi::sendResponse($response);
  108. $headers = xdebug_get_headers();
  109. $result = ob_get_clean();
  110. header_remove();
  111. $this->assertEquals(
  112. [
  113. 'Content-Type: text/xml;charset=UTF-8',
  114. 'Content-Type: application/xml',
  115. ],
  116. $headers
  117. );
  118. $this->assertEquals('foo', $result);
  119. }
  120. /**
  121. * @runInSeparateProcess
  122. *
  123. * @depends testSend
  124. */
  125. public function testSendLimitedByContentLengthString()
  126. {
  127. $response = new Response(200);
  128. $response->addHeader('Content-Length', 19);
  129. $response->setBody('Send this sentence. Ignore this one.');
  130. ob_start();
  131. Sapi::sendResponse($response);
  132. $result = ob_get_clean();
  133. header_remove();
  134. $this->assertEquals('Send this sentence.', $result);
  135. }
  136. /**
  137. * Tests whether http2 is recognized.
  138. */
  139. public function testRecognizeHttp2()
  140. {
  141. $request = Sapi::createFromServerArray([
  142. 'SERVER_PROTOCOL' => 'HTTP/2.0',
  143. 'REQUEST_URI' => 'bla',
  144. 'REQUEST_METHOD' => 'GET',
  145. ]);
  146. $this->assertEquals('2.0', $request->getHttpVersion());
  147. }
  148. /**
  149. * @runInSeparateProcess
  150. *
  151. * @depends testSend
  152. */
  153. public function testSendLimitedByContentLengthStream()
  154. {
  155. $response = new Response(200, ['Content-Length' => 19]);
  156. $body = fopen('php://memory', 'w');
  157. fwrite($body, 'Ignore this. Send this sentence. Ignore this too.');
  158. rewind($body);
  159. fread($body, 13);
  160. $response->setBody($body);
  161. ob_start();
  162. Sapi::sendResponse($response);
  163. $result = ob_get_clean();
  164. header_remove();
  165. $this->assertEquals('Send this sentence.', $result);
  166. }
  167. /**
  168. * @runInSeparateProcess
  169. *
  170. * @depends testSend
  171. *
  172. * @dataProvider sendContentRangeStreamData
  173. */
  174. public function testSendContentRangeStream($ignoreAtStart, $sendText, $multiplier, $ignoreAtEnd, $contentLength)
  175. {
  176. $partial = str_repeat($sendText, $multiplier);
  177. $ignoreAtStartLength = strlen($ignoreAtStart);
  178. $ignoreAtEndLength = strlen($ignoreAtEnd);
  179. $body = fopen('php://memory', 'w');
  180. if (!$contentLength) {
  181. $contentLength = strlen($partial);
  182. }
  183. fwrite($body, $ignoreAtStart);
  184. fwrite($body, $partial);
  185. if ($ignoreAtEndLength > 0) {
  186. fwrite($body, $ignoreAtEnd);
  187. }
  188. rewind($body);
  189. if ($ignoreAtStartLength > 0) {
  190. fread($body, $ignoreAtStartLength);
  191. }
  192. $response = new Response(200, [
  193. 'Content-Length' => $contentLength,
  194. 'Content-Range' => sprintf('bytes %d-%d/%d', $ignoreAtStartLength, $ignoreAtStartLength + strlen($partial) - 1, $ignoreAtStartLength + strlen($partial) + $ignoreAtEndLength),
  195. ]);
  196. $response->setBody($body);
  197. ob_start();
  198. Sapi::sendResponse($response);
  199. $result = ob_get_clean();
  200. header_remove();
  201. $this->assertEquals($partial, $result);
  202. }
  203. public function sendContentRangeStreamData()
  204. {
  205. return [
  206. ['Ignore this. ', 'Send this.', 10, ' Ignore this at end.'],
  207. ['Ignore this. ', 'Send this.', 1000, ' Ignore this at end.'],
  208. ['Ignore this. ', 'S', 4096, ' Ignore this at end.'],
  209. ['I', 'S', 4094, 'E'],
  210. ['', 'Send this.', 10, ' Ignore this at end.'],
  211. ['', 'Send this.', 1000, ' Ignore this at end.'],
  212. ['', 'S', 4096, ' Ignore this at end.'],
  213. ['', 'S', 4094, 'En'],
  214. ['Ignore this. ', 'Send this.', 10, ''],
  215. ['Ignore this. ', 'Send this.', 1000, ''],
  216. ['Ignore this. ', 'S', 4096, ''],
  217. ['Ig', 'S', 4094, ''],
  218. // Provide contentLength greater than the bytes remaining in the stream.
  219. ['Ignore this. ', 'Send this.', 10, '', 101],
  220. ['Ignore this. ', 'Send this.', 1000, '', 10001],
  221. ['Ignore this. ', 'S', 4096, '', 5000000],
  222. ['I', 'S', 4094, '', 8095],
  223. // Provide contentLength equal to the bytes remaining in the stream.
  224. ['', 'Send this.', 10, '', 100],
  225. ['Ignore this. ', 'Send this.', 1000, '', 10000],
  226. ];
  227. }
  228. /**
  229. * @runInSeparateProcess
  230. *
  231. * @depends testSend
  232. */
  233. public function testSendWorksWithCallbackAsBody()
  234. {
  235. $response = new Response(200, [], function () {
  236. $fd = fopen('php://output', 'r+');
  237. fwrite($fd, 'foo');
  238. fclose($fd);
  239. });
  240. ob_start();
  241. Sapi::sendResponse($response);
  242. $result = ob_get_clean();
  243. $this->assertEquals('foo', $result);
  244. }
  245. public function testSendConnectionAborted(): void
  246. {
  247. $baseUrl = getenv('BASEURL');
  248. if (!$baseUrl) {
  249. $this->markTestSkipped('Set an environment value BASEURL to continue');
  250. }
  251. $url = rtrim($baseUrl, '/').'/connection_aborted.php';
  252. $chunk_size = 4 * 1024 * 1024;
  253. $fetch_size = 6 * 1024 * 1024;
  254. $stream = fopen($url, 'r');
  255. $size = 0;
  256. while ($size <= $fetch_size) {
  257. $temp = fread($stream, 8192);
  258. if (false === $temp) {
  259. break;
  260. }
  261. $size += strlen($temp);
  262. }
  263. fclose($stream);
  264. sleep(5);
  265. $bytes_read = file_get_contents(sys_get_temp_dir().'/dummy_stream_read_counter');
  266. $this->assertEquals($chunk_size * 2, $bytes_read);
  267. $this->assertGreaterThanOrEqual($fetch_size, $bytes_read);
  268. }
  269. }