RequestTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Test Webapi Request model.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Webapi\Test\Unit\Rest;
  9. class RequestTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * Request mock.
  13. *
  14. * @var \Magento\Framework\Webapi\Rest\Request
  15. */
  16. protected $_request;
  17. /**
  18. * @var \Magento\Framework\Stdlib\CookieManagerInterface
  19. */
  20. protected $_cookieManagerMock;
  21. /** @var \Magento\Framework\Webapi\Rest\Request\DeserializerFactory */
  22. protected $_deserializerFactory;
  23. protected function setUp()
  24. {
  25. /** Prepare mocks for request constructor arguments. */
  26. $this->_deserializerFactory = $this->getMockBuilder(
  27. \Magento\Framework\Webapi\Rest\Request\DeserializerFactory::class
  28. )->setMethods(
  29. ['deserialize', 'get']
  30. )->disableOriginalConstructor()->getMock();
  31. $areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
  32. $configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
  33. $areaListMock->expects($this->once())->method('getFrontName')->will($this->returnValue('rest'));
  34. /** Instantiate request. */
  35. // TODO: Get rid of SUT mocks.
  36. $this->_cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
  37. $converterMock = $this->getMockBuilder(\Magento\Framework\Stdlib\StringUtils::class)
  38. ->disableOriginalConstructor()
  39. ->setMethods(['cleanString'])
  40. ->getMock();
  41. $this->_request = $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Request::class)
  42. ->setMethods(['getHeader', 'getMethod', 'isGet', 'isPost', 'isPut', 'isDelete', 'getContent'])
  43. ->setConstructorArgs(
  44. [
  45. $this->_cookieManagerMock,
  46. $converterMock,
  47. $areaListMock,
  48. $configScopeMock,
  49. $this->_deserializerFactory
  50. ]
  51. )
  52. ->getMock();
  53. parent::setUp();
  54. }
  55. protected function tearDown()
  56. {
  57. unset($this->_deserializerFactory);
  58. unset($this->_request);
  59. parent::tearDown();
  60. }
  61. /**
  62. * Test for getAcceptTypes() method.
  63. *
  64. * @dataProvider providerAcceptType
  65. * @param string $acceptHeader Value of Accept HTTP header
  66. * @param array $expectedResult Method call result
  67. */
  68. public function testGetAcceptTypes($acceptHeader, $expectedResult)
  69. {
  70. $this->_request->expects(
  71. $this->once()
  72. )->method(
  73. 'getHeader'
  74. )->with(
  75. 'Accept'
  76. )->will(
  77. $this->returnValue($acceptHeader)
  78. );
  79. $this->assertSame($expectedResult, $this->_request->getAcceptTypes());
  80. }
  81. /**
  82. * Test for getBodyParams() method.
  83. */
  84. public function testGetBodyParams()
  85. {
  86. $params = ['a' => 123, 'b' => 145];
  87. $this->_prepareSutForGetBodyParamsTest($params);
  88. $this->assertEquals($params, $this->_request->getBodyParams(), 'Body parameters were retrieved incorrectly.');
  89. }
  90. /**
  91. * Prepare SUT for GetBodyParams() method mock.
  92. *
  93. * @param array $params
  94. */
  95. protected function _prepareSutForGetBodyParamsTest($params)
  96. {
  97. $content = 'rawBody';
  98. $this->_request->expects($this->exactly(2))->method('getContent')->will($this->returnValue($content));
  99. $contentType = 'contentType';
  100. $this->_request->expects(
  101. $this->once()
  102. )->method(
  103. 'getHeader'
  104. )->with(
  105. 'Content-Type'
  106. )->will(
  107. $this->returnValue($contentType)
  108. );
  109. $deserializer = $this->getMockBuilder(
  110. \Magento\Framework\Webapi\Rest\Request\Deserializer\Json::class
  111. )->disableOriginalConstructor()->setMethods(
  112. ['deserialize']
  113. )->getMock();
  114. $deserializer->expects(
  115. $this->once()
  116. )->method(
  117. 'deserialize'
  118. )->with(
  119. $content
  120. )->will(
  121. $this->returnValue($params)
  122. );
  123. $this->_deserializerFactory->expects(
  124. $this->once()
  125. )->method(
  126. 'get'
  127. )->with(
  128. $contentType
  129. )->will(
  130. $this->returnValue($deserializer)
  131. );
  132. }
  133. /**
  134. * Test for getContentType() method.
  135. *
  136. * @dataProvider providerContentType
  137. * @param string $contentTypeHeader 'Content-Type' header value
  138. * @param string $contentType Appropriate content type for header value
  139. * @param string|boolean $exceptionMessage \Exception message (boolean FALSE if exception is not expected)
  140. */
  141. public function testGetContentType($contentTypeHeader, $contentType, $exceptionMessage = false)
  142. {
  143. $this->_request->expects(
  144. $this->once()
  145. )->method(
  146. 'getHeader'
  147. )->with(
  148. 'Content-Type'
  149. )->will(
  150. $this->returnValue($contentTypeHeader)
  151. );
  152. try {
  153. $this->assertEquals($contentType, $this->_request->getContentType());
  154. } catch (\Magento\Framework\Exception\InputException $e) {
  155. if ($exceptionMessage) {
  156. $this->assertEquals(
  157. $exceptionMessage,
  158. $e->getMessage(),
  159. 'Exception message does not match the expected one.'
  160. );
  161. return;
  162. } else {
  163. $this->fail('Exception is thrown on valid header: ' . $e->getMessage());
  164. }
  165. }
  166. if ($exceptionMessage) {
  167. $this->fail('Expected exception was not raised.');
  168. }
  169. }
  170. /**
  171. * Data provider for testGetAcceptTypes().
  172. *
  173. * @return array
  174. */
  175. public function providerAcceptType()
  176. {
  177. return [
  178. // Each element is: array(Accept HTTP header value, expected result))
  179. ['', ['*/*']],
  180. [
  181. 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  182. ['text/html', 'application/xhtml+xml', 'application/xml', '*/*']
  183. ],
  184. ['text/html, application/*, text, */*', ['text/html', 'application/*', 'text', '*/*']],
  185. [
  186. 'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp,' .
  187. ' image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
  188. [
  189. 'text/html',
  190. 'application/xhtml+xml',
  191. 'image/png',
  192. 'image/webp',
  193. 'image/jpeg',
  194. 'image/gif',
  195. 'image/x-xbitmap',
  196. 'application/xml',
  197. '*/*'
  198. ]
  199. ]
  200. ];
  201. }
  202. /**
  203. * Data provider for testGetContentType().
  204. *
  205. * @return array
  206. */
  207. public function providerContentType()
  208. {
  209. return [
  210. // Each element is: array(Content-Type header value, content-type part[, expected exception message])
  211. ['', null, 'Content-Type header is empty.'],
  212. ['_?', null, 'Content-Type header is invalid.'],
  213. ['application/x-www-form-urlencoded; charset=UTF-8', 'application/x-www-form-urlencoded'],
  214. ['application/x-www-form-urlencoded; charset=utf-8', 'application/x-www-form-urlencoded'],
  215. ['text/html; charset=uTf-8', 'text/html'],
  216. ['text/html; charset=', null, 'Content-Type header is invalid.'],
  217. ['text/html;', null, 'Content-Type header is invalid.'],
  218. ['application/dialog.dot-info7+xml', 'application/dialog.dot-info7+xml'],
  219. ['application/x-www-form-urlencoded; charset=cp1251', null, 'UTF-8 is the only supported charset.']
  220. ];
  221. }
  222. }