2014-04-24 15:07:31 +03:00
|
|
|
import os
|
|
|
|
|
2014-04-24 18:08:40 +03:00
|
|
|
import pytest
|
|
|
|
|
2014-04-24 15:07:31 +03:00
|
|
|
from httpie.input import ParseError
|
2014-06-28 17:35:57 +03:00
|
|
|
from utils import TestEnvironment, http, HTTP_OK
|
2014-04-28 12:29:41 +03:00
|
|
|
from fixtures import FILE_PATH_ARG, FILE_PATH, FILE_CONTENT
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestMultipartFormDataFileUpload:
|
2014-06-28 17:35:57 +03:00
|
|
|
|
|
|
|
def test_non_existent_file_raises_parse_error(self, httpbin):
|
2014-04-24 18:08:40 +03:00
|
|
|
with pytest.raises(ParseError):
|
2014-06-28 17:35:57 +03:00
|
|
|
http('--form',
|
|
|
|
'POST', httpbin.url + '/post', 'foo@/__does_not_exist__')
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_upload_ok(self, httpbin):
|
|
|
|
r = http('--form', '--verbose', 'POST', httpbin.url + '/post',
|
2014-04-24 16:48:01 +03:00
|
|
|
'test-file@%s' % FILE_PATH_ARG, 'foo=bar')
|
2014-04-24 15:58:15 +03:00
|
|
|
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
|
2014-06-28 17:35:57 +03:00
|
|
|
assert FILE_CONTENT in r
|
2014-04-24 15:58:15 +03:00
|
|
|
assert '"foo": "bar"' in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestRequestBodyFromFilePath:
|
2014-04-24 15:07:31 +03:00
|
|
|
"""
|
|
|
|
`http URL @file'
|
|
|
|
|
|
|
|
"""
|
2014-04-24 16:48:01 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_request_body_from_file_by_path(self, httpbin):
|
|
|
|
r = http('--verbose',
|
|
|
|
'POST', httpbin.url + '/post', '@' + FILE_PATH_ARG)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r
|
2014-04-26 18:53:01 +03:00
|
|
|
assert FILE_CONTENT in r, r
|
2014-04-24 15:58:15 +03:00
|
|
|
assert '"Content-Type": "text/plain"' in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03: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')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FILE_CONTENT in r
|
2014-06-28 17:35:57 +03:00
|
|
|
assert 'Content-Type: text/plain; charset=utf8' in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_request_body_from_file_by_path_no_field_name_allowed(
|
|
|
|
self, httpbin):
|
2014-04-24 15:07:31 +03:00
|
|
|
env = TestEnvironment(stdin_isatty=True)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('POST', httpbin.url + '/post', 'field-name@' + FILE_PATH_ARG,
|
2014-04-26 16:06:51 +03:00
|
|
|
env=env, error_exit_ok=True)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert 'perhaps you meant --form?' in r.stderr
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_request_body_from_file_by_path_no_data_items_allowed(
|
|
|
|
self, httpbin):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(stdin_isatty=False)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('POST', httpbin.url + '/post', '@' + FILE_PATH_ARG, 'foo=bar',
|
2014-04-26 16:06:51 +03:00
|
|
|
env=env, error_exit_ok=True)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert 'cannot be mixed' in r.stderr
|