1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00
httpie-cli/tests/test_uploads.py

55 lines
2.0 KiB
Python
Raw Normal View History

2014-04-24 15:07:31 +03:00
import os
from unittest import TestCase
2014-04-24 15:07:31 +03:00
from httpie.input import ParseError
from tests import TestEnvironment, http, httpbin, HTTP_OK
from tests.fixtures import FILE_PATH_ARG, FILE_PATH, FILE_CONTENT
2014-04-24 15:07:31 +03:00
class MultipartFormDataFileUploadTest(TestCase):
2014-04-24 15:07:31 +03:00
def test_non_existent_file_raises_parse_error(self):
2014-04-24 16:48:01 +03:00
with self.assertRaises(ParseError):
http('--form', 'POST', httpbin('/post'), 'foo@/__does_not_exist__')
2014-04-24 15:07:31 +03:00
def test_upload_ok(self):
2014-04-24 16:48:01 +03:00
r = http('--form', '--verbose', 'POST', httpbin('/post'),
'test-file@%s' % FILE_PATH_ARG, 'foo=bar')
assert HTTP_OK in r
assert 'Content-Disposition: form-data; name="foo"' in r
assert 'Content-Disposition: form-data; name="test-file";' \
' filename="%s"' % os.path.basename(FILE_PATH) in r
assert r.count(FILE_CONTENT) == 2
assert '"foo": "bar"' in r
2014-04-24 15:07:31 +03:00
class RequestBodyFromFilePathTest(TestCase):
2014-04-24 15:07:31 +03:00
"""
`http URL @file'
"""
2014-04-24 16:48:01 +03:00
2014-04-24 15:07:31 +03:00
def test_request_body_from_file_by_path(self):
2014-04-24 16:48:01 +03:00
r = http('--verbose', 'POST', httpbin('/post'), '@' + FILE_PATH_ARG)
assert HTTP_OK in r
assert FILE_CONTENT in r
assert '"Content-Type": "text/plain"' in r
2014-04-24 15:07:31 +03:00
def test_request_body_from_file_by_path_with_explicit_content_type(self):
2014-04-24 16:48:01 +03:00
r = http('POST', httpbin('/post'), '@' + FILE_PATH_ARG,
'Content-Type:x-foo/bar')
assert HTTP_OK in r
assert FILE_CONTENT in r
assert '"Content-Type": "x-foo/bar"' in r
2014-04-24 15:07:31 +03:00
def test_request_body_from_file_by_path_no_field_name_allowed(self):
env = TestEnvironment(stdin_isatty=True)
2014-04-24 16:48:01 +03:00
r = http('POST', httpbin('/post'), 'field-name@' + FILE_PATH_ARG,
env=env)
assert 'perhaps you meant --form?' in r.stderr
2014-04-24 15:07:31 +03:00
def test_request_body_from_file_by_path_no_data_items_allowed(self):
2014-04-24 16:48:01 +03:00
env = TestEnvironment(stdin_isatty=False)
r = http('POST', httpbin('/post'), '@' + FILE_PATH_ARG, 'foo=bar',
env=env)
assert 'cannot be mixed' in r.stderr