-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathtest_local_config.py
More file actions
173 lines (125 loc) · 5.67 KB
/
Copy pathtest_local_config.py
File metadata and controls
173 lines (125 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
from pathlib import Path
import pytest
from _pytest.monkeypatch import MonkeyPatch
from httpie.config import (
ENV_HTTPIE_LOCAL_CONFIG, LOCAL_CONFIG_FILENAME, LocalConfig,
get_local_config_path, load_local_config,
)
from .utils import MockEnvironment, http
URL = 'http://example.com/path'
@pytest.fixture
def local_config(monkeypatch: MonkeyPatch, tmp_path: Path):
"""Yield a callable that writes a local config and points HTTPIE_LOCAL_CONFIG at it."""
path = tmp_path / LOCAL_CONFIG_FILENAME
def write(data):
if isinstance(data, str):
path.write_text(data)
else:
path.write_text(json.dumps(data))
monkeypatch.setenv(ENV_HTTPIE_LOCAL_CONFIG, str(path))
return path
monkeypatch.delenv(ENV_HTTPIE_LOCAL_CONFIG, raising=False)
yield write
# --- Path resolution -------------------------------------------------------
def test_local_config_path_env_override(monkeypatch, tmp_path):
custom = tmp_path / 'somewhere/.httpie'
monkeypatch.setenv(ENV_HTTPIE_LOCAL_CONFIG, str(custom))
assert get_local_config_path() == custom
def test_local_config_path_defaults_to_cwd(monkeypatch, tmp_path):
monkeypatch.delenv(ENV_HTTPIE_LOCAL_CONFIG, raising=False)
monkeypatch.chdir(tmp_path)
assert get_local_config_path() == tmp_path / LOCAL_CONFIG_FILENAME
# --- Loader ----------------------------------------------------------------
def test_load_returns_none_when_absent(monkeypatch, tmp_path):
monkeypatch.setenv(ENV_HTTPIE_LOCAL_CONFIG, str(tmp_path / 'missing'))
assert load_local_config() is None
def test_load_returns_typed_object(monkeypatch, tmp_path):
path = tmp_path / LOCAL_CONFIG_FILENAME
path.write_text(json.dumps({
'default_options': ['--form'],
'headers': {'X-A': '1'},
'query': {'q': 'v'},
'mystery': 'ignored',
}))
monkeypatch.setenv(ENV_HTTPIE_LOCAL_CONFIG, str(path))
cfg = load_local_config()
assert isinstance(cfg, LocalConfig)
assert cfg.default_options == ['--form']
assert cfg.headers == {'X-A': '1'}
assert cfg.query == {'q': 'v'}
assert 'mystery' not in cfg
assert not cfg.is_empty()
def test_load_empty_object_is_empty(monkeypatch, tmp_path):
path = tmp_path / LOCAL_CONFIG_FILENAME
path.write_text('{}')
monkeypatch.setenv(ENV_HTTPIE_LOCAL_CONFIG, str(path))
cfg = load_local_config()
assert cfg.is_empty()
# --- Integration via --offline --------------------------------------------
def test_no_local_config_is_noop(monkeypatch, tmp_path):
monkeypatch.setenv(ENV_HTTPIE_LOCAL_CONFIG, str(tmp_path / 'missing'))
r = http('--offline', URL, env=MockEnvironment())
assert 'loaded local config' not in r.stderr
assert 'X-' not in r # no surprise headers
def test_default_options_from_local(local_config):
local_config({'default_options': ['--form']})
r = http('--offline', 'POST', URL, 'foo=bar', env=MockEnvironment())
assert 'Content-Type: application/x-www-form-urlencoded' in r
assert 'foo=bar' in r
def test_cli_overrides_local_default_options(local_config):
local_config({'default_options': ['--form']})
r = http('--offline', '--json', 'POST', URL, 'foo=bar', env=MockEnvironment())
assert 'Content-Type: application/json' in r
def test_local_overrides_user_default_options(local_config):
env = MockEnvironment()
env.config['default_options'] = ['--form']
env.config.save()
local_config({'default_options': ['--json']})
r = http('--offline', 'POST', URL, 'foo=bar', env=env)
assert 'Content-Type: application/json' in r
def test_header_from_local(local_config):
local_config({'headers': {'X-Custom': 'from-local'}})
r = http('--offline', URL, env=MockEnvironment())
assert 'X-Custom: from-local' in r
def test_cli_overrides_local_header(local_config):
local_config({'headers': {'X-Custom': 'from-local'}})
r = http('--offline', URL, 'X-Custom:from-cli', env=MockEnvironment())
assert 'X-Custom: from-cli' in r
assert 'from-local' not in r
def test_local_header_case_insensitive_against_cli(local_config):
local_config({'headers': {'X-Custom': 'from-local'}})
r = http('--offline', URL, 'x-custom:from-cli', env=MockEnvironment())
assert 'from-cli' in r
assert 'from-local' not in r
def test_query_from_local(local_config):
local_config({'query': {'api_version': '2'}})
r = http('--offline', URL, env=MockEnvironment())
assert 'GET /path?api_version=2' in r
def test_cli_overrides_local_query(local_config):
local_config({'query': {'api_version': '2'}})
r = http('--offline', URL, 'api_version==3', env=MockEnvironment())
assert 'api_version=3' in r
assert 'api_version=2' not in r
def test_stderr_notice_when_loaded(local_config):
local_config({'headers': {'X-Custom': 'x'}})
r = http('--offline', URL, env=MockEnvironment())
assert 'loaded local config' in r.stderr
assert '1 headers' in r.stderr
def test_no_stderr_notice_when_empty(local_config):
local_config({})
r = http('--offline', URL, env=MockEnvironment())
assert 'loaded local config' not in r.stderr
def test_invalid_json_warns_and_continues(local_config):
local_config('{not valid json')
r = http('--offline', URL, env=MockEnvironment())
assert 'warning' in r.stderr
assert 'invalid local config file' in r.stderr
def test_non_object_top_level_warns(local_config):
local_config([1, 2, 3])
r = http('--offline', URL, env=MockEnvironment())
assert 'warning' in r.stderr
def test_unknown_keys_ignored(local_config):
local_config({'headers': {'X-Custom': 'x'}, 'mystery': 'value'})
r = http('--offline', URL, env=MockEnvironment())
assert 'X-Custom: x' in r