Skip to content

Commit 5cc325d

Browse files
sebastianneubertDavertMik
authored andcommitted
[REST] Short API responses in debug mode (#5455)
* [REST] Short API responses in debug mode * codestyle * Use validateConfig for shortDebugResponse. Just allow integer as config params. * fix encoding. fix if condition for shorting messages.
1 parent 693144c commit 5cc325d

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

src/Codeception/Module/REST.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
23
namespace Codeception\Module;
34

45
use Codeception\Exception\ConfigurationException;
6+
use Codeception\Exception\ModuleConfigException;
57
use Codeception\Exception\ModuleException;
68
use Codeception\Lib\Interfaces\ConflictsWithModule;
79
use Codeception\Module as CodeceptionModule;
@@ -28,6 +30,7 @@
2830
* ## Configuration
2931
*
3032
* * url *optional* - the url of api
33+
* * shortDebugResponse *optional* - amount of chars to limit the api response length
3134
*
3235
* This module requires PHPBrowser or any of Framework modules enabled.
3336
*
@@ -38,6 +41,7 @@
3841
* - REST:
3942
* depends: PhpBrowser
4043
* url: 'http://serviceapp/api/v1/'
44+
* shortDebugResponse: 300 # only the first 300 chars of the response
4145
*
4246
* ## Public Properties
4347
*
@@ -70,10 +74,13 @@ class REST extends CodeceptionModule implements DependsOnModule, PartedModule, A
7074
- REST:
7175
depends: PhpBrowser
7276
url: http://localhost/api/
77+
shortDebugResponse: 300
7378
--
7479
Framework modules can be used for testing of API as well.
7580
EOF;
7681

82+
protected $DEFAULT_SHORTEN_VALUE = 150;
83+
7784
/**
7885
* @var \Symfony\Component\HttpKernel\Client|\Symfony\Component\BrowserKit\Client
7986
*/
@@ -332,7 +339,7 @@ public function amNTLMAuthenticated($username, $password)
332339
throw new ModuleException(__METHOD__, 'Not supported if not using a Guzzle client');
333340
}
334341
if (version_compare(\GuzzleHttp\Client::VERSION, '6.2.1', 'lt')) {
335-
throw new ModuleException(__METHOD__, 'Guzzle '.\GuzzleHttp\Client::VERSION.' found. Requires Guzzle >=6.3.0 for NTLM auth option');
342+
throw new ModuleException(__METHOD__, 'Guzzle ' . \GuzzleHttp\Client::VERSION . ' found. Requires Guzzle >=6.3.0 for NTLM auth option');
336343
}
337344
$this->client->setAuth($username, $password, 'ntlm');
338345
}
@@ -608,7 +615,15 @@ protected function execute($method, $url, $parameters = [], $files = [])
608615
if ($this->isBinaryData($printedResponse)) {
609616
$printedResponse = $this->binaryToDebugString($printedResponse);
610617
}
611-
$this->debugSection("Response", $printedResponse);
618+
619+
$short = $this->_getConfig('shortDebugResponse');
620+
621+
if (!is_null($short)) {
622+
$printedResponse = $this->shortenMessage($printedResponse, $short);
623+
$this->debugSection("Shortened Response", $printedResponse);
624+
} else {
625+
$this->debugSection("Response", $printedResponse);
626+
}
612627
}
613628

614629
/**
@@ -717,6 +732,23 @@ private function checkFileBeforeUpload($file)
717732
}
718733
}
719734

735+
/**
736+
* Extends the function Module::validateConfig for shorten messages
737+
*
738+
*/
739+
protected function validateConfig()
740+
{
741+
parent::validateConfig();
742+
743+
$short = $this->_getConfig('shortDebugResponse');
744+
745+
if (!is_null($short)) {
746+
if (!is_int($short) || $short < 0) {
747+
throw new ModuleConfigException(__CLASS__, 'The value of "shortDebugMessage" should be integer and greater or equal "0".');
748+
}
749+
}
750+
}
751+
720752
/**
721753
* Checks whether last response was valid JSON.
722754
* This is done with json_last_error function.
@@ -809,10 +841,10 @@ public function seeResponseContainsJson($json = [])
809841
* ?>
810842
* ```
811843
*
812-
* @version 1.1
813844
* @return string
814845
* @part json
815846
* @part xml
847+
* @version 1.1
816848
*/
817849
public function grabResponse()
818850
{
@@ -839,9 +871,9 @@ public function grabResponse()
839871
*
840872
* @param string $jsonPath
841873
* @return array Array of matching items
842-
* @version 2.0.9
843874
* @throws \Exception
844875
* @part json
876+
* @version 2.0.9
845877
*/
846878
public function grabDataFromResponseByJsonPath($jsonPath)
847879
{
@@ -1077,9 +1109,9 @@ public function dontSeeResponseContainsJson($json = [])
10771109
* See [JsonType reference](http://codeception.com/docs/reference/JsonType).
10781110
*
10791111
* @part json
1080-
* @version 2.1.3
10811112
* @param array $jsonType
10821113
* @param string $jsonPath
1114+
* @version 2.1.3
10831115
*/
10841116
public function seeResponseMatchesJsonType(array $jsonType, $jsonPath = null)
10851117
{
@@ -1095,9 +1127,9 @@ public function seeResponseMatchesJsonType(array $jsonType, $jsonPath = null)
10951127
* Opposite to `seeResponseMatchesJsonType`.
10961128
*
10971129
* @part json
1098-
* @see seeResponseMatchesJsonType
10991130
* @param $jsonType jsonType structure
11001131
* @param null $jsonPath optionally set specific path to structure with JsonPath
1132+
* @see seeResponseMatchesJsonType
11011133
* @version 2.1.3
11021134
*/
11031135
public function dontSeeResponseMatchesJsonType($jsonType, $jsonPath = null)

0 commit comments

Comments
 (0)