commit | author | age
|
2207d6
|
1 |
# REST |
W |
2 |
|
|
3 |
|
|
4 |
Module for testing REST WebService. |
|
5 |
|
|
6 |
This module can be used either with frameworks or PHPBrowser. |
|
7 |
If a framework module is connected, the testing will occur in the application directly. |
|
8 |
Otherwise, a PHPBrowser should be specified as a dependency to send requests and receive responses from a server. |
|
9 |
|
|
10 |
## Configuration |
|
11 |
|
|
12 |
* url *optional* - the url of api |
|
13 |
|
|
14 |
This module requires PHPBrowser or any of Framework modules enabled. |
|
15 |
|
|
16 |
### Example |
|
17 |
|
|
18 |
modules: |
|
19 |
enabled: |
|
20 |
- REST: |
|
21 |
depends: PhpBrowser |
|
22 |
url: 'http://serviceapp/api/v1/' |
|
23 |
|
|
24 |
## Public Properties |
|
25 |
|
|
26 |
* headers - array of headers going to be sent. |
|
27 |
* params - array of sent data |
|
28 |
* response - last response (string) |
|
29 |
|
|
30 |
## Parts |
|
31 |
|
|
32 |
* Json - actions for validating Json responses (no Xml responses) |
|
33 |
* Xml - actions for validating XML responses (no Json responses) |
|
34 |
|
|
35 |
## Conflicts |
|
36 |
|
|
37 |
Conflicts with SOAP module |
|
38 |
|
|
39 |
|
|
40 |
## Actions |
|
41 |
|
|
42 |
### amAWSAuthenticated |
|
43 |
|
|
44 |
Allows to send REST request using AWS Authorization |
|
45 |
|
|
46 |
Only works with PhpBrowser |
|
47 |
Example Config: |
|
48 |
```yml |
|
49 |
modules: |
|
50 |
enabled: |
|
51 |
- REST: |
|
52 |
aws: |
|
53 |
key: accessKey |
|
54 |
secret: accessSecret |
|
55 |
service: awsService |
|
56 |
region: awsRegion |
|
57 |
``` |
|
58 |
Code: |
|
59 |
```php |
|
60 |
<?php |
|
61 |
$I->amAWSAuthenticated(); |
|
62 |
?> |
|
63 |
``` |
|
64 |
* `param array` $additionalAWSConfig |
|
65 |
@throws ModuleException |
|
66 |
|
|
67 |
|
|
68 |
### amBearerAuthenticated |
|
69 |
|
|
70 |
Adds Bearer authentication via access token. |
|
71 |
|
|
72 |
* `param` $accessToken |
|
73 |
* `[Part]` json |
|
74 |
* `[Part]` xml |
|
75 |
|
|
76 |
|
|
77 |
### amDigestAuthenticated |
|
78 |
|
|
79 |
Adds Digest authentication via username/password. |
|
80 |
|
|
81 |
* `param` $username |
|
82 |
* `param` $password |
|
83 |
* `[Part]` json |
|
84 |
* `[Part]` xml |
|
85 |
|
|
86 |
|
|
87 |
### amHttpAuthenticated |
|
88 |
|
|
89 |
Adds HTTP authentication via username/password. |
|
90 |
|
|
91 |
* `param` $username |
|
92 |
* `param` $password |
|
93 |
* `[Part]` json |
|
94 |
* `[Part]` xml |
|
95 |
|
|
96 |
|
|
97 |
### amNTLMAuthenticated |
|
98 |
|
|
99 |
Adds NTLM authentication via username/password. |
|
100 |
Requires client to be Guzzle >=6.3.0 |
|
101 |
Out of scope for functional modules. |
|
102 |
|
|
103 |
Example: |
|
104 |
```php |
|
105 |
<?php |
|
106 |
$I->amNTLMAuthenticated('jon_snow', 'targaryen'); |
|
107 |
?> |
|
108 |
``` |
|
109 |
|
|
110 |
* `param` $username |
|
111 |
* `param` $password |
|
112 |
@throws ModuleException |
|
113 |
* `[Part]` json |
|
114 |
* `[Part]` xml |
|
115 |
|
|
116 |
|
|
117 |
### deleteHeader |
|
118 |
|
|
119 |
Deletes the header with the passed name. Subsequent requests |
|
120 |
will not have the deleted header in its request. |
|
121 |
|
|
122 |
Example: |
|
123 |
```php |
|
124 |
<?php |
|
125 |
$I->haveHttpHeader('X-Requested-With', 'Codeception'); |
|
126 |
$I->sendGET('test-headers.php'); |
|
127 |
// ... |
|
128 |
$I->deleteHeader('X-Requested-With'); |
|
129 |
$I->sendPOST('some-other-page.php'); |
|
130 |
?> |
|
131 |
``` |
|
132 |
|
|
133 |
* `param string` $name the name of the header to delete. |
|
134 |
* `[Part]` json |
|
135 |
* `[Part]` xml |
|
136 |
|
|
137 |
|
|
138 |
### dontSeeBinaryResponseEquals |
|
139 |
|
|
140 |
Checks if the hash of a binary response is not the same as provided. |
|
141 |
|
|
142 |
```php |
|
143 |
<?php |
|
144 |
$I->dontSeeBinaryResponseEquals("8c90748342f19b195b9c6b4eff742ded"); |
|
145 |
?> |
|
146 |
``` |
|
147 |
Opposite to `seeBinaryResponseEquals` |
|
148 |
|
|
149 |
* `param` $hash the hashed data response expected |
|
150 |
* `param` $algo the hash algorithm to use. Default md5. |
|
151 |
* `[Part]` json |
|
152 |
* `[Part]` xml |
|
153 |
|
|
154 |
|
|
155 |
### dontSeeHttpHeader |
|
156 |
|
|
157 |
Checks over the given HTTP header and (optionally) |
|
158 |
its value, asserting that are not there |
|
159 |
|
|
160 |
* `param` $name |
|
161 |
* `param` $value |
|
162 |
* `[Part]` json |
|
163 |
* `[Part]` xml |
|
164 |
|
|
165 |
|
|
166 |
### dontSeeResponseCodeIs |
|
167 |
|
|
168 |
Checks that response code is not equal to provided value. |
|
169 |
|
|
170 |
```php |
|
171 |
<?php |
|
172 |
$I->dontSeeResponseCodeIs(200); |
|
173 |
|
|
174 |
// preferred to use \Codeception\Util\HttpCode |
|
175 |
$I->dontSeeResponseCodeIs(\Codeception\Util\HttpCode::OK); |
|
176 |
``` |
|
177 |
|
|
178 |
* `[Part]` json |
|
179 |
* `[Part]` xml |
|
180 |
* `param` $code |
|
181 |
|
|
182 |
|
|
183 |
### dontSeeResponseContains |
|
184 |
|
|
185 |
Checks whether last response do not contain text. |
|
186 |
|
|
187 |
* `param` $text |
|
188 |
* `[Part]` json |
|
189 |
* `[Part]` xml |
|
190 |
|
|
191 |
|
|
192 |
### dontSeeResponseContainsJson |
|
193 |
|
|
194 |
Opposite to seeResponseContainsJson |
|
195 |
|
|
196 |
* `[Part]` json |
|
197 |
* `param array` $json |
|
198 |
|
|
199 |
|
|
200 |
### dontSeeResponseJsonMatchesJsonPath |
|
201 |
|
|
202 |
Opposite to seeResponseJsonMatchesJsonPath |
|
203 |
|
|
204 |
* `param string` $jsonPath |
|
205 |
* `[Part]` json |
|
206 |
|
|
207 |
|
|
208 |
### dontSeeResponseJsonMatchesXpath |
|
209 |
|
|
210 |
Opposite to seeResponseJsonMatchesXpath |
|
211 |
|
|
212 |
* `param string` $xpath |
|
213 |
* `[Part]` json |
|
214 |
|
|
215 |
|
|
216 |
### dontSeeResponseMatchesJsonType |
|
217 |
|
|
218 |
Opposite to `seeResponseMatchesJsonType`. |
|
219 |
|
|
220 |
* `[Part]` json |
|
221 |
@see seeResponseMatchesJsonType |
|
222 |
* `param` $jsonType jsonType structure |
|
223 |
* `param null` $jsonPath optionally set specific path to structure with JsonPath |
|
224 |
* `Available since` 2.1.3 |
|
225 |
|
|
226 |
|
|
227 |
### dontSeeXmlResponseEquals |
|
228 |
|
|
229 |
Checks XML response does not equal to provided XML. |
|
230 |
Comparison is done by canonicalizing both xml`s. |
|
231 |
|
|
232 |
Parameter can be passed either as XmlBuilder, DOMDocument, DOMNode, XML string, or array (if no attributes). |
|
233 |
|
|
234 |
* `param` $xml |
|
235 |
* `[Part]` xml |
|
236 |
|
|
237 |
|
|
238 |
### dontSeeXmlResponseIncludes |
|
239 |
|
|
240 |
Checks XML response does not include provided XML. |
|
241 |
Comparison is done by canonicalizing both xml`s. |
|
242 |
Parameter can be passed either as XmlBuilder, DOMDocument, DOMNode, XML string, or array (if no attributes). |
|
243 |
|
|
244 |
* `param` $xml |
|
245 |
* `[Part]` xml |
|
246 |
|
|
247 |
|
|
248 |
### dontSeeXmlResponseMatchesXpath |
|
249 |
|
|
250 |
Checks whether XML response does not match XPath |
|
251 |
|
|
252 |
```php |
|
253 |
<?php |
|
254 |
$I->dontSeeXmlResponseMatchesXpath('//root/user[@id=1]'); |
|
255 |
``` |
|
256 |
* `[Part]` xml |
|
257 |
* `param` $xpath |
|
258 |
|
|
259 |
|
|
260 |
### grabAttributeFromXmlElement |
|
261 |
|
|
262 |
Finds and returns attribute of element. |
|
263 |
Element is matched by either CSS or XPath |
|
264 |
|
|
265 |
* `param` $cssOrXPath |
|
266 |
* `param` $attribute |
|
267 |
* `return` string |
|
268 |
* `[Part]` xml |
|
269 |
|
|
270 |
|
|
271 |
### grabDataFromJsonResponse |
|
272 |
|
|
273 |
Deprecated since 2.0.9 and removed since 2.1.0 |
|
274 |
|
|
275 |
* `param` $path |
|
276 |
@throws ModuleException |
|
277 |
@deprecated |
|
278 |
|
|
279 |
|
|
280 |
### grabDataFromResponseByJsonPath |
|
281 |
|
|
282 |
Returns data from the current JSON response using [JSONPath](http://goessner.net/articles/JsonPath/) as selector. |
|
283 |
JsonPath is XPath equivalent for querying Json structures. |
|
284 |
Try your JsonPath expressions [online](http://jsonpath.curiousconcept.com/). |
|
285 |
Even for a single value an array is returned. |
|
286 |
|
|
287 |
This method **require [`flow/jsonpath` > 0.2](https://github.com/FlowCommunications/JSONPath/) library to be installed**. |
|
288 |
|
|
289 |
Example: |
|
290 |
|
|
291 |
``` php |
|
292 |
<?php |
|
293 |
// match the first `user.id` in json |
|
294 |
$firstUserId = $I->grabDataFromResponseByJsonPath('$..users[0].id'); |
|
295 |
$I->sendPUT('/user', array('id' => $firstUserId[0], 'name' => 'davert')); |
|
296 |
?> |
|
297 |
``` |
|
298 |
|
|
299 |
* `param string` $jsonPath |
|
300 |
* `return` array Array of matching items |
|
301 |
* `Available since` 2.0.9 |
|
302 |
@throws \Exception |
|
303 |
* `[Part]` json |
|
304 |
|
|
305 |
|
|
306 |
### grabHttpHeader |
|
307 |
|
|
308 |
Returns the value of the specified header name |
|
309 |
|
|
310 |
* `param` $name |
|
311 |
* `param Boolean` $first Whether to return the first value or all header values |
|
312 |
|
|
313 |
* `return string|array The first header value if` $first is true, an array of values otherwise |
|
314 |
* `[Part]` json |
|
315 |
* `[Part]` xml |
|
316 |
|
|
317 |
|
|
318 |
### grabResponse |
|
319 |
|
|
320 |
Returns current response so that it can be used in next scenario steps. |
|
321 |
|
|
322 |
Example: |
|
323 |
|
|
324 |
``` php |
|
325 |
<?php |
|
326 |
$user_id = $I->grabResponse(); |
|
327 |
$I->sendPUT('/user', array('id' => $user_id, 'name' => 'davert')); |
|
328 |
?> |
|
329 |
``` |
|
330 |
|
|
331 |
* `Available since` 1.1 |
|
332 |
* `return` string |
|
333 |
* `[Part]` json |
|
334 |
* `[Part]` xml |
|
335 |
|
|
336 |
|
|
337 |
### grabTextContentFromXmlElement |
|
338 |
|
|
339 |
Finds and returns text contents of element. |
|
340 |
Element is matched by either CSS or XPath |
|
341 |
|
|
342 |
* `param` $cssOrXPath |
|
343 |
* `return` string |
|
344 |
* `[Part]` xml |
|
345 |
|
|
346 |
|
|
347 |
### haveHttpHeader |
|
348 |
|
|
349 |
Sets HTTP header valid for all next requests. Use `deleteHeader` to unset it |
|
350 |
|
|
351 |
```php |
|
352 |
<?php |
|
353 |
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
354 |
// all next requests will contain this header |
|
355 |
?> |
|
356 |
``` |
|
357 |
|
|
358 |
* `param` $name |
|
359 |
* `param` $value |
|
360 |
* `[Part]` json |
|
361 |
* `[Part]` xml |
|
362 |
|
|
363 |
|
|
364 |
### seeBinaryResponseEquals |
|
365 |
|
|
366 |
Checks if the hash of a binary response is exactly the same as provided. |
|
367 |
Parameter can be passed as any hash string supported by hash(), with an |
|
368 |
optional second parameter to specify the hash type, which defaults to md5. |
|
369 |
|
|
370 |
Example: Using md5 hash key |
|
371 |
|
|
372 |
```php |
|
373 |
<?php |
|
374 |
$I->seeBinaryResponseEquals("8c90748342f19b195b9c6b4eff742ded"); |
|
375 |
?> |
|
376 |
``` |
|
377 |
|
|
378 |
Example: Using md5 for a file contents |
|
379 |
|
|
380 |
```php |
|
381 |
<?php |
|
382 |
$fileData = file_get_contents("test_file.jpg"); |
|
383 |
$I->seeBinaryResponseEquals(md5($fileData)); |
|
384 |
?> |
|
385 |
``` |
|
386 |
Example: Using sha256 hash |
|
387 |
|
|
388 |
```php |
|
389 |
<?php |
|
390 |
$fileData = '/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k='; // very small jpeg |
|
391 |
$I->seeBinaryResponseEquals(hash("sha256", base64_decode($fileData)), 'sha256'); |
|
392 |
?> |
|
393 |
``` |
|
394 |
|
|
395 |
* `param` $hash the hashed data response expected |
|
396 |
* `param` $algo the hash algorithm to use. Default md5. |
|
397 |
* `[Part]` json |
|
398 |
* `[Part]` xml |
|
399 |
|
|
400 |
|
|
401 |
### seeHttpHeader |
|
402 |
|
|
403 |
Checks over the given HTTP header and (optionally) |
|
404 |
its value, asserting that are there |
|
405 |
|
|
406 |
* `param` $name |
|
407 |
* `param` $value |
|
408 |
* `[Part]` json |
|
409 |
* `[Part]` xml |
|
410 |
|
|
411 |
|
|
412 |
### seeHttpHeaderOnce |
|
413 |
|
|
414 |
Checks that http response header is received only once. |
|
415 |
HTTP RFC2616 allows multiple response headers with the same name. |
|
416 |
You can check that you didn't accidentally sent the same header twice. |
|
417 |
|
|
418 |
``` php |
|
419 |
<?php |
|
420 |
$I->seeHttpHeaderOnce('Cache-Control'); |
|
421 |
?>> |
|
422 |
``` |
|
423 |
|
|
424 |
* `param` $name |
|
425 |
* `[Part]` json |
|
426 |
* `[Part]` xml |
|
427 |
|
|
428 |
|
|
429 |
### seeResponseCodeIs |
|
430 |
|
|
431 |
Checks response code equals to provided value. |
|
432 |
|
|
433 |
```php |
|
434 |
<?php |
|
435 |
$I->seeResponseCodeIs(200); |
|
436 |
|
|
437 |
// preferred to use \Codeception\Util\HttpCode |
|
438 |
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); |
|
439 |
``` |
|
440 |
|
|
441 |
* `[Part]` json |
|
442 |
* `[Part]` xml |
|
443 |
* `param` $code |
|
444 |
|
|
445 |
|
|
446 |
### seeResponseCodeIsClientError |
|
447 |
|
|
448 |
Checks that the response code is 4xx |
|
449 |
|
|
450 |
* `[Part]` json |
|
451 |
* `[Part]` xml |
|
452 |
|
|
453 |
|
|
454 |
### seeResponseCodeIsRedirection |
|
455 |
|
|
456 |
Checks that the response code 3xx |
|
457 |
|
|
458 |
* `[Part]` json |
|
459 |
* `[Part]` xml |
|
460 |
|
|
461 |
|
|
462 |
### seeResponseCodeIsServerError |
|
463 |
|
|
464 |
Checks that the response code is 5xx |
|
465 |
|
|
466 |
* `[Part]` json |
|
467 |
* `[Part]` xml |
|
468 |
|
|
469 |
|
|
470 |
### seeResponseCodeIsSuccessful |
|
471 |
|
|
472 |
Checks that the response code is 2xx |
|
473 |
|
|
474 |
* `[Part]` json |
|
475 |
* `[Part]` xml |
|
476 |
|
|
477 |
|
|
478 |
### seeResponseContains |
|
479 |
|
|
480 |
Checks whether the last response contains text. |
|
481 |
|
|
482 |
* `param` $text |
|
483 |
* `[Part]` json |
|
484 |
* `[Part]` xml |
|
485 |
|
|
486 |
|
|
487 |
### seeResponseContainsJson |
|
488 |
|
|
489 |
Checks whether the last JSON response contains provided array. |
|
490 |
The response is converted to array with json_decode($response, true) |
|
491 |
Thus, JSON is represented by associative array. |
|
492 |
This method matches that response array contains provided array. |
|
493 |
|
|
494 |
Examples: |
|
495 |
|
|
496 |
``` php |
|
497 |
<?php |
|
498 |
// response: {name: john, email: john@gmail.com} |
|
499 |
$I->seeResponseContainsJson(array('name' => 'john')); |
|
500 |
|
|
501 |
// response {user: john, profile: { email: john@gmail.com }} |
|
502 |
$I->seeResponseContainsJson(array('email' => 'john@gmail.com')); |
|
503 |
|
|
504 |
?> |
|
505 |
``` |
|
506 |
|
|
507 |
This method recursively checks if one array can be found inside of another. |
|
508 |
|
|
509 |
* `param array` $json |
|
510 |
* `[Part]` json |
|
511 |
|
|
512 |
|
|
513 |
### seeResponseEquals |
|
514 |
|
|
515 |
Checks if response is exactly the same as provided. |
|
516 |
|
|
517 |
* `[Part]` json |
|
518 |
* `[Part]` xml |
|
519 |
* `param` $response |
|
520 |
|
|
521 |
|
|
522 |
### seeResponseIsJson |
|
523 |
|
|
524 |
Checks whether last response was valid JSON. |
|
525 |
This is done with json_last_error function. |
|
526 |
|
|
527 |
* `[Part]` json |
|
528 |
|
|
529 |
|
|
530 |
### seeResponseIsXml |
|
531 |
|
|
532 |
Checks whether last response was valid XML. |
|
533 |
This is done with libxml_get_last_error function. |
|
534 |
|
|
535 |
* `[Part]` xml |
|
536 |
|
|
537 |
|
|
538 |
### seeResponseJsonMatchesJsonPath |
|
539 |
|
|
540 |
Checks if json structure in response matches [JsonPath](http://goessner.net/articles/JsonPath/). |
|
541 |
JsonPath is XPath equivalent for querying Json structures. |
|
542 |
Try your JsonPath expressions [online](http://jsonpath.curiousconcept.com/). |
|
543 |
This assertion allows you to check the structure of response json. |
|
544 |
|
|
545 |
This method **require [`flow/jsonpath` > 0.2](https://github.com/FlowCommunications/JSONPath/) library to be installed**. |
|
546 |
|
|
547 |
```json |
|
548 |
{ "store": { |
|
549 |
"book": [ |
|
550 |
{ "category": "reference", |
|
551 |
"author": "Nigel Rees", |
|
552 |
"title": "Sayings of the Century", |
|
553 |
"price": 8.95 |
|
554 |
}, |
|
555 |
{ "category": "fiction", |
|
556 |
"author": "Evelyn Waugh", |
|
557 |
"title": "Sword of Honour", |
|
558 |
"price": 12.99 |
|
559 |
} |
|
560 |
], |
|
561 |
"bicycle": { |
|
562 |
"color": "red", |
|
563 |
"price": 19.95 |
|
564 |
} |
|
565 |
} |
|
566 |
} |
|
567 |
``` |
|
568 |
|
|
569 |
```php |
|
570 |
<?php |
|
571 |
// at least one book in store has author |
|
572 |
$I->seeResponseJsonMatchesJsonPath('$.store.book[*].author'); |
|
573 |
// first book in store has author |
|
574 |
$I->seeResponseJsonMatchesJsonPath('$.store.book[0].author'); |
|
575 |
// at least one item in store has price |
|
576 |
$I->seeResponseJsonMatchesJsonPath('$.store..price'); |
|
577 |
?> |
|
578 |
``` |
|
579 |
|
|
580 |
* `param string` $jsonPath |
|
581 |
* `[Part]` json |
|
582 |
* `Available since` 2.0.9 |
|
583 |
|
|
584 |
|
|
585 |
### seeResponseJsonMatchesXpath |
|
586 |
|
|
587 |
Checks if json structure in response matches the xpath provided. |
|
588 |
JSON is not supposed to be checked against XPath, yet it can be converted to xml and used with XPath. |
|
589 |
This assertion allows you to check the structure of response json. |
|
590 |
* |
|
591 |
```json |
|
592 |
{ "store": { |
|
593 |
"book": [ |
|
594 |
{ "category": "reference", |
|
595 |
"author": "Nigel Rees", |
|
596 |
"title": "Sayings of the Century", |
|
597 |
"price": 8.95 |
|
598 |
}, |
|
599 |
{ "category": "fiction", |
|
600 |
"author": "Evelyn Waugh", |
|
601 |
"title": "Sword of Honour", |
|
602 |
"price": 12.99 |
|
603 |
} |
|
604 |
], |
|
605 |
"bicycle": { |
|
606 |
"color": "red", |
|
607 |
"price": 19.95 |
|
608 |
} |
|
609 |
} |
|
610 |
} |
|
611 |
``` |
|
612 |
|
|
613 |
```php |
|
614 |
<?php |
|
615 |
// at least one book in store has author |
|
616 |
$I->seeResponseJsonMatchesXpath('//store/book/author'); |
|
617 |
// first book in store has author |
|
618 |
$I->seeResponseJsonMatchesXpath('//store/book[1]/author'); |
|
619 |
// at least one item in store has price |
|
620 |
$I->seeResponseJsonMatchesXpath('/store//price'); |
|
621 |
?> |
|
622 |
``` |
|
623 |
* `param string` $xpath |
|
624 |
* `[Part]` json |
|
625 |
* `Available since` 2.0.9 |
|
626 |
|
|
627 |
|
|
628 |
### seeResponseMatchesJsonType |
|
629 |
|
|
630 |
Checks that Json matches provided types. |
|
631 |
In case you don't know the actual values of JSON data returned you can match them by type. |
|
632 |
Starts check with a root element. If JSON data is array it will check the first element of an array. |
|
633 |
You can specify the path in the json which should be checked with JsonPath |
|
634 |
|
|
635 |
Basic example: |
|
636 |
|
|
637 |
```php |
|
638 |
<?php |
|
639 |
// {'user_id': 1, 'name': 'davert', 'is_active': false} |
|
640 |
$I->seeResponseMatchesJsonType([ |
|
641 |
'user_id' => 'integer', |
|
642 |
'name' => 'string|null', |
|
643 |
'is_active' => 'boolean' |
|
644 |
]); |
|
645 |
|
|
646 |
// narrow down matching with JsonPath: |
|
647 |
// {"users": [{ "name": "davert"}, {"id": 1}]} |
|
648 |
$I->seeResponseMatchesJsonType(['name' => 'string'], '$.users[0]'); |
|
649 |
?> |
|
650 |
``` |
|
651 |
|
|
652 |
In this case you can match that record contains fields with data types you expected. |
|
653 |
The list of possible data types: |
|
654 |
|
|
655 |
* string |
|
656 |
* integer |
|
657 |
* float |
|
658 |
* array (json object is array as well) |
|
659 |
* boolean |
|
660 |
|
|
661 |
You can also use nested data type structures: |
|
662 |
|
|
663 |
```php |
|
664 |
<?php |
|
665 |
// {'user_id': 1, 'name': 'davert', 'company': {'name': 'Codegyre'}} |
|
666 |
$I->seeResponseMatchesJsonType([ |
|
667 |
'user_id' => 'integer|string', // multiple types |
|
668 |
'company' => ['name' => 'string'] |
|
669 |
]); |
|
670 |
?> |
|
671 |
``` |
|
672 |
|
|
673 |
You can also apply filters to check values. Filter can be applied with `:` char after the type declaration. |
|
674 |
|
|
675 |
Here is the list of possible filters: |
|
676 |
|
|
677 |
* `integer:>{val}` - checks that integer is greater than {val} (works with float and string types too). |
|
678 |
* `integer:<{val}` - checks that integer is lower than {val} (works with float and string types too). |
|
679 |
* `string:url` - checks that value is valid url. |
|
680 |
* `string:date` - checks that value is date in JavaScript format: https://weblog.west-wind.com/posts/2014/Jan/06/JavaScript-JSON-Date-Parsing-and-real-Dates |
|
681 |
* `string:email` - checks that value is a valid email according to http://emailregex.com/ |
|
682 |
* `string:regex({val})` - checks that string matches a regex provided with {val} |
|
683 |
|
|
684 |
This is how filters can be used: |
|
685 |
|
|
686 |
```php |
|
687 |
<?php |
|
688 |
// {'user_id': 1, 'email' => 'davert@codeception.com'} |
|
689 |
$I->seeResponseMatchesJsonType([ |
|
690 |
'user_id' => 'string:>0:<1000', // multiple filters can be used |
|
691 |
'email' => 'string:regex(~\@~)' // we just check that @ char is included |
|
692 |
]); |
|
693 |
|
|
694 |
// {'user_id': '1'} |
|
695 |
$I->seeResponseMatchesJsonType([ |
|
696 |
'user_id' => 'string:>0', // works with strings as well |
|
697 |
} |
|
698 |
?> |
|
699 |
``` |
|
700 |
|
|
701 |
You can also add custom filters y accessing `JsonType::addCustomFilter` method. |
|
702 |
See [JsonType reference](http://codeception.com/docs/reference/JsonType). |
|
703 |
|
|
704 |
* `[Part]` json |
|
705 |
* `Available since` 2.1.3 |
|
706 |
* `param array` $jsonType |
|
707 |
* `param string` $jsonPath |
|
708 |
|
|
709 |
|
|
710 |
### seeXmlResponseEquals |
|
711 |
|
|
712 |
Checks XML response equals provided XML. |
|
713 |
Comparison is done by canonicalizing both xml`s. |
|
714 |
|
|
715 |
Parameters can be passed either as DOMDocument, DOMNode, XML string, or array (if no attributes). |
|
716 |
|
|
717 |
* `param` $xml |
|
718 |
* `[Part]` xml |
|
719 |
|
|
720 |
|
|
721 |
### seeXmlResponseIncludes |
|
722 |
|
|
723 |
Checks XML response includes provided XML. |
|
724 |
Comparison is done by canonicalizing both xml`s. |
|
725 |
Parameter can be passed either as XmlBuilder, DOMDocument, DOMNode, XML string, or array (if no attributes). |
|
726 |
|
|
727 |
Example: |
|
728 |
|
|
729 |
``` php |
|
730 |
<?php |
|
731 |
$I->seeXmlResponseIncludes("<result>1</result>"); |
|
732 |
?> |
|
733 |
``` |
|
734 |
|
|
735 |
* `param` $xml |
|
736 |
* `[Part]` xml |
|
737 |
|
|
738 |
|
|
739 |
### seeXmlResponseMatchesXpath |
|
740 |
|
|
741 |
Checks whether XML response matches XPath |
|
742 |
|
|
743 |
```php |
|
744 |
<?php |
|
745 |
$I->seeXmlResponseMatchesXpath('//root/user[@id=1]'); |
|
746 |
``` |
|
747 |
* `[Part]` xml |
|
748 |
* `param` $xpath |
|
749 |
|
|
750 |
|
|
751 |
### sendDELETE |
|
752 |
|
|
753 |
Sends DELETE request to given uri. |
|
754 |
|
|
755 |
* `param` $url |
|
756 |
* `param array` $params |
|
757 |
* `param array` $files |
|
758 |
* `[Part]` json |
|
759 |
* `[Part]` xml |
|
760 |
|
|
761 |
|
|
762 |
### sendGET |
|
763 |
|
|
764 |
Sends a GET request to given uri. |
|
765 |
|
|
766 |
* `param` $url |
|
767 |
* `param array` $params |
|
768 |
* `[Part]` json |
|
769 |
* `[Part]` xml |
|
770 |
|
|
771 |
|
|
772 |
### sendHEAD |
|
773 |
|
|
774 |
Sends a HEAD request to given uri. |
|
775 |
|
|
776 |
* `param` $url |
|
777 |
* `param array` $params |
|
778 |
* `[Part]` json |
|
779 |
* `[Part]` xml |
|
780 |
|
|
781 |
|
|
782 |
### sendLINK |
|
783 |
|
|
784 |
Sends LINK request to given uri. |
|
785 |
|
|
786 |
* `param` $url |
|
787 |
* `param array` $linkEntries (entry is array with keys "uri" and "link-param") |
|
788 |
|
|
789 |
@link http://tools.ietf.org/html/rfc2068#section-19.6.2.4 |
|
790 |
|
|
791 |
@author samva.ua@gmail.com |
|
792 |
* `[Part]` json |
|
793 |
* `[Part]` xml |
|
794 |
|
|
795 |
|
|
796 |
### sendOPTIONS |
|
797 |
|
|
798 |
Sends an OPTIONS request to given uri. |
|
799 |
|
|
800 |
* `param` $url |
|
801 |
* `param array` $params |
|
802 |
* `[Part]` json |
|
803 |
* `[Part]` xml |
|
804 |
|
|
805 |
|
|
806 |
### sendPATCH |
|
807 |
|
|
808 |
Sends PATCH request to given uri. |
|
809 |
|
|
810 |
* `param` $url |
|
811 |
* `param array` $params |
|
812 |
* `param array` $files |
|
813 |
* `[Part]` json |
|
814 |
* `[Part]` xml |
|
815 |
|
|
816 |
|
|
817 |
### sendPOST |
|
818 |
|
|
819 |
Sends a POST request to given uri. Parameters and files can be provided separately. |
|
820 |
|
|
821 |
Example: |
|
822 |
```php |
|
823 |
<?php |
|
824 |
//simple POST call |
|
825 |
$I->sendPOST('/message', ['subject' => 'Read this!', 'to' => 'johndoe@example.com']); |
|
826 |
//simple upload method |
|
827 |
$I->sendPOST('/message/24', ['inline' => 0], ['attachmentFile' => codecept_data_dir('sample_file.pdf')]); |
|
828 |
//uploading a file with a custom name and mime-type. This is also useful to simulate upload errors. |
|
829 |
$I->sendPOST('/message/24', ['inline' => 0], [ |
|
830 |
'attachmentFile' => [ |
|
831 |
'name' => 'document.pdf', |
|
832 |
'type' => 'application/pdf', |
|
833 |
'error' => UPLOAD_ERR_OK, |
|
834 |
'size' => filesize(codecept_data_dir('sample_file.pdf')), |
|
835 |
'tmp_name' => codecept_data_dir('sample_file.pdf') |
|
836 |
] |
|
837 |
]); |
|
838 |
``` |
|
839 |
|
|
840 |
* `param` $url |
|
841 |
* `param array|\JsonSerializable` $params |
|
842 |
* `param array` $files A list of filenames or "mocks" of $_FILES (each entry being an array with the following |
|
843 |
keys: name, type, error, size, tmp_name (pointing to the real file path). Each key works |
|
844 |
as the "name" attribute of a file input field. |
|
845 |
|
|
846 |
@see http://php.net/manual/en/features.file-upload.post-method.php |
|
847 |
@see codecept_data_dir() |
|
848 |
* `[Part]` json |
|
849 |
* `[Part]` xml |
|
850 |
|
|
851 |
|
|
852 |
### sendPUT |
|
853 |
|
|
854 |
Sends PUT request to given uri. |
|
855 |
|
|
856 |
* `param` $url |
|
857 |
* `param array` $params |
|
858 |
* `param array` $files |
|
859 |
* `[Part]` json |
|
860 |
* `[Part]` xml |
|
861 |
|
|
862 |
|
|
863 |
### sendUNLINK |
|
864 |
|
|
865 |
Sends UNLINK request to given uri. |
|
866 |
|
|
867 |
* `param` $url |
|
868 |
* `param array` $linkEntries (entry is array with keys "uri" and "link-param") |
|
869 |
@link http://tools.ietf.org/html/rfc2068#section-19.6.2.4 |
|
870 |
@author samva.ua@gmail.com |
|
871 |
* `[Part]` json |
|
872 |
* `[Part]` xml |
|
873 |
|
|
874 |
|
|
875 |
### startFollowingRedirects |
|
876 |
|
|
877 |
Enables automatic redirects to be followed by the client |
|
878 |
|
|
879 |
```php |
|
880 |
<?php |
|
881 |
$I->startFollowingRedirects(); |
|
882 |
``` |
|
883 |
|
|
884 |
* `[Part]` xml |
|
885 |
* `[Part]` json |
|
886 |
|
|
887 |
|
|
888 |
### stopFollowingRedirects |
|
889 |
|
|
890 |
Prevents automatic redirects to be followed by the client |
|
891 |
|
|
892 |
```php |
|
893 |
<?php |
|
894 |
$I->stopFollowingRedirects(); |
|
895 |
``` |
|
896 |
|
|
897 |
* `[Part]` xml |
|
898 |
* `[Part]` json |
|
899 |
|
|
900 |
<p> </p><div class="alert alert-warning">Module reference is taken from the source code. <a href="https://github.com/Codeception/Codeception/tree/2.5/src/Codeception/Module/REST.php">Help us to improve documentation. Edit module reference</a></div> |