1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-28 08:38:44 +02:00
httpie-cli/tests/test_binary.py

51 lines
1.9 KiB
Python
Raw Normal View History

2014-04-24 15:07:31 +03:00
"""Tests for dealing with binary request and response data."""
import requests
2016-11-24 01:58:41 +02:00
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
2014-04-24 15:07:31 +03:00
class TestBinaryRequestData:
def test_binary_stdin(self, httpbin):
2014-04-24 15:07:31 +03:00
with open(BIN_FILE_PATH, 'rb') as stdin:
env = MockEnvironment(
2014-04-24 15:07:31 +03:00
stdin=stdin,
stdin_isatty=False,
stdout_isatty=False
)
r = http('--print=B', 'POST', httpbin.url + '/post', env=env)
assert r == BIN_FILE_CONTENT
2014-04-24 15:07:31 +03:00
def test_binary_file_path(self, httpbin):
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('--print=B', 'POST', httpbin.url + '/post',
2014-04-24 16:48:01 +03:00
'@' + BIN_FILE_PATH_ARG, env=env, )
assert r == BIN_FILE_CONTENT
2014-04-24 15:07:31 +03:00
def test_binary_file_form(self, httpbin):
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
2014-04-24 16:48:01 +03:00
'test@' + BIN_FILE_PATH_ARG, env=env)
assert bytes(BIN_FILE_CONTENT) in bytes(r)
2014-04-24 15:07:31 +03:00
class TestBinaryResponseData:
2014-04-24 15:07:31 +03:00
def test_binary_suppresses_when_terminal(self, httpbin):
r = http('GET', httpbin + '/bytes/1024?seed=1')
assert BINARY_SUPPRESSED_NOTICE.decode() in r
2014-04-24 15:07:31 +03:00
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
2014-04-24 15:07:31 +03:00
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