-
Notifications
You must be signed in to change notification settings - Fork 8
/
ApiTestCase.php
330 lines (300 loc) · 10.7 KB
/
ApiTestCase.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* This file contains the ApiTestCase class.
*
*/
/**
* ApiTestCase is the base class for testing all RESTful API controller methods.
*
* Usage:
* <pre>
* $response = $this->post(
* 'api/login',
* array(
* 'username'=>'userName',
* 'password'=>'password'
* ),
* array(
* CURLOPT_CRLF=>true,
* 'cookies'=>array(
* 'giud'=>'value',
* ...
* )
* )
* );
* </pre>
*
* @package test
*
*/
class ApiTestCase extends CDbTestCase {
/**
* @var array authenticated cookies
*/
public $authCookies;
/**
* @var string authentication url
*/
public $loginUrl = 'login';
/**
* @var array data which will be sended for authentication.
*/
public $loginData = array('username'=>'admin', 'password'=>'admin');
/**
* Function authenticates in application and set $authCookies from response;
*
* @param boolean $reload need to reload cookies and not return from the cache.
*/
public function getAuthCookies($reload = false){
$this->authCookies = unserialize(Yii::app()->testdb->createCommand(
"SELECT PHPSESSID from auth_data"
)->queryScalar());
if($reload || empty($this->authCookies)){
$response = $this->post($this->loginUrl, $this->loginData);
$cookies = array();
foreach($response['cookies'] as $cookieName=>$cookieData){
$cookies[$cookieName] = $cookieData['value'];
}
$this->authCookies = $cookies;
Yii::app()->testdb->createCommand()->delete('auth_data');
Yii::app()->testdb->createCommand()->insert('auth_data', array('PHPSESSID'=>serialize($this->authCookies)));
}
return $this->authCookies;
}
/**
* Function creates report and save it in report folder
* @param mixed $data data to report
* @return void
*/
public function createReport($data){
ob_start();
if(is_string($data)){
echo($data);
}
else{
var_dump($data);
}
$data = ob_get_clean();
file_put_contents(dirname(__FILE__).'/../../assets/report.php', $data);
}
/**
* Function executes curl request with GET method.
*
* @param string $url URL
* @param array $params request GET params
* @param array $options additional request options
* @return array response
*/
public function get( $url, array $params = array(), array $options = array()){
$options[CURLOPT_CUSTOMREQUEST] = 'GET';
if(!empty($params)){
$url .= "?".$this->_encode($params);
}
return $this->request($url, $params, $options);
}
/**
* Function executes curl request with POST method.
*
* @param string $url URL
* @param array $params request parameters
* @param array $options additional request options
* @return array response
*/
public function post($url, array $params = array(), array $options = array())
{
$options[CURLOPT_CUSTOMREQUEST] = 'POST';
$options[CURLOPT_POSTFIELDS] = $params;
return $this->request($url, $params, $options);
}
/**
* Function executes curl request with PUT method.
*
* @param string $url URL
* @param array $params request parameters
* @param array $options additional request options
* @return array response
*/
public function put($url, array $params = array(), array $options = array())
{
$options[CURLOPT_CUSTOMREQUEST] = 'PUT';
$options[CURLOPT_POSTFIELDS] = $params;
return $this->request($url, $params, $options);
}
/**
* Function executes curl request with DELETE method.
*
* @param string $url URL
* @param array $params request parameters
* @param array $options additional request options
* @return array response
*/
public function delete($url, array $params = array(), array $options = array())
{
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
$options[CURLOPT_POSTFIELDS] = $params;
return $this->request($url, $params, $options);
}
/**
* Function executes curl request
*
* @param string $url URL
* @param array $params request parameters
* @param array $options additional request options
* @return array response
* <pre>
* array{
* 'body' => "response content",
* 'code' => 200,
* 'location' => 'http://www.example.org/index.php',
* 'cookies' => array(...)
* 'headers' => array(...)
* }
* </pre>
*/
private function request($url, array $params=array(), array $options=array()){
$url = rtrim(TEST_BASE_URL,'/').'/'.trim($url, '/');
$ch = curl_init($url);
$defaultOptions = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 1,
);
if (isset($options[CURLOPT_POSTFIELDS])) {
$options[CURLOPT_POSTFIELDS] = $this->_encode($options[CURLOPT_POSTFIELDS]);
}
if(isset($options['cookies'])){
$cookiesStringToPass = "";
foreach ($options['cookies'] as $name=>$value) {
if ($cookiesStringToPass) {
$cookiesStringToPass .= ';';
}
$cookiesStringToPass .= $name . '=' . addslashes($value);
}
$options[CURLOPT_COOKIE] = $cookiesStringToPass;
unset($options['cookies']);
}
$options = $options + $defaultOptions;
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$header = substr($response, 0, $info['header_size']);
$body = substr($response, $info['header_size']);
preg_match("/Location: (.*?)\n/", $header, $matches);
$location = isset($matches[1]) ? $matches[1] : null;
$headersArray = ($this->http_parse_headers( $header ));
$cookies = isset($headersArray['Set-Cookie'])?$headersArray['Set-Cookie'] : array();
curl_close($ch);
return array(
'body' => $body,
'code' => $info['http_code'],
'location' => $location,
'cookies' => $cookies,
'headers' => $headersArray,
'decoded' => json_decode($body, true),
);
}
/**
* Function generate URL-encoded query string from array
*
* @param array $params query parameters
* @return string
*/
private function _encode(array $params)
{
return http_build_query($params, null, '&');
}
/**
* Function convert HTTP headers to array
* @param string $header
* @return array HTTP headers
* <br>
* Example:
* <pre>
* array(
* 'Date' =>" Tue, 18 Mar 2014 11:50:12 GMT",
* 'Server' => "Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9",
* 'X-Powered-By' =>"PHP/5.5.9",
* 'Set-Cookie' => array(
* 'PHPSESSID' =>array(
* 'value' => "ou85gd41ad8rlgme12dps41ki4",
* 'path' => "/"
* )
* 'guid' => array(
* 'value' => "53283b69df9cc8.91971156",
* 'expires' => 1397737577,
* 'Max-Age' => "2592000",
* 'path' => "/",
* )
* ),
* 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
* 'Cache-Control' =>"no-store, no-cache, must-revalidate, post-check=0, pre-check=0",
* 'Pragma' =>"no-cache",
* 'Content-Length' =>"204",
* 'Content-Type' =>"application/json",
* )
* </pre>
*/
private function http_parse_headers( $header )
{
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $i => $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m){return strtoupper($m[0]);}, strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
if(!is_array($retVal[$match[1]])){
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
}
else{
$retVal[$match[1]][] = trim($match[2]);
}
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
$cookies = array();
if(isset($retVal['Set-Cookie'])){
if(is_string($retVal['Set-Cookie'])){
$retVal['Set-Cookie'] = array($retVal['Set-Cookie']);
}
foreach($retVal['Set-Cookie'] as $cookie){
$cookiesParts = explode(';', trim($cookie));
foreach($cookiesParts as $cookiesPartNum=>$cookiesPart){
$cookiesValues = explode('=', trim($cookiesPart));
if($cookiesPartNum == 0){
$cookieName = $cookiesValues[0];
$cookies[$cookieName] = array();
}
$aTmp = array();
if (count($cookiesValues) == 2)
{
switch ($cookiesValues[0])
{
case 'path':
case 'domain':
$aTmp['name'] = trim($cookiesValues[0]);
$aTmp['value'] = urldecode(trim($cookiesValues[1]));
break;
case 'expires':
$aTmp['name'] = trim($cookiesValues[0]);
$aTmp['value'] = strtotime(urldecode(trim($cookiesValues[1])));
break;
default:
$aTmp['name'] = trim($cookiesValues[0]);
$aTmp['value'] = trim($cookiesValues[1]);
break;
}
}
if($cookiesPartNum == 0){
$cookies[$cookieName]['value'] = $aTmp['value'];
}
else{
$cookies[$cookieName][$aTmp['name']] = $aTmp['value'];
}
}
}
$retVal['Set-Cookie'] = $cookies;
}
return $retVal;
}
}