DeserializationTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi;
  7. use Magento\TestFramework\TestCase\Webapi\Adapter\Rest\RestClient;
  8. class DeserializationTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  9. {
  10. /**
  11. * @var string
  12. */
  13. protected $_version;
  14. /**
  15. * @var string
  16. */
  17. protected $_restResourcePath;
  18. protected function setUp()
  19. {
  20. $this->_version = 'V1';
  21. $this->_restResourcePath = "/{$this->_version}/TestModule5/";
  22. }
  23. /**
  24. * Test POST request with empty body
  25. */
  26. public function testPostRequestWithEmptyBody()
  27. {
  28. $this->_markTestAsRestOnly();
  29. $serviceInfo = [
  30. 'rest' => [
  31. 'resourcePath' => $this->_restResourcePath,
  32. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  33. ],
  34. ];
  35. $expectedMessage =
  36. '{"message":"\"%fieldName\" is required. Enter and try again.","parameters":{"fieldName":"item"}}';
  37. try {
  38. $this->_webApiCall($serviceInfo, RestClient::EMPTY_REQUEST_BODY);
  39. } catch (\Exception $e) {
  40. $this->assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
  41. $this->assertContains(
  42. $expectedMessage,
  43. $e->getMessage(),
  44. "Response does not contain expected message."
  45. );
  46. }
  47. }
  48. /**
  49. * Test PUT request with empty body
  50. */
  51. public function testPutRequestWithEmptyBody()
  52. {
  53. $this->_markTestAsRestOnly();
  54. $itemId = 1;
  55. $serviceInfo = [
  56. 'rest' => [
  57. 'resourcePath' => $this->_restResourcePath . $itemId,
  58. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  59. ],
  60. ];
  61. $expectedMessage =
  62. '{"message":"\"%fieldName\" is required. Enter and try again.","parameters":{"fieldName":"entityItem"}}';
  63. try {
  64. $this->_webApiCall($serviceInfo, RestClient::EMPTY_REQUEST_BODY);
  65. } catch (\Exception $e) {
  66. $this->assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
  67. $this->assertContains(
  68. $expectedMessage,
  69. $e->getMessage(),
  70. "Response does not contain expected message."
  71. );
  72. }
  73. }
  74. }