httpie-cli/tests/test_uploads.py

74 lines
2.8 KiB
Python
Raw Normal View History

2014-04-24 14:07:31 +02:00
import os
2014-04-24 17:08:40 +02:00
import pytest
2014-04-24 14:07:31 +02:00
from httpie.input import ParseError
from utils import TestEnvironment, http, HTTP_OK
from fixtures import FILE_PATH_ARG, FILE_PATH, FILE_CONTENT
2014-04-24 14:07:31 +02:00
class TestMultipartFormDataFileUpload:
def test_non_existent_file_raises_parse_error(self, httpbin):
2014-04-24 17:08:40 +02:00
with pytest.raises(ParseError):
http('--form',
'POST', httpbin.url + '/post', 'foo@/__does_not_exist__')
2014-04-24 14:07:31 +02:00
def test_upload_ok(self, httpbin):
r = http('--form', '--verbose', 'POST', httpbin.url + '/post',
2014-04-24 15:48:01 +02:00
'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 FILE_CONTENT in r
assert '"foo": "bar"' in r
2014-04-24 14:07:31 +02:00
def test_upload_multiple_fields_with_the_same_name(self, httpbin):
r = http('--form', '--verbose', 'POST', httpbin.url + '/post',
'test-file@%s' % FILE_PATH_ARG,
'test-file@%s' % FILE_PATH_ARG)
assert HTTP_OK in r
assert r.count('Content-Disposition: form-data; name="test-file";'
' filename="%s"' % os.path.basename(FILE_PATH)) == 2
# Should be 4, but is 3 because httpbin
# doesn't seem to support filed field lists
assert r.count(FILE_CONTENT) in [3, 4]
2014-04-24 14:07:31 +02:00
class TestRequestBodyFromFilePath:
2014-04-24 14:07:31 +02:00
"""
`http URL @file'
"""
2014-04-24 15:48:01 +02:00
def test_request_body_from_file_by_path(self, httpbin):
r = http('--verbose',
'POST', httpbin.url + '/post', '@' + FILE_PATH_ARG)
assert HTTP_OK in r
2014-04-26 17:53:01 +02:00
assert FILE_CONTENT in r, r
assert '"Content-Type": "text/plain"' in r
2014-04-24 14:07:31 +02:00
def test_request_body_from_file_by_path_with_explicit_content_type(
self, httpbin):
r = http('--verbose',
'POST', httpbin.url + '/post', '@' + FILE_PATH_ARG,
'Content-Type:text/plain; charset=utf8')
assert HTTP_OK in r
assert FILE_CONTENT in r
assert 'Content-Type: text/plain; charset=utf8' in r
2014-04-24 14:07:31 +02:00
def test_request_body_from_file_by_path_no_field_name_allowed(
self, httpbin):
2014-04-24 14:07:31 +02:00
env = TestEnvironment(stdin_isatty=True)
r = http('POST', httpbin.url + '/post', 'field-name@' + FILE_PATH_ARG,
env=env, error_exit_ok=True)
assert 'perhaps you meant --form?' in r.stderr
2014-04-24 14:07:31 +02:00
def test_request_body_from_file_by_path_no_data_items_allowed(
self, httpbin):
2014-04-24 15:48:01 +02:00
env = TestEnvironment(stdin_isatty=False)
r = http('POST', httpbin.url + '/post', '@' + FILE_PATH_ARG, 'foo=bar',
env=env, error_exit_ok=True)
assert 'cannot be mixed' in r.stderr