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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Test Webapi Request model.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Webapi\Test\Unit\Rest;
class RequestTest extends \PHPUnit\Framework\TestCase
{
/**
* Request mock.
*
* @var \Magento\Framework\Webapi\Rest\Request
*/
protected $_request;
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/
protected $_cookieManagerMock;
/** @var \Magento\Framework\Webapi\Rest\Request\DeserializerFactory */
protected $_deserializerFactory;
protected function setUp()
{
/** Prepare mocks for request constructor arguments. */
$this->_deserializerFactory = $this->getMockBuilder(
\Magento\Framework\Webapi\Rest\Request\DeserializerFactory::class
)->setMethods(
['deserialize', 'get']
)->disableOriginalConstructor()->getMock();
$areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
$configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
$areaListMock->expects($this->once())->method('getFrontName')->will($this->returnValue('rest'));
/** Instantiate request. */
// TODO: Get rid of SUT mocks.
$this->_cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
$converterMock = $this->getMockBuilder(\Magento\Framework\Stdlib\StringUtils::class)
->disableOriginalConstructor()
->setMethods(['cleanString'])
->getMock();
$this->_request = $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Request::class)
->setMethods(['getHeader', 'getMethod', 'isGet', 'isPost', 'isPut', 'isDelete', 'getContent'])
->setConstructorArgs(
[
$this->_cookieManagerMock,
$converterMock,
$areaListMock,
$configScopeMock,
$this->_deserializerFactory
]
)
->getMock();
parent::setUp();
}
protected function tearDown()
{
unset($this->_deserializerFactory);
unset($this->_request);
parent::tearDown();
}
/**
* Test for getAcceptTypes() method.
*
* @dataProvider providerAcceptType
* @param string $acceptHeader Value of Accept HTTP header
* @param array $expectedResult Method call result
*/
public function testGetAcceptTypes($acceptHeader, $expectedResult)
{
$this->_request->expects(
$this->once()
)->method(
'getHeader'
)->with(
'Accept'
)->will(
$this->returnValue($acceptHeader)
);
$this->assertSame($expectedResult, $this->_request->getAcceptTypes());
}
/**
* Test for getBodyParams() method.
*/
public function testGetBodyParams()
{
$params = ['a' => 123, 'b' => 145];
$this->_prepareSutForGetBodyParamsTest($params);
$this->assertEquals($params, $this->_request->getBodyParams(), 'Body parameters were retrieved incorrectly.');
}
/**
* Prepare SUT for GetBodyParams() method mock.
*
* @param array $params
*/
protected function _prepareSutForGetBodyParamsTest($params)
{
$content = 'rawBody';
$this->_request->expects($this->exactly(2))->method('getContent')->will($this->returnValue($content));
$contentType = 'contentType';
$this->_request->expects(
$this->once()
)->method(
'getHeader'
)->with(
'Content-Type'
)->will(
$this->returnValue($contentType)
);
$deserializer = $this->getMockBuilder(
\Magento\Framework\Webapi\Rest\Request\Deserializer\Json::class
)->disableOriginalConstructor()->setMethods(
['deserialize']
)->getMock();
$deserializer->expects(
$this->once()
)->method(
'deserialize'
)->with(
$content
)->will(
$this->returnValue($params)
);
$this->_deserializerFactory->expects(
$this->once()
)->method(
'get'
)->with(
$contentType
)->will(
$this->returnValue($deserializer)
);
}
/**
* Test for getContentType() method.
*
* @dataProvider providerContentType
* @param string $contentTypeHeader 'Content-Type' header value
* @param string $contentType Appropriate content type for header value
* @param string|boolean $exceptionMessage \Exception message (boolean FALSE if exception is not expected)
*/
public function testGetContentType($contentTypeHeader, $contentType, $exceptionMessage = false)
{
$this->_request->expects(
$this->once()
)->method(
'getHeader'
)->with(
'Content-Type'
)->will(
$this->returnValue($contentTypeHeader)
);
try {
$this->assertEquals($contentType, $this->_request->getContentType());
} catch (\Magento\Framework\Exception\InputException $e) {
if ($exceptionMessage) {
$this->assertEquals(
$exceptionMessage,
$e->getMessage(),
'Exception message does not match the expected one.'
);
return;
} else {
$this->fail('Exception is thrown on valid header: ' . $e->getMessage());
}
}
if ($exceptionMessage) {
$this->fail('Expected exception was not raised.');
}
}
/**
* Data provider for testGetAcceptTypes().
*
* @return array
*/
public function providerAcceptType()
{
return [
// Each element is: array(Accept HTTP header value, expected result))
['', ['*/*']],
[
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
['text/html', 'application/xhtml+xml', 'application/xml', '*/*']
],
['text/html, application/*, text, */*', ['text/html', 'application/*', 'text', '*/*']],
[
'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp,' .
' image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
[
'text/html',
'application/xhtml+xml',
'image/png',
'image/webp',
'image/jpeg',
'image/gif',
'image/x-xbitmap',
'application/xml',
'*/*'
]
]
];
}
/**
* Data provider for testGetContentType().
*
* @return array
*/
public function providerContentType()
{
return [
// Each element is: array(Content-Type header value, content-type part[, expected exception message])
['', null, 'Content-Type header is empty.'],
['_?', null, 'Content-Type header is invalid.'],
['application/x-www-form-urlencoded; charset=UTF-8', 'application/x-www-form-urlencoded'],
['application/x-www-form-urlencoded; charset=utf-8', 'application/x-www-form-urlencoded'],
['text/html; charset=uTf-8', 'text/html'],
['text/html; charset=', null, 'Content-Type header is invalid.'],
['text/html;', null, 'Content-Type header is invalid.'],
['application/dialog.dot-info7+xml', 'application/dialog.dot-info7+xml'],
['application/x-www-form-urlencoded; charset=cp1251', null, 'UTF-8 is the only supported charset.']
];
}
}