BodyParserMiddlewareTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @since 3.5.0
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http\Middleware;
  17. use Cake\Http\Exception\BadRequestException;
  18. use Cake\Http\Middleware\BodyParserMiddleware;
  19. use Cake\Http\Response;
  20. use Cake\Http\ServerRequest;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Test for BodyParser
  24. */
  25. class BodyParserMiddlewareTest extends TestCase
  26. {
  27. /**
  28. * Data provider for HTTP method tests.
  29. *
  30. * HEAD and GET do not populate $_POST or request->data.
  31. *
  32. * @return array
  33. */
  34. public static function safeHttpMethodProvider()
  35. {
  36. return [
  37. ['GET'],
  38. ['HEAD'],
  39. ];
  40. }
  41. /**
  42. * Data provider for HTTP methods that can contain request bodies.
  43. *
  44. * @return array
  45. */
  46. public static function httpMethodProvider()
  47. {
  48. return [
  49. ['PATCH'], ['PUT'], ['POST'], ['DELETE'],
  50. ];
  51. }
  52. /**
  53. * test constructor options
  54. *
  55. * @return void
  56. */
  57. public function testConstructorMethodsOption()
  58. {
  59. $parser = new BodyParserMiddleware(['methods' => ['PUT']]);
  60. $this->assertAttributeEquals(['PUT'], 'methods', $parser);
  61. }
  62. /**
  63. * test constructor options
  64. *
  65. * @return void
  66. */
  67. public function testConstructorXmlOption()
  68. {
  69. $parser = new BodyParserMiddleware(['json' => false]);
  70. $this->assertAttributeEquals([], 'parsers', $parser, 'Xml off by default');
  71. $parser = new BodyParserMiddleware(['json' => false, 'xml' => false]);
  72. $this->assertAttributeEquals([], 'parsers', $parser, 'No Xml types set.');
  73. $parser = new BodyParserMiddleware(['json' => false, 'xml' => true]);
  74. $expected = [
  75. 'application/xml' => [$parser, 'decodeXml'],
  76. 'text/xml' => [$parser, 'decodeXml'],
  77. ];
  78. $this->assertAttributeEquals($expected, 'parsers', $parser, 'Xml types are incorrect.');
  79. }
  80. /**
  81. * test constructor options
  82. *
  83. * @return void
  84. */
  85. public function testConstructorJsonOption()
  86. {
  87. $parser = new BodyParserMiddleware(['json' => false]);
  88. $this->assertAttributeEquals([], 'parsers', $parser, 'No JSON types set.');
  89. $parser = new BodyParserMiddleware([]);
  90. $expected = [
  91. 'application/json' => [$parser, 'decodeJson'],
  92. 'text/json' => [$parser, 'decodeJson'],
  93. ];
  94. $this->assertAttributeEquals($expected, 'parsers', $parser, 'JSON types are incorrect.');
  95. }
  96. /**
  97. * test setMethods()
  98. *
  99. * @return void
  100. */
  101. public function testSetMethodsReturn()
  102. {
  103. $parser = new BodyParserMiddleware();
  104. $this->assertSame($parser, $parser->setMethods(['PUT']));
  105. $this->assertAttributeEquals(['PUT'], 'methods', $parser);
  106. }
  107. /**
  108. * test addParser()
  109. *
  110. * @return void
  111. */
  112. public function testAddParserReturn()
  113. {
  114. $parser = new BodyParserMiddleware(['json' => false]);
  115. $this->assertSame($parser, $parser->addParser(['application/json'], 'json_decode'));
  116. }
  117. /**
  118. * test last parser defined wins
  119. *
  120. * @return void
  121. */
  122. public function testAddParserOverwrite()
  123. {
  124. $parser = new BodyParserMiddleware(['json' => false]);
  125. $parser->addParser(['application/json'], 'json_decode');
  126. $parser->addParser(['application/json'], 'strpos');
  127. $this->assertAttributeEquals(['application/json' => 'strpos'], 'parsers', $parser);
  128. }
  129. /**
  130. * test skipping parsing on unknown type
  131. *
  132. * @dataProvider httpMethodProvider
  133. * @return void
  134. */
  135. public function testInvokeMismatchedType($method)
  136. {
  137. $parser = new BodyParserMiddleware();
  138. $request = new ServerRequest([
  139. 'environment' => [
  140. 'REQUEST_METHOD' => $method,
  141. 'CONTENT_TYPE' => 'text/csv',
  142. ],
  143. 'input' => 'a,b,c',
  144. ]);
  145. $response = new Response();
  146. $next = function ($req, $res) {
  147. $this->assertEquals([], $req->getParsedBody());
  148. return $res;
  149. };
  150. $parser($request, $response, $next);
  151. }
  152. /**
  153. * test parsing on valid http method
  154. *
  155. * @dataProvider httpMethodProvider
  156. * @return void
  157. */
  158. public function testInvokeCaseInsensitiveContentType($method)
  159. {
  160. $parser = new BodyParserMiddleware();
  161. $request = new ServerRequest([
  162. 'environment' => [
  163. 'REQUEST_METHOD' => $method,
  164. 'CONTENT_TYPE' => 'ApPlIcAtIoN/JSoN',
  165. ],
  166. 'input' => '{"title": "yay"}',
  167. ]);
  168. $response = new Response();
  169. $next = function ($req, $res) {
  170. $this->assertEquals(['title' => 'yay'], $req->getParsedBody());
  171. return $res;
  172. };
  173. $parser($request, $response, $next);
  174. }
  175. /**
  176. * test parsing on valid http method
  177. *
  178. * @dataProvider httpMethodProvider
  179. * @return void
  180. */
  181. public function testInvokeParse($method)
  182. {
  183. $parser = new BodyParserMiddleware();
  184. $request = new ServerRequest([
  185. 'environment' => [
  186. 'REQUEST_METHOD' => $method,
  187. 'CONTENT_TYPE' => 'application/json',
  188. ],
  189. 'input' => '{"title": "yay"}',
  190. ]);
  191. $response = new Response();
  192. $next = function ($req, $res) {
  193. $this->assertEquals(['title' => 'yay'], $req->getParsedBody());
  194. return $res;
  195. };
  196. $parser($request, $response, $next);
  197. }
  198. /**
  199. * test parsing on valid http method with charset
  200. *
  201. * @return void
  202. */
  203. public function testInvokeParseStripCharset()
  204. {
  205. $parser = new BodyParserMiddleware();
  206. $request = new ServerRequest([
  207. 'environment' => [
  208. 'REQUEST_METHOD' => 'POST',
  209. 'CONTENT_TYPE' => 'application/json; charset=utf-8',
  210. ],
  211. 'input' => '{"title": "yay"}',
  212. ]);
  213. $response = new Response();
  214. $next = function ($req, $res) {
  215. $this->assertEquals(['title' => 'yay'], $req->getParsedBody());
  216. return $res;
  217. };
  218. $parser($request, $response, $next);
  219. }
  220. /**
  221. * test parsing on ignored http method
  222. *
  223. * @dataProvider safeHttpMethodProvider
  224. * @return void
  225. */
  226. public function testInvokeNoParseOnSafe($method)
  227. {
  228. $parser = new BodyParserMiddleware();
  229. $request = new ServerRequest([
  230. 'environment' => [
  231. 'REQUEST_METHOD' => $method,
  232. 'CONTENT_TYPE' => 'application/json',
  233. ],
  234. 'input' => '{"title": "yay"}',
  235. ]);
  236. $response = new Response();
  237. $next = function ($req, $res) {
  238. $this->assertEquals([], $req->getParsedBody());
  239. return $res;
  240. };
  241. $parser($request, $response, $next);
  242. }
  243. /**
  244. * test parsing XML bodies.
  245. *
  246. * @return void
  247. */
  248. public function testInvokeXml()
  249. {
  250. $xml = <<<XML
  251. <?xml version="1.0" encoding="utf-8"?>
  252. <article>
  253. <title>yay</title>
  254. </article>
  255. XML;
  256. $request = new ServerRequest([
  257. 'environment' => [
  258. 'REQUEST_METHOD' => 'POST',
  259. 'CONTENT_TYPE' => 'application/xml',
  260. ],
  261. 'input' => $xml,
  262. ]);
  263. $response = new Response();
  264. $next = function ($req, $res) {
  265. $expected = [
  266. 'article' => ['title' => 'yay'],
  267. ];
  268. $this->assertEquals($expected, $req->getParsedBody());
  269. return $res;
  270. };
  271. $parser = new BodyParserMiddleware(['xml' => true]);
  272. $parser($request, $response, $next);
  273. }
  274. /**
  275. * Test that CDATA is removed in XML data.
  276. *
  277. * @return void
  278. */
  279. public function testInvokeXmlCdata()
  280. {
  281. $xml = <<<XML
  282. <?xml version="1.0" encoding="utf-8"?>
  283. <article>
  284. <id>1</id>
  285. <title><![CDATA[first]]></title>
  286. </article>
  287. XML;
  288. $request = new ServerRequest([
  289. 'environment' => [
  290. 'REQUEST_METHOD' => 'POST',
  291. 'CONTENT_TYPE' => 'application/xml',
  292. ],
  293. 'input' => $xml,
  294. ]);
  295. $response = new Response();
  296. $next = function ($req, $res) {
  297. $expected = [
  298. 'article' => [
  299. 'id' => 1,
  300. 'title' => 'first',
  301. ],
  302. ];
  303. $this->assertEquals($expected, $req->getParsedBody());
  304. return $res;
  305. };
  306. $parser = new BodyParserMiddleware(['xml' => true]);
  307. $parser($request, $response, $next);
  308. }
  309. /**
  310. * Test that internal entity recursion is ignored.
  311. *
  312. * @return void
  313. */
  314. public function testInvokeXmlInternalEntities()
  315. {
  316. $xml = <<<XML
  317. <?xml version="1.0" encoding="UTF-8"?>
  318. <!DOCTYPE item [
  319. <!ENTITY item "item">
  320. <!ENTITY item1 "&item;&item;&item;&item;&item;&item;">
  321. <!ENTITY item2 "&item1;&item1;&item1;&item1;&item1;&item1;&item1;&item1;&item1;">
  322. <!ENTITY item3 "&item2;&item2;&item2;&item2;&item2;&item2;&item2;&item2;&item2;">
  323. <!ENTITY item4 "&item3;&item3;&item3;&item3;&item3;&item3;&item3;&item3;&item3;">
  324. <!ENTITY item5 "&item4;&item4;&item4;&item4;&item4;&item4;&item4;&item4;&item4;">
  325. <!ENTITY item6 "&item5;&item5;&item5;&item5;&item5;&item5;&item5;&item5;&item5;">
  326. <!ENTITY item7 "&item6;&item6;&item6;&item6;&item6;&item6;&item6;&item6;&item6;">
  327. <!ENTITY item8 "&item7;&item7;&item7;&item7;&item7;&item7;&item7;&item7;&item7;">
  328. ]>
  329. <item>
  330. <description>&item8;</description>
  331. </item>
  332. XML;
  333. $request = new ServerRequest([
  334. 'environment' => [
  335. 'REQUEST_METHOD' => 'POST',
  336. 'CONTENT_TYPE' => 'application/xml',
  337. ],
  338. 'input' => $xml,
  339. ]);
  340. $response = new Response();
  341. $next = function ($req, $res) {
  342. $this->assertEquals([], $req->getParsedBody());
  343. return $res;
  344. };
  345. $parser = new BodyParserMiddleware(['xml' => true]);
  346. $parser($request, $response, $next);
  347. }
  348. /**
  349. * test parsing fails will raise a bad request.
  350. *
  351. * @return void
  352. */
  353. public function testInvokeParseNoArray()
  354. {
  355. $request = new ServerRequest([
  356. 'environment' => [
  357. 'REQUEST_METHOD' => 'POST',
  358. 'CONTENT_TYPE' => 'application/json',
  359. ],
  360. 'input' => 'lol',
  361. ]);
  362. $response = new Response();
  363. $next = function ($req, $res) {
  364. return $res;
  365. };
  366. $this->expectException(BadRequestException::class);
  367. $parser = new BodyParserMiddleware();
  368. $parser($request, $response, $next);
  369. }
  370. }