2014-04-24 15:07:31 +03:00
|
|
|
"""Tests for dealing with binary request and response data."""
|
|
|
|
from httpie.compat import urlopen
|
2014-04-27 01:07:13 +03:00
|
|
|
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
|
2014-06-28 17:35:57 +03:00
|
|
|
from utils import TestEnvironment, http
|
2014-04-28 12:29:41 +03:00
|
|
|
from fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestBinaryRequestData:
|
2016-03-06 11:42:35 +02:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_binary_stdin(self, httpbin):
|
2014-04-24 15:07:31 +03:00
|
|
|
with open(BIN_FILE_PATH, 'rb') as stdin:
|
|
|
|
env = TestEnvironment(
|
|
|
|
stdin=stdin,
|
|
|
|
stdin_isatty=False,
|
|
|
|
stdout_isatty=False
|
|
|
|
)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('--print=B', 'POST', httpbin.url + '/post', env=env)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert r == BIN_FILE_CONTENT
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_binary_file_path(self, httpbin):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('--print=B', 'POST', httpbin.url + '/post',
|
2014-04-24 16:48:01 +03:00
|
|
|
'@' + BIN_FILE_PATH_ARG, env=env, )
|
2014-04-24 15:58:15 +03:00
|
|
|
assert r == BIN_FILE_CONTENT
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_binary_file_form(self, httpbin):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
|
2014-04-24 16:48:01 +03:00
|
|
|
'test@' + BIN_FILE_PATH_ARG, env=env)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert bytes(BIN_FILE_CONTENT) in bytes(r)
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestBinaryResponseData:
|
2014-04-24 15:07:31 +03:00
|
|
|
url = 'http://www.google.com/favicon.ico'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def bindata(self):
|
|
|
|
if not hasattr(self, '_bindata'):
|
|
|
|
self._bindata = urlopen(self.url).read()
|
|
|
|
return self._bindata
|
|
|
|
|
|
|
|
def test_binary_suppresses_when_terminal(self):
|
2014-04-24 16:48:01 +03:00
|
|
|
r = http('GET', self.url)
|
2014-04-24 15:58:15 +03:00
|
|
|
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):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
|
|
|
r = http('--pretty=all', 'GET', self.url,
|
|
|
|
env=env)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
def test_binary_included_and_correct_when_suitable(self):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
|
|
|
r = http('GET', self.url, env=env)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert r == self.bindata
|