Testomatio - Test Management for Codeception
composer require --dev codeception/module-restModule for testing REST WebService.
This module requires either PhpBrowser or a framework module (e.g. Symfony, Laravel) to send the actual HTTP request.
url optional - the url of apishortDebugResponse optional - number of chars to limit the API response lengthmodules:
enabled:
- REST:
depends: PhpBrowser
url: 'https://example.com/api/v1/'
shortDebugResponse: 300 # only the first 300 characters of the responseIn case you need to configure low-level HTTP headers, that’s done on the PhpBrowser level like so:
modules:
enabled:
- REST:
depends: PhpBrowser
url: &url 'https://example.com/api/v1/'
config:
PhpBrowser:
url: *url
headers:
Content-Type: application/jsonJSONPath is the equivalent to XPath, for querying JSON data structures. Here’s an Online JSONPath Expressions Tester
Conflicts with SOAP module
param array $additionalAWSConfigthrows \Codeception\Exception\ConfigurationExceptionreturn voidAllows to send REST request using AWS Authorization
Only works with PhpBrowser Example Config:
yml
modules:
enabled:
- REST:
aws:
key: accessKey
secret: accessSecret
service: awsService
region: awsRegionCode:
<?php
$I->amAWSAuthenticated();part jsonpart xmlparam string $accessTokenreturn voidAdds Bearer authentication via access token.
part jsonpart xmlparam string $usernameparam string $passwordreturn voidAdds Digest authentication via username/password.
part jsonpart xmlparam string $usernameparam string $passwordreturn voidAdds HTTP authentication via username/password.
part jsonpart xmlparam string $usernameparam string $passwordthrows \Codeception\Exception\ModuleExceptionreturn voidAdds NTLM authentication via username/password.
Requires client to be Guzzle >=6.3.0 Out of scope for functional modules.
Example:
<?php
$I->amNTLMAuthenticated('jon_snow', 'targaryen');@deprecated
param string $namereturn voidpart jsonpart xmlparam string $hash the hashed data response expectedparam string $algo the hash algorithm to use. Default md5.return voidChecks if the hash of a binary response is not the same as provided.
<?php
$I->dontSeeBinaryResponseEquals('8c90748342f19b195b9c6b4eff742ded');Opposite to seeBinaryResponseEquals
part jsonpart xmlparam string $nameparam $valuereturn voidChecks over the given HTTP header and (optionally) its value, asserting that are not there
part jsonpart xmlparam int $codereturn voidChecks that response code is not equal to provided value.
<?php
$I->dontSeeResponseCodeIs(200);
// preferred to use \Codeception\Util\HttpCode
$I->dontSeeResponseCodeIs(\Codeception\Util\HttpCode::OK);part jsonpart xmlparam string $textreturn voidChecks whether last response do not contain text.
part jsonparam array $jsonreturn voidOpposite to seeResponseContainsJson
part jsonparam string $jsonPathreturn voidSee #jsonpath for general info on JSONPath.
Opposite to seeResponseJsonMatchesJsonPath()
part jsonparam string $xPathreturn voidOpposite to seeResponseJsonMatchesXpath
part jsonparam string $xPathparam $expectedreturn voidOpposite to seeResponseJsonXpathEvaluatesTo
part jsonsee seeResponseMatchesJsonTypeparam array $jsonType JsonType structureparam ?string $jsonPathreturn voidOpposite to seeResponseMatchesJsonType.
part xmlparam mixed $xmlreturn voidChecks XML response does not equal to provided XML.
Comparison is done by canonicalizing both xml`s.
Parameter can be passed either as XmlBuilder, DOMDocument, DOMNode, XML string, or array (if no attributes).
part xmlparam mixed $xmlreturn voidChecks XML response does not include provided XML.
Comparison is done by canonicalizing both xml`s. Parameter can be passed either as XmlBuilder, DOMDocument, DOMNode, XML string, or array (if no attributes).
part xmlparam string $xPathreturn voidChecks whether XML response does not match XPath
<?php
$I->dontSeeXmlResponseMatchesXpath('//root/user[@id=1]');part xmlparam string $cssOrXPathparam string $attributereturn stringFinds and returns attribute of element.
Element is matched by either CSS or XPath
part jsonparam string $jsonPaththrows \Exceptionreturn array Array of matching itemsSee #jsonpath for general info on JSONPath.
Even for a single value an array is returned. Example:
<?php
// match the first `user.id` in json
$firstUserId = $I->grabDataFromResponseByJsonPath('$..users[0].id');
$I->sendPut('/user', array('id' => $firstUserId[0], 'name' => 'davert'));part jsonpart xmlparam string $nameparam bool $first Whether to return the first value or all header valuesreturn string|array The first header value if $first is true, an array of values otherwiseReturns the value of the specified header name
part jsonpart xmlreturn stringReturns current response so that it can be used in next scenario steps.
Example:
<?php
$user_id = $I->grabResponse();
$I->sendPut('/user', array('id' => $user_id, 'name' => 'davert'));part xmlparam mixed $cssOrXPathreturn stringFinds and returns text contents of element.
Element is matched by either CSS or XPath
part jsonpart xmlparam string $nameparam string $valuereturn voidSets a HTTP header to be used for all subsequent requests. Use unsetHttpHeader to unset it.
<?php
$I->haveHttpHeader('Content-Type', 'application/json');
// all next requests will contain this headerparam $nameparam $valuereturn voidSets SERVER parameter valid for all next requests.
$I->haveServerParameter('name', 'value');part jsonpart xmlparam string $hash the hashed data response expectedparam string $algo the hash algorithm to use. Default sha1.return voidChecks if the hash of a binary response is exactly the same as provided.
Parameter can be passed as any hash string supported by hash(), with an
optional second parameter to specify the hash type, which defaults to sha1.
Example: Using sha1 hash key
<?php
$I->seeBinaryResponseEquals('df589122eac0f6a7bd8795436e692e3675cadc3b');Example: Using sha1 for a file contents
<?php
$fileData = file_get_contents('test_file.jpg');
$I->seeBinaryResponseEquals(md5($fileData));Example: Using sha256 hash
<?php
$fileData = '/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k='; // very small jpeg
$I->seeBinaryResponseEquals(hash('sha256', base64_decode($fileData)), 'sha256');part json