RequestTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Test WebAPI authentication helper.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Oauth\Test\Unit\Helper;
  9. use Magento\Framework\App\Request\Http;
  10. use Magento\Framework\Phrase;
  11. class RequestTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Framework\Oauth\Helper\Request */
  14. protected $oauthRequestHelper;
  15. /** @var \Magento\Framework\App\Response\Http */
  16. protected $response;
  17. /**
  18. * @return void
  19. */
  20. protected function setUp()
  21. {
  22. $this->oauthRequestHelper = new \Magento\Framework\Oauth\Helper\Request();
  23. $this->response =
  24. $this->createPartialMock(\Magento\Framework\HTTP\PhpEnvironment\Response::class, ['setHttpResponseCode']);
  25. }
  26. /**
  27. * @return void
  28. */
  29. protected function tearDown()
  30. {
  31. unset($this->oauthRequestHelper, $this->response);
  32. }
  33. /**
  34. * @param \Exception $exception
  35. * @param array $expected
  36. * @return void
  37. * @dataProvider dataProviderForPrepareErrorResponseTest
  38. */
  39. public function testPrepareErrorResponse($exception, $expected)
  40. {
  41. $this->response
  42. ->expects($this->once())
  43. ->method('setHttpResponseCode')
  44. ->with($expected[1]);
  45. $errorResponse = $this->oauthRequestHelper->prepareErrorResponse($exception, $this->response);
  46. $this->assertEquals(['oauth_problem' => $expected[0]], $errorResponse);
  47. }
  48. /**
  49. * @return array
  50. */
  51. public function dataProviderForPrepareErrorResponseTest()
  52. {
  53. return [
  54. [
  55. new \Magento\Framework\Oauth\OauthInputException(new Phrase('msg')),
  56. ['msg', \Magento\Framework\Oauth\Helper\Request::HTTP_BAD_REQUEST],
  57. ],
  58. [
  59. new \Exception('msg'),
  60. ['internal_error&message=msg', \Magento\Framework\Oauth\Helper\Request::HTTP_INTERNAL_ERROR]
  61. ],
  62. [
  63. new \Exception(),
  64. [
  65. 'internal_error&message=empty_message',
  66. \Magento\Framework\Oauth\Helper\Request::HTTP_INTERNAL_ERROR
  67. ]
  68. ]
  69. ];
  70. }
  71. /**
  72. * @param string $url
  73. * @param string $host
  74. * @return void
  75. * @dataProvider hostsDataProvider
  76. */
  77. public function testGetRequestUrl($url, $host)
  78. {
  79. $httpRequestMock = $this->createPartialMock(
  80. \Magento\Framework\App\Request\Http::class,
  81. ['getHttpHost', 'getScheme', 'getRequestUri']
  82. );
  83. $httpRequestMock->expects($this->any())->method('getHttpHost')->will($this->returnValue($host));
  84. $httpRequestMock->expects($this->any())->method('getScheme')->will($this->returnValue('http'));
  85. $httpRequestMock->expects($this->any())->method('getRequestUri')->will($this->returnValue('/'));
  86. $this->assertEquals($url, $this->oauthRequestHelper->getRequestUrl($httpRequestMock));
  87. }
  88. /**
  89. * @return array
  90. */
  91. public function hostsDataProvider()
  92. {
  93. return [
  94. 'hostWithoutPort' => [
  95. 'url' => 'http://localhost/',
  96. 'host' => 'localhost'
  97. ],
  98. 'hostWithPort' => [
  99. 'url' => 'http://localhost:81/',
  100. 'host' => 'localhost:81'
  101. ]
  102. ];
  103. }
  104. /**
  105. * Test that the OAuth parameters are correctly extracted from the Authorization header.
  106. *
  107. * @param $authHeaderValue
  108. * @param $expectedParams
  109. * @dataProvider dataProviderForTestPrepareRequestOAuthHeader
  110. */
  111. public function testPrepareRequestOAuthHeader($authHeaderValue, $expectedParams)
  112. {
  113. $httpRequestMock = $this->getMockBuilder(Http::class)
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $httpRequestMock->expects($this->once())->method('getScheme')->willReturn('https');
  117. $httpRequestMock->expects($this->once())->method('getHttpHost')->willReturn('example.com');
  118. $httpRequestMock->expects($this->once())->method('getRequestUri')->willReturn('/');
  119. $httpRequestMock->expects($this->any())
  120. ->method('getHeader')
  121. ->willReturnCallback(function ($header) use ($authHeaderValue) {
  122. switch ($header) {
  123. case 'Authorization':
  124. return $authHeaderValue;
  125. case \Zend_Http_Client::CONTENT_TYPE:
  126. return \Zend_Http_Client::ENC_URLENCODED;
  127. default:
  128. return null;
  129. }
  130. });
  131. $this->assertEquals($expectedParams, $this->oauthRequestHelper->prepareRequest($httpRequestMock));
  132. }
  133. /**
  134. * @return array
  135. */
  136. public function dataProviderForTestPrepareRequestOAuthHeader()
  137. {
  138. return [
  139. [
  140. null,
  141. []
  142. ],
  143. [
  144. '',
  145. []
  146. ],
  147. [
  148. 'OAuth oauth_consumer_key="x",oauth_token="x", Basic d2luZHNvcm0yOldpTmRzb1JTbWlUSDAwMTQ=',
  149. ['oauth_consumer_key' => 'x', 'oauth_token' => 'x']
  150. ],
  151. [
  152. 'Basic d2luZHNvcm0yOldpTmRzb1JTbWlUSDAwMTQ=, OAuth oauth_consumer_key="x",oauth_token="x"',
  153. ['oauth_consumer_key' => 'x', 'oauth_token' => 'x']
  154. ],
  155. [
  156. 'Basic d2luZHNvcm0yOldpTmRzb1JTbWlUSDAwMTQ=, oauth oauth_consumer_key="x", oauth_token="x"',
  157. ['oauth_consumer_key' => 'x', 'oauth_token' => 'x']
  158. ],
  159. [
  160. 'oauth oauth_consumer_key="x", oauth_token="x", Basic d2luZHNvcm0yOldpTmRzb1JTbWlUSDAwMTQ=',
  161. ['oauth_consumer_key' => 'x', 'oauth_token' => 'x']
  162. ]
  163. ];
  164. }
  165. }