forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebApiExecutor.php
144 lines (132 loc) · 3.54 KB
/
WebApiExecutor.php
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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\DataTransport;
use Magento\FunctionalTestingFramework\Exceptions\FastFailException;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Util\MftfGlobals;
use Magento\FunctionalTestingFramework\DataTransport\Protocol\CurlInterface;
use Magento\FunctionalTestingFramework\DataTransport\Protocol\CurlTransport;
use Magento\FunctionalTestingFramework\DataTransport\Auth\WebApiAuth;
/**
* Curl executor for Magento Web Api requests.
*/
class WebApiExecutor implements CurlInterface
{
/**
* Curl transport protocol
*
* @var CurlTransport
*/
private $transport;
/**
* Rest request headers
*
* @var string[]
*/
private $headers = [
'Accept: application/json',
'Content-Type: application/json',
];
/**
* Store code in API request
*
* @var string
*/
private $storeCode;
/**
* WebApiExecutor Constructor
*
* @param string $storeCode
* @throws FastFailException
*/
public function __construct($storeCode = null)
{
$this->storeCode = $storeCode;
$this->transport = new CurlTransport();
$this->authorize();
}
/**
* Acquire and store the authorization token needed for REST requests
*
* @return void
* @throws FastFailException
*/
protected function authorize()
{
$this->headers = array_merge(
['Authorization: Bearer ' . WebApiAuth::getAdminToken()],
$this->headers
);
}
/**
* Send request to the remote server.
*
* @param string $url
* @param array $data
* @param string $method
* @param array $headers
* @return void
* @throws TestFrameworkException
*/
public function write($url, $data = [], $method = CurlInterface::POST, $headers = [])
{
$this->transport->write(
$this->getFormattedUrl($url),
json_encode($data, JSON_PRETTY_PRINT),
$method,
array_unique(array_merge($headers, $this->headers))
);
}
/**
* Read response from server.
*
* @param string $successRegex
* @param string $returnRegex
* @param string|null $returnIndex
* @return string
* @throws TestFrameworkException
*/
public function read($successRegex = null, $returnRegex = null, $returnIndex = null)
{
return $this->transport->read();
}
/**
* Add additional option to cURL.
*
* @param integer $option CURLOPT_* constants.
* @param integer|string|boolean|array $value
* @return void
*/
public function addOption($option, $value)
{
$this->transport->addOption($option, $value);
}
/**
* Close the connection to the server.
*
* @return void
*/
public function close()
{
$this->transport->close();
}
/**
* Builds and returns URL for request, appending storeCode if needed
*
* @param string $resource
* @return string
* @throws TestFrameworkException
*/
protected function getFormattedUrl($resource)
{
$urlResult = MftfGlobals::getWebApiBaseUrl();
if ($this->storeCode !== null) {
$urlResult .= $this->storeCode . '/';
}
$urlResult .= trim($resource, '/');
return $urlResult;
}
}