From b04a7a5d37e5c682d95d46d978cbb9aace548376 Mon Sep 17 00:00:00 2001 From: JT Date: Thu, 2 Feb 2017 23:16:33 +0000 Subject: [PATCH 1/2] hide binary response data in debug #1884 --- src/Codeception/Module/REST.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Codeception/Module/REST.php b/src/Codeception/Module/REST.php index d215ec3045..a4e1cee14f 100644 --- a/src/Codeception/Module/REST.php +++ b/src/Codeception/Module/REST.php @@ -498,7 +498,13 @@ protected function execute($method, $url, $parameters = [], $files = []) $this->debugSection("Request", "$method $url " . $requestData); $this->response = (string)$this->connectionModule->_request($method, $url, [], $files, [], $parameters); } - $this->debugSection("Response", $this->response); + $response = $this->response; + if (!ctype_print($response) && false === mb_detect_encoding($response, mb_detect_order(), true)) { + // if the response has non-printable bytes and it is not a valid unicode string, reformat the + // display string to signify the presence of a binary response + $response = '[binary-data length:' . strlen($response) . ' md5:' . md5($response) . ']'; + } + $this->debugSection("Response", $response); } protected function encodeApplicationJson($method, $parameters) From cd5fac4ba99df2d5fd1e121ee7a741413fa82057 Mon Sep 17 00:00:00 2001 From: JT Date: Fri, 3 Feb 2017 18:57:39 +0000 Subject: [PATCH 2/2] rework binary formatting for DRY, variable rename #1884 --- src/Codeception/Module/REST.php | 38 ++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Codeception/Module/REST.php b/src/Codeception/Module/REST.php index a4e1cee14f..aa7939c982 100644 --- a/src/Codeception/Module/REST.php +++ b/src/Codeception/Module/REST.php @@ -490,21 +490,39 @@ protected function execute($method, $url, $parameters = [], $files = []) $this->response = (string)$this->connectionModule->_request($method, $url, $parameters, $files); } else { $requestData = $parameters; - if (!ctype_print($requestData) && false === mb_detect_encoding($requestData, mb_detect_order(), true)) { - // if the request data has non-printable bytes and it is not a valid unicode string, reformat the - // display string to signify the presence of request data - $requestData = '[binary-data length:' . strlen($requestData) . ' md5:' . md5($requestData) . ']'; + if ($this->isBinaryData($requestData)) { + $requestData = $this->binaryToDebugString($requestData); } $this->debugSection("Request", "$method $url " . $requestData); $this->response = (string)$this->connectionModule->_request($method, $url, [], $files, [], $parameters); } - $response = $this->response; - if (!ctype_print($response) && false === mb_detect_encoding($response, mb_detect_order(), true)) { - // if the response has non-printable bytes and it is not a valid unicode string, reformat the - // display string to signify the presence of a binary response - $response = '[binary-data length:' . strlen($response) . ' md5:' . md5($response) . ']'; + $printedResponse = $this->response; + if ($this->isBinaryData($printedResponse)) { + $printedResponse = $this->binaryToDebugString($printedResponse); } - $this->debugSection("Response", $response); + $this->debugSection("Response", $printedResponse); + } + + /** + * Check if data has non-printable bytes and it is not a valid unicode string + * + * @param string $data the text or binary data string + * @return boolean + */ + protected function isBinaryData($data) + { + return !ctype_print($data) && false === mb_detect_encoding($data, mb_detect_order(), true); + } + + /** + * Format a binary string for debug printing + * + * @param string $data the binary data string + * @return string the debug string + */ + protected function binaryToDebugString($data) + { + return '[binary-data length:' . strlen($data) . ' md5:' . md5($data) . ']'; } protected function encodeApplicationJson($method, $parameters)