Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent output binary data #1884

Closed
jumale opened this issue Apr 29, 2015 · 8 comments
Closed

Prevent output binary data #1884

jumale opened this issue Apr 29, 2015 · 8 comments
Labels

Comments

@jumale
Copy link

@jumale jumale commented Apr 29, 2015

  • _Known Linux issue:_ terminal is broken after binary data output.
  • _Codeception issue:_ controllers may return binary response which causes the problem in "verbose" or "debug" mode.
  • _How to reproduce:_
    • create a controller which returns binary data, for example ZIP archive
    • create a functional test using REST module and send there a GET request to the controller
    • run the test with -vvv or --debug
  • _Solution:_ if it's possible, it would be nice to check that response contains binary data, and do not output it here (maybe show something like string [Binary Data] instead).

P.S.: I got this issue in REST module, but probably it can happen in some another modules

@DavertMik
Copy link
Member

@DavertMik DavertMik commented Apr 29, 2015

How to do you propose to detect binary data?

@jumale
Copy link
Author

@jumale jumale commented Apr 30, 2015

@DavertMik ok, I've discovered internet and looks like there is no easy solution to define the binary data.
But I found a way which is enough for me. All is needed from codeception - allow to define a callback to intercept debug messages. Like that:

<?php
namespace Codeception\Util;

class Debug
{
    /**
     * @var callable
     */
    public static $interceptDebug = null;

    public static function debug($message)
    {
        if (!self::$output) {
            return;
        }
        // modified part
        if (is_callable(static::$interceptDebug)) {
            $message = call_user_func(static::$interceptDebug, $message);
        }
        self::$output->debug($message);
    }

    // ...
}

then I can create a helper with the next method:

<?php
public function expectBinaryResponse()
{
    \Codeception\Util\Debug::$interceptDebug = function ($message) {
        if (0 == strpos($message, '[Response]')) {
            $message = '[Response] [Binary Data]';
        }

        return $message;
    };
}

and then in my test:

$I = new Tester($scenario);
$I->expectBinaryResponse();
// ...

I know it looks a bit ugly, but I have no other idea how to hack it :)

@DavertMik
Copy link
Member

@DavertMik DavertMik commented Apr 30, 2015

Thanks, I will try to think on this too

@codedokode
Copy link

@codedokode codedokode commented May 9, 2015

You could also replace unprintable characters 00-1f with codes like \x00 so the data are still readable.

codemedic added a commit to redmatter/Codeception that referenced this issue Nov 18, 2015
DavertMik added a commit that referenced this issue Nov 25, 2015
…T-module-debug

Fixing issue #1884 in v2.1.x - hides binary request data in debug
@Naktibalda Naktibalda closed this Jun 11, 2016
@spikyjt
Copy link
Contributor

@spikyjt spikyjt commented Feb 2, 2017

The linked PR did not fix this issue, it only fixed an issue where the request data included binary data.

This issue is about the response having binary data.

I fixed this locally in a similar manner to #2552 with the following at the end of Codeception\Module\REST::execute():

        $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);

I'll create a PR later if it would be welcomed, but I can't right now as I'm at work!

@Naktibalda
Copy link
Member

@Naktibalda Naktibalda commented Feb 2, 2017

Thanks, please make a PR.

@spikyjt
Copy link
Contributor

@spikyjt spikyjt commented Feb 2, 2017

Done. Sorry for any confusion, I stupidly created a PR on my own fork instead of to the main repo!

spikyjt added a commit to spikyjt/Codeception that referenced this issue Feb 3, 2017
DavertMik added a commit that referenced this issue Feb 3, 2017
* hide binary response data in debug #1884

* rework binary formatting for DRY, variable rename #1884
chris1312 pushed a commit to chris1312/Codeception that referenced this issue Jun 16, 2017
* hide binary response data in debug Codeception#1884

* rework binary formatting for DRY, variable rename Codeception#1884
@Naktibalda
Copy link
Member

@Naktibalda Naktibalda commented Mar 25, 2019

Fixed by #3985 two years ago.

@Naktibalda Naktibalda closed this Mar 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.