Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbrzt committed Mar 4, 2024
1 parent db16bbe commit 3de7c82
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 147 deletions.
9 changes: 1 addition & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,8 @@ def _remote_httpbin_available():

@pytest.fixture
def remote_httpbin(_remote_httpbin_available):

if _remote_httpbin_available:
class Server(str):
"""Look like `pytest_httpbin.serve.Server` but only provide URL info."""
@property
def url(self):
return self

return Server('http://' + REMOTE_HTTPBIN_DOMAIN)
return 'http://' + REMOTE_HTTPBIN_DOMAIN
pytest.skip(f'{REMOTE_HTTPBIN_DOMAIN} not resolvable')


Expand Down
2 changes: 1 addition & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_bearer_auth(httpbin_both, token):
new=lambda self, prompt: 'password')
def test_password_prompt(httpbin):
r = http('--auth', 'user',
'GET', httpbin.url + '/basic-auth/user/password')
'GET', httpbin + '/basic-auth/user/password')
assert HTTP_OK in r
assert r.json == {'authenticated': True, 'user': 'user'}

Expand Down
6 changes: 3 additions & 3 deletions tests/test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ def test_binary_stdin(self, httpbin):
stdin_isatty=False,
stdout_isatty=False
)
r = http('--print=B', 'POST', httpbin.url + '/post', env=env)
r = http('--print=B', 'POST', httpbin + '/post', env=env)
assert r == BIN_FILE_CONTENT

def test_binary_file_path(self, httpbin):
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('--print=B', 'POST', httpbin.url + '/post',
r = http('--print=B', 'POST', httpbin + '/post',
'@' + BIN_FILE_PATH_ARG, env=env)
assert r == BIN_FILE_CONTENT

def test_binary_file_form(self, httpbin):
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
r = http('--print=B', '--form', 'POST', httpbin + '/post',
'test@' + BIN_FILE_PATH_ARG, env=env)
assert bytes(BIN_FILE_CONTENT) in bytes(r)

Expand Down
20 changes: 10 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,27 @@ def test_multiple_text_fields_with_same_field_name(self):

class TestQuerystring:
def test_query_string_params_in_url(self, httpbin):
r = http('--print=Hhb', 'GET', httpbin.url + '/get?a=1&b=2')
r = http('--print=Hhb', 'GET', httpbin + '/get?a=1&b=2')
path = '/get?a=1&b=2'
url = httpbin.url + path
url = httpbin + path
assert HTTP_OK in r
assert f'GET {path} HTTP/1.1' in r
assert f'"url": "{url}"' in r

def test_query_string_params_items(self, httpbin):
r = http('--print=Hhb', 'GET', httpbin.url + '/get', 'a==1')
r = http('--print=Hhb', 'GET', httpbin + '/get', 'a==1')
path = '/get?a=1'
url = httpbin.url + path
url = httpbin + path
assert HTTP_OK in r
assert f'GET {path} HTTP/1.1' in r
assert f'"url": "{url}"' in r

def test_query_string_params_in_url_and_items_with_duplicates(self,
httpbin):
r = http('--print=Hhb', 'GET',
httpbin.url + '/get?a=1&a=1', 'a==1', 'a==1')
httpbin + '/get?a=1&a=1', 'a==1', 'a==1')
path = '/get?a=1&a=1&a=1&a=1'
url = httpbin.url + path
url = httpbin + path
assert HTTP_OK in r
assert f'GET {path} HTTP/1.1' in r
assert f'"url": "{url}"' in r
Expand Down Expand Up @@ -320,11 +320,11 @@ def test_guess_when_method_set_but_invalid_and_item_exists(self):
class TestNoOptions:

def test_valid_no_options(self, httpbin):
r = http('--verbose', '--no-verbose', 'GET', httpbin.url + '/get')
r = http('--verbose', '--no-verbose', 'GET', httpbin + '/get')
assert 'GET /get HTTP/1.1' not in r

def test_invalid_no_options(self, httpbin):
r = http('--no-war', 'GET', httpbin.url + '/get',
r = http('--no-war', 'GET', httpbin + '/get',
tolerate_error_exit_status=True)
assert r.exit_status == ExitStatus.ERROR
assert 'unrecognized arguments: --no-war' in r.stderr
Expand All @@ -338,13 +338,13 @@ def test_ignore_stdin(self, httpbin):
stdin=StdinBytesIO(FILE_PATH.read_bytes()),
stdin_isatty=False,
)
r = http('--ignore-stdin', '--verbose', httpbin.url + '/get', env=env)
r = http('--ignore-stdin', '--verbose', httpbin + '/get', env=env)
assert HTTP_OK in r
assert 'GET /get HTTP' in r, "Don't default to POST."
assert FILE_CONTENT not in r, "Don't send stdin data."

def test_ignore_stdin_cannot_prompt_password(self, httpbin):
r = http('--ignore-stdin', '--auth=no-password', httpbin.url + '/get',
r = http('--ignore-stdin', '--auth=no-password', httpbin + '/get',
tolerate_error_exit_status=True)
assert r.exit_status == ExitStatus.ERROR
assert 'because --ignore-stdin' in r.stderr
Expand Down
4 changes: 2 additions & 2 deletions tests/test_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def assert_decompressed_equal(base64_compressed_data, expected_str):


def test_cannot_combine_compress_with_chunked(httpbin):
r = http('--compress', '--chunked', httpbin.url + '/get',
r = http('--compress', '--chunked', httpbin + '/get',
tolerate_error_exit_status=True)
assert r.exit_status == ExitStatus.ERROR
assert 'cannot combine --compress and --chunked' in r.stderr


def test_cannot_combine_compress_with_multipart(httpbin):
r = http('--compress', '--multipart', httpbin.url + '/get',
r = http('--compress', '--multipart', httpbin + '/get',
tolerate_error_exit_status=True)
assert r.exit_status == ExitStatus.ERROR
assert 'cannot combine --compress and --multipart' in r.stderr
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_default_options(httpbin):
env = MockEnvironment()
env.config['default_options'] = ['--form']
env.config.save()
r = http(httpbin.url + '/post', 'foo=bar', env=env)
r = http(httpbin + '/post', 'foo=bar', env=env)
assert r.json['form'] == {
"foo": "bar"
}
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_default_options_overwrite(httpbin):
env = MockEnvironment()
env.config['default_options'] = ['--form']
env.config.save()
r = http('--json', httpbin.url + '/post', 'foo=bar', env=env)
r = http('--json', httpbin + '/post', 'foo=bar', env=env)
assert r.json['json'] == {
"foo": "bar"
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cookie_on_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_explicit_user_set_cookie(httpbin, target_httpbin, request):
r = http(
'--follow',
httpbin + '/redirect-to',
f'url=={target_httpbin.url}/cookies',
f'url=={target_httpbin}/cookies',
'Cookie:a=b'
)
assert r.json == {'cookies': {}}
Expand Down
34 changes: 17 additions & 17 deletions tests/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_default_headers_case_insensitive(httpbin):
r = http(
'--debug',
'--print=H',
httpbin.url + '/post',
httpbin + '/post',
'CONTENT-TYPE:application/json-patch+json',
'a=b',
)
Expand All @@ -27,26 +27,26 @@ def test_default_headers_case_insensitive(httpbin):
# noinspection PyPep8Naming
class TestImplicitHTTPMethod:
def test_implicit_GET(self, httpbin):
r = http(httpbin.url + '/get')
r = http(httpbin + '/get')
assert HTTP_OK in r

def test_implicit_GET_with_headers(self, httpbin):
r = http(httpbin.url + '/headers', 'Foo:bar')
r = http(httpbin + '/headers', 'Foo:bar')
assert HTTP_OK in r
assert r.json['headers']['Foo'] == 'bar'

def test_implicit_POST_json(self, httpbin):
r = http(httpbin.url + '/post', 'hello=world')
r = http(httpbin + '/post', 'hello=world')
assert HTTP_OK in r
assert r.json['json'] == {'hello': 'world'}

def test_implicit_POST_form(self, httpbin):
r = http('--form', httpbin.url + '/post', 'foo=bar')
r = http('--form', httpbin + '/post', 'foo=bar')
assert HTTP_OK in r
assert r.json['form'] == {'foo': 'bar'}

def test_implicit_POST_raw(self, httpbin):
r = http('--raw', 'foo bar', httpbin.url + '/post')
r = http('--raw', 'foo bar', httpbin + '/post')
assert HTTP_OK in r
assert r.json['data'] == 'foo bar'

Expand All @@ -55,7 +55,7 @@ def test_implicit_POST_stdin(self, httpbin):
stdin_isatty=False,
stdin=BytesIO(FILE_PATH.read_bytes())
)
r = http('--form', httpbin.url + '/post', env=env)
r = http('--form', httpbin + '/post', env=env)
assert HTTP_OK in r


Expand All @@ -69,64 +69,64 @@ class TestAutoContentTypeAndAcceptHeaders:

def test_GET_no_data_no_auto_headers(self, httpbin):
# https://github.com/httpie/cli/issues/62
r = http('GET', httpbin.url + '/headers')
r = http('GET', httpbin + '/headers')
assert HTTP_OK in r
assert r.json['headers']['Accept'] == '*/*'
assert 'Content-Type' not in r.json['headers']

def test_POST_no_data_no_auto_headers(self, httpbin):
# JSON headers shouldn't be automatically set for POST with no data.
r = http('POST', httpbin.url + '/post')
r = http('POST', httpbin + '/post')
assert HTTP_OK in r
assert '"Accept": "*/*"' in r
assert '"Content-Type": "application/json' not in r

def test_POST_with_data_auto_JSON_headers(self, httpbin):
r = http('POST', httpbin.url + '/post', 'a=b')
r = http('POST', httpbin + '/post', 'a=b')
assert HTTP_OK in r
assert r.json['headers']['Accept'] == JSON_ACCEPT
assert r.json['headers']['Content-Type'] == 'application/json'

def test_GET_with_data_auto_JSON_headers(self, httpbin):
# JSON headers should automatically be set also for GET with data.
r = http('POST', httpbin.url + '/post', 'a=b')
r = http('POST', httpbin + '/post', 'a=b')
assert HTTP_OK in r
assert r.json['headers']['Accept'] == JSON_ACCEPT
assert r.json['headers']['Content-Type'] == 'application/json'

def test_POST_explicit_JSON_JSON_ACCEPT(self, httpbin):
r = http('--json', 'POST', httpbin.url + '/post')
r = http('--json', 'POST', httpbin + '/post')
assert HTTP_OK in r
assert r.json['headers']['Accept'] == JSON_ACCEPT
# Make sure Content-Type gets set even with no data.
# https://github.com/httpie/cli/issues/137
assert 'application/json' in r.json['headers']['Content-Type']

def test_GET_explicit_JSON_explicit_headers(self, httpbin):
r = http('--json', 'GET', httpbin.url + '/headers',
r = http('--json', 'GET', httpbin + '/headers',
'Accept:application/xml',
'Content-Type:application/xml')
assert HTTP_OK in r
assert '"Accept": "application/xml"' in r
assert '"Content-Type": "application/xml"' in r

def test_POST_form_auto_Content_Type(self, httpbin):
r = http('--form', 'POST', httpbin.url + '/post')
r = http('--form', 'POST', httpbin + '/post')
assert HTTP_OK in r
assert '"Content-Type": "application/x-www-form-urlencoded' in r

def test_POST_form_Content_Type_override(self, httpbin):
r = http('--form', 'POST', httpbin.url + '/post',
r = http('--form', 'POST', httpbin + '/post',
'Content-Type:application/xml')
assert HTTP_OK in r
assert '"Content-Type": "application/xml"' in r

def test_print_only_body_when_stdout_redirected_by_default(self, httpbin):
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('GET', httpbin.url + '/get', env=env)
r = http('GET', httpbin + '/get', env=env)
assert 'HTTP/' not in r

def test_print_overridable_when_stdout_redirected(self, httpbin):
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('--print=h', 'GET', httpbin.url + '/get', env=env)
r = http('--print=h', 'GET', httpbin + '/get', env=env)
assert HTTP_OK in r
2 changes: 1 addition & 1 deletion tests/test_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def test_download_with_redirect_original_url_used_for_filename(self, httpbin):
os.chdir(tmp_dirname)
try:
assert os.listdir('.') == []
http('--download', httpbin.url + '/redirect/1')
http('--download', httpbin + '/redirect/1')
assert os.listdir('.') == [expected_filename]
finally:
os.chdir(orig_cwd)
Loading

0 comments on commit 3de7c82

Please sign in to comment.