2020-05-21 15:50:00 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
2019-12-02 17:59:44 +01:00
|
|
|
import pytest
|
2020-05-23 12:12:15 +02:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2019-12-02 17:59:44 +01:00
|
|
|
|
|
|
|
from httpie.compat import is_windows
|
2021-08-05 20:58:43 +02:00
|
|
|
from httpie.constants import UTF8
|
2020-05-23 12:12:15 +02:00
|
|
|
from httpie.config import (
|
|
|
|
Config, DEFAULT_CONFIG_DIRNAME, DEFAULT_RELATIVE_LEGACY_CONFIG_DIR,
|
|
|
|
DEFAULT_RELATIVE_XDG_CONFIG_HOME, DEFAULT_WINDOWS_CONFIG_DIR,
|
|
|
|
ENV_HTTPIE_CONFIG_DIR, ENV_XDG_CONFIG_HOME, get_default_config_dir,
|
|
|
|
)
|
2021-05-05 14:13:39 +02:00
|
|
|
from .utils import HTTP_OK, MockEnvironment, http
|
2016-03-03 10:14:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_default_options(httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment()
|
2016-03-03 10:14:39 +01:00
|
|
|
env.config['default_options'] = ['--form']
|
|
|
|
env.config.save()
|
|
|
|
r = http(httpbin.url + '/post', 'foo=bar', env=env)
|
2019-12-02 17:43:16 +01:00
|
|
|
assert r.json['form'] == {
|
|
|
|
"foo": "bar"
|
|
|
|
}
|
2016-03-03 10:14:39 +01:00
|
|
|
|
|
|
|
|
2019-12-02 17:43:16 +01:00
|
|
|
def test_config_file_not_valid(httpbin):
|
|
|
|
env = MockEnvironment()
|
|
|
|
env.create_temp_config_dir()
|
2021-08-05 20:58:43 +02:00
|
|
|
(env.config_dir / Config.FILENAME).write_text('{invalid json}', encoding=UTF8)
|
2019-12-02 17:43:16 +01:00
|
|
|
r = http(httpbin + '/get', env=env)
|
2019-08-29 14:05:00 +02:00
|
|
|
assert HTTP_OK in r
|
2019-12-02 17:43:16 +01:00
|
|
|
assert 'http: warning' in r.stderr
|
|
|
|
assert 'invalid config file' in r.stderr
|
|
|
|
|
|
|
|
|
2019-12-02 17:59:44 +01:00
|
|
|
@pytest.mark.skipif(is_windows, reason='cannot chmod 000 on Windows')
|
|
|
|
def test_config_file_inaccessible(httpbin):
|
2019-12-02 17:43:16 +01:00
|
|
|
env = MockEnvironment()
|
|
|
|
env.create_temp_config_dir()
|
|
|
|
config_path = env.config_dir / Config.FILENAME
|
|
|
|
assert not config_path.exists()
|
|
|
|
config_path.touch(0o000)
|
|
|
|
assert config_path.exists()
|
|
|
|
r = http(httpbin + '/get', env=env)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert 'http: warning' in r.stderr
|
|
|
|
assert 'cannot read config file' in r.stderr
|
2019-08-29 14:05:00 +02:00
|
|
|
|
|
|
|
|
2016-03-03 10:14:39 +01:00
|
|
|
def test_default_options_overwrite(httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment()
|
2016-03-03 10:14:39 +01:00
|
|
|
env.config['default_options'] = ['--form']
|
|
|
|
env.config.save()
|
|
|
|
r = http('--json', httpbin.url + '/post', 'foo=bar', env=env)
|
2019-12-02 17:43:16 +01:00
|
|
|
assert r.json['json'] == {
|
|
|
|
"foo": "bar"
|
|
|
|
}
|
2020-05-21 15:50:00 +02:00
|
|
|
|
|
|
|
|
2020-05-23 12:12:15 +02:00
|
|
|
@pytest.mark.skipif(is_windows, reason='XDG_CONFIG_HOME needs *nix')
|
|
|
|
def test_explicit_xdg_config_home(monkeypatch: MonkeyPatch, tmp_path: Path):
|
|
|
|
home_dir = tmp_path
|
|
|
|
monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False)
|
2020-05-23 12:14:09 +02:00
|
|
|
monkeypatch.setenv('HOME', str(home_dir))
|
2020-05-23 12:12:15 +02:00
|
|
|
custom_xdg_config_home = home_dir / 'custom_xdg_config_home'
|
|
|
|
monkeypatch.setenv(ENV_XDG_CONFIG_HOME, str(custom_xdg_config_home))
|
|
|
|
expected_config_dir = custom_xdg_config_home / DEFAULT_CONFIG_DIRNAME
|
|
|
|
assert get_default_config_dir() == expected_config_dir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(is_windows, reason='XDG_CONFIG_HOME needs *nix')
|
|
|
|
def test_default_xdg_config_home(monkeypatch: MonkeyPatch, tmp_path: Path):
|
|
|
|
home_dir = tmp_path
|
|
|
|
monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False)
|
|
|
|
monkeypatch.delenv(ENV_XDG_CONFIG_HOME, raising=False)
|
2020-05-23 12:14:09 +02:00
|
|
|
monkeypatch.setenv('HOME', str(home_dir))
|
2020-05-23 12:12:15 +02:00
|
|
|
expected_config_dir = (
|
|
|
|
home_dir
|
|
|
|
/ DEFAULT_RELATIVE_XDG_CONFIG_HOME
|
|
|
|
/ DEFAULT_CONFIG_DIRNAME
|
|
|
|
)
|
|
|
|
assert get_default_config_dir() == expected_config_dir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(is_windows, reason='legacy config dir needs *nix')
|
|
|
|
def test_legacy_config_dir(monkeypatch: MonkeyPatch, tmp_path: Path):
|
|
|
|
home_dir = tmp_path
|
|
|
|
monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False)
|
2020-05-23 12:14:09 +02:00
|
|
|
monkeypatch.setenv('HOME', str(home_dir))
|
2020-05-23 12:12:15 +02:00
|
|
|
legacy_config_dir = home_dir / DEFAULT_RELATIVE_LEGACY_CONFIG_DIR
|
|
|
|
legacy_config_dir.mkdir()
|
|
|
|
assert get_default_config_dir() == legacy_config_dir
|
|
|
|
|
|
|
|
|
|
|
|
def test_custom_config_dir(monkeypatch: MonkeyPatch, tmp_path: Path):
|
|
|
|
httpie_config_dir = tmp_path / 'custom/directory'
|
|
|
|
monkeypatch.setenv(ENV_HTTPIE_CONFIG_DIR, str(httpie_config_dir))
|
|
|
|
assert get_default_config_dir() == httpie_config_dir
|
2020-05-21 15:50:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not is_windows, reason='windows-only')
|
2020-05-23 12:12:15 +02:00
|
|
|
def test_windows_config_dir(monkeypatch: MonkeyPatch):
|
|
|
|
monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False)
|
|
|
|
assert get_default_config_dir() == DEFAULT_WINDOWS_CONFIG_DIR
|