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