| layout | doc |
|---|---|
| title | REST Module - Codeception - Documentation |
For additional reference, please review the source
Module for testing REST WebService.
This module can be used either with frameworks or PHPBrowser. It tries to guess the framework is is attached to.
Whether framework is used it operates via standard framework modules. Otherwise sends raw HTTP requests to url via PHPBrowser.
- Maintainer: tiger-seo, davert
- Stability: stable
- Contact: codecept@davert.mail.ua
- Contact: tiger.seo@gmail.com
- url optional - the url of api
This module requires PHPBrowser or any of Framework modules enabled.
modules:
enabled: [PhpBrowser, REST]
config:
PhpBrowser:
url: http://serviceapp/
REST:
url: 'http://serviceapp/api/v1/'
- headers - array of headers going to be sent.
- params - array of sent data
- response - last response (string)
Adds Bearer authentication via access token.
param$accessToken
s Digest authentication via username/password.
ram $username ram $password
Adds HTTP authentication via username/password.
param$usernameparam$password
Checks over the given HTTP header and (optionally) its value, asserting that are not there
param$nameparam$value
Checks that response code is not equal to provided value.
param$code
Checks whether last response do not contain text.
param$text
Opposite to seeResponseContainsJson
param array$json
Opposite to seeResponseJsonMatchesJsonPath
param array$jsonPath
Returns data from the current JSON response using specified path so that it can be used in next scenario steps.
this method is deprecated in favor of grabDataFromResponseByJsonPath
Example:
{% highlight php %}
grabDataFromJsonResponse('user.user_id'); $I->sendPUT('/user', array('id' => $user_id, 'name' => 'davert')); ?>{% endhighlight %}
@deprecated please use grabDataFromResponseByJsonPath
param string$path @return string
Returns data from the current JSON response using JSONPath as selector. JsonPath is XPath equivalent for querying Json structures. Try your JsonPath expressions online. Even for a single value an array is returned.
This method require flow/jsonpath library to be installed.
Example:
{% highlight php %}
grabDataFromJsonResponse('$..users[0].id'); $I->sendPUT('/user', array('id' => $firstUser[0], 'name' => 'davert')); ?>{% endhighlight %}
param$jsonPath @return array @version 2.0.9 \Exception
Returns the value of the specified header name
-
param$name -
param Boolean$first Whether to return the first value or all header values -
return string|array The first header value if$first is true, an array of values otherwise
Returns current response so that it can be used in next scenario steps.
Example:
{% highlight php %}
grabResponse(); $I->sendPUT('/user', array('id' => $user_id, 'name' => 'davert')); ?>{% endhighlight %}
@version 1.1 @return string
Sets HTTP header
param$nameparam$value
Checks over the given HTTP header and (optionally) its value, asserting that are there
param$nameparam$value
Checks that http response header is received only once. HTTP RFC2616 allows multiple response headers with the same name. You can check that you didn't accidentally sent the same header twice.
{% highlight php %}
seeHttpHeaderOnce('Cache-Control'); ?>>{% endhighlight %}
param$name
Checks response code equals to provided value.
param$code
Checks whether the last response contains text.
param$text
Checks whether the last JSON response contains provided array. The response is converted to array with json_decode($response, true) Thus, JSON is represented by associative array. This method matches that response array contains provided array.
Examples:
{% highlight php %}
seeResponseContainsJson(array('name' => 'john')); // response {user: john, profile: { email: john@gmail.com }} $I->seeResponseContainsJson(array('email' => 'john@gmail.com')); ?>{% endhighlight %}
This method recursively checks if one array can be found inside of another.
param array$json
Checks if response is exactly the same as provided.
param$response
Checks whether last response was valid JSON. This is done with json_last_error function.
Checks whether last response was valid XML. This is done with libxml_get_last_error function.
Checks if json structure in response matches JsonPath. JsonPath is XPath equivalent for querying Json structures. Try your JsonPath expressions online. This assertion allows you to check the structure of response json.
This method require flow/jsonpath library to be installed.
{% highlight json %}
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 } ], "bicycle": { "color": "red", "price": 19.95 } } }
{% endhighlight %}
{% highlight php %}
seeResponseJsonMatchesJsonPath('$.store.book[*].author'); // first book in store has author $I->seeResponseJsonMatchesJsonPath('$.store.book[0].author'); // at least one item in store has price $I->seeResponseJsonMatchesJsonPath('$.store..price'); ?>{% endhighlight %}
@version 2.0.9
Checks if json structure in response matches the xpath provided. JSON is not supposed to be checked against XPath, yet it can be converted to xml and used with XPath. This assertion allows you to check the structure of response json. * {% highlight json %}
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 } ], "bicycle": { "color": "red", "price": 19.95 } } }
{% endhighlight %}
{% highlight php %}
seeResponseJsonMatchesXpath('//store/book/author'); // first book in store has author $I->seeResponseJsonMatchesXpath('//store/book[1]/author'); // at least one item in store has price $I->seeResponseJsonMatchesXpath('/store//price'); ?>{% endhighlight %}
@version 2.0.9
Sends DELETE request to given uri.
param$urlparam array$paramsparam array$files
Sends a GET request to given uri.
param$urlparam array$params
Sends a HEAD request to given uri.
param$urlparam array$params
Sends LINK request to given uri.
param$urlparam array$linkEntries (entry is array with keys "uri" and "link-param")
@link http://tools.ietf.org/html/rfc2068#section-19.6.2.4
@author samva.ua@gmail.com
Sends an OPTIONS request to given uri.
param$urlparam array$params
Sends PATCH request to given uri.
param$urlparam array$paramsparam array$files
Sends a POST request to given uri.
Parameters and files (as array of filenames) can be provided.
param$urlparam array|\JsonSerializable$paramsparam array$files
Sends PUT request to given uri.
param$urlparam array$paramsparam array$files
Sends UNLINK request to given uri.
param$urlparam array$linkEntries (entry is array with keys "uri" and "link-param") @link http://tools.ietf.org/html/rfc2068#section-19.6.2.4 @author samva.ua@gmail.com