tests for dealing with binary request and response data import requests from fixtures import binfilepath binfilecontent binfilepatharg from httpie output streams import binarysuppressednotice from utils import mockenvironment http class testbinaryrequestdata def testbinarystdinself httpbin with openbinfilepath rb as stdin env mockenvironment stdinstdin stdinisattyfalse stdoutisattyfalse r http printb post httpbin url post envenv assert r binfilecontent def testbinaryfilepathself httpbin env mockenvironmentstdinisattytrue stdoutisattyfalse r http printb post httpbin url post binfilepatharg envenv assert r binfilecontent def testbinaryfileformself httpbin env mockenvironmentstdinisattytrue stdoutisattyfalse r http printb form post httpbin url post test binfilepatharg envenv assert bytesbinfilecontent in bytesr class testbinaryresponsedata def testbinarysuppresseswhenterminalself httpbin r http get httpbin bytes1024 seed1 assert binarysuppressednotice decode in r def testbinarysuppresseswhennotterminalbutprettyself httpbin env mockenvironmentstdinisattytrue stdoutisattyfalse r http prettyall get httpbin bytes1024 seed1 envenv assert binarysuppressednotice decode in r def testbinaryincludedandcorrectwhensuitableself httpbin env mockenvironmentstdinisattytrue stdoutisattyfalse url httpbin bytes1024 seed1 r http get url envenv expected requests geturl content assert r expected
import requests

from .fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
from .utils import MockEnvironment, http


class TestBinaryRequestData:

    def test_binary_stdin(self, httpbin):
        with open(BIN_FILE_PATH, 'rb') as stdin:
            env = MockEnvironment(
                stdin=stdin,
                stdin_isatty=False,
                stdout_isatty=False
            )
            r = http('--print=B', 'POST', httpbin.url + '/post', env=env)
            assert r == BIN_FILE_CONTENT

    def test_binary_file_path(self, httpbin):
        env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
        r = http('--print=B', 'POST', httpbin.url + '/post',
                 '@' + BIN_FILE_PATH_ARG, env=env)
        assert r == BIN_FILE_CONTENT

    def test_binary_file_form(self, httpbin):
        env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
        r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
                 'test@' + BIN_FILE_PATH_ARG, env=env)
        assert bytes(BIN_FILE_CONTENT) in bytes(r)


class TestBinaryResponseData:

    def test_binary_suppresses_when_terminal(self, httpbin):
        r = http('GET', httpbin + '/bytes/1024?seed=1')
        assert BINARY_SUPPRESSED_NOTICE.decode() in r

    def test_binary_suppresses_when_not_terminal_but_pretty(self, httpbin):
        env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
        r = http('--pretty=all', 'GET', httpbin + '/bytes/1024?seed=1', env=env)
        assert BINARY_SUPPRESSED_NOTICE.decode() in r

    def test_binary_included_and_correct_when_suitable(self, httpbin):
        env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
        url = httpbin + '/bytes/1024?seed=1'
        r = http('GET', url, env=env)
        expected = requests.get(url).content
        assert r == expected