2014-04-24 14:07:31 +02:00
|
|
|
"""Tests for dealing with binary request and response data."""
|
2017-03-08 09:49:51 +01:00
|
|
|
import requests
|
|
|
|
|
2016-11-24 00:58:41 +01:00
|
|
|
from fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG
|
2014-04-27 00:07:13 +02:00
|
|
|
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
|
2017-12-28 18:17:48 +01:00
|
|
|
from utils import MockEnvironment, http
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestBinaryRequestData:
|
2016-03-06 10:42:35 +01:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_binary_stdin(self, httpbin):
|
2014-04-24 14:07:31 +02:00
|
|
|
with open(BIN_FILE_PATH, 'rb') as stdin:
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment(
|
2014-04-24 14:07:31 +02:00
|
|
|
stdin=stdin,
|
|
|
|
stdin_isatty=False,
|
|
|
|
stdout_isatty=False
|
|
|
|
)
|
2014-06-28 16:35:57 +02:00
|
|
|
r = http('--print=B', 'POST', httpbin.url + '/post', env=env)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r == BIN_FILE_CONTENT
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_binary_file_path(self, httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
2014-06-28 16:35:57 +02:00
|
|
|
r = http('--print=B', 'POST', httpbin.url + '/post',
|
2014-04-24 15:48:01 +02:00
|
|
|
'@' + BIN_FILE_PATH_ARG, env=env, )
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r == BIN_FILE_CONTENT
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_binary_file_form(self, httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
2014-06-28 16:35:57 +02:00
|
|
|
r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
|
2014-04-24 15:48:01 +02:00
|
|
|
'test@' + BIN_FILE_PATH_ARG, env=env)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert bytes(BIN_FILE_CONTENT) in bytes(r)
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestBinaryResponseData:
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2017-03-08 09:49:51 +01:00
|
|
|
def test_binary_suppresses_when_terminal(self, httpbin):
|
|
|
|
r = http('GET', httpbin + '/bytes/1024')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2017-03-08 09:49:51 +01:00
|
|
|
def test_binary_suppresses_when_not_terminal_but_pretty(self, httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
2017-03-08 09:49:51 +01:00
|
|
|
r = http('--pretty=all', 'GET', httpbin + '/bytes/1024', env=env)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2017-03-08 09:49:51 +01:00
|
|
|
def test_binary_included_and_correct_when_suitable(self, httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
2017-03-08 09:49:51 +01:00
|
|
|
url = httpbin + '/bytes/1024?seed=1'
|
|
|
|
r = http('GET', url, env=env)
|
|
|
|
expected = requests.get(url).content
|
|
|
|
assert r == expected
|