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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
"""High-level tests."""
import io
from unittest import mock
import pytest
import httpie
import httpie.__main__
from .fixtures import FILE_CONTENT, FILE_PATH
from httpie.cli.exceptions import ParseError
from httpie.context import Environment
from httpie.encoding import UTF8
from httpie.status import ExitStatus
from .utils import HTTP_OK, MockEnvironment, StdinBytesIO, http
def test_main_entry_point():
# Patch stdin to bypass pytest capture
with mock.patch.object(Environment, 'stdin', io.StringIO()):
assert httpie.__main__.main() == ExitStatus.ERROR.value
@mock.patch('httpie.core.main')
def test_main_entry_point_keyboard_interrupt(main):
main.side_effect = KeyboardInterrupt()
with mock.patch.object(Environment, 'stdin', io.StringIO()):
assert httpie.__main__.main() == ExitStatus.ERROR_CTRL_C.value
def test_debug():
r = http('--debug')
assert r.exit_status == ExitStatus.SUCCESS
assert f'HTTPie {httpie.__version__}' in r.stderr
def test_help():
r = http('--help', tolerate_error_exit_status=True)
assert r.exit_status == ExitStatus.SUCCESS
assert 'https://github.com/httpie/cli/issues' in r
def test_version():
r = http('--version', tolerate_error_exit_status=True)
assert r.exit_status == ExitStatus.SUCCESS
assert httpie.__version__ == r.strip()
def test_GET(httpbin_both):
r = http('GET', httpbin_both + '/get')
assert HTTP_OK in r
def test_path_dot_normalization():
r = http(
'--offline',
'example.org/../../etc/password',
'param==value'
)
assert 'GET /etc/password?param=value' in r
def test_path_as_is():
r = http(
'--offline',
'--path-as-is',
'example.org/../../etc/password',
'param==value'
)
assert 'GET /../../etc/password?param=value' in r
def test_DELETE(httpbin_both):
r = http('DELETE', httpbin_both + '/delete')
assert HTTP_OK in r
def test_PUT(httpbin_both):
r = http('PUT', httpbin_both + '/put', 'foo=bar')
assert HTTP_OK in r
assert r.json['json']['foo'] == 'bar'
def test_POST_JSON_data(httpbin_both):
r = http('POST', httpbin_both + '/post', 'foo=bar')
assert HTTP_OK in r
assert r.json['json']['foo'] == 'bar'
def test_POST_form(httpbin_both):
r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar')
assert HTTP_OK in r
assert '"foo": "bar"' in r
def test_POST_form_multiple_values(httpbin_both):
r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar', 'foo=baz')
assert HTTP_OK in r
assert r.json['form'] == {
'foo': ['bar', 'baz']
}
def test_POST_raw(httpbin_both):
r = http('--raw', 'foo bar', 'POST', httpbin_both + '/post')
assert HTTP_OK in r
assert '"foo bar"' in r
def test_POST_stdin(httpbin_both):
env = MockEnvironment(
stdin=StdinBytesIO(FILE_PATH.read_bytes()),
stdin_isatty=False,
)
r = http('--form', 'POST', httpbin_both + '/post', env=env)
assert HTTP_OK in r
assert FILE_CONTENT in r
def test_POST_file(httpbin_both):
r = http('--form', 'POST', httpbin_both + '/post', f'file@{FILE_PATH}')
assert HTTP_OK in r
assert FILE_CONTENT in r
def test_form_POST_file_redirected_stdin(httpbin):
"""
<https://github.com/httpie/cli/issues/840>
"""
with open(FILE_PATH, encoding=UTF8):
r = http(
'--form',
'POST',
httpbin + '/post',
f'file@{FILE_PATH}',
tolerate_error_exit_status=True,
env=MockEnvironment(
stdin=StdinBytesIO(FILE_PATH.read_bytes()),
stdin_isatty=False,
),
)
assert r.exit_status == ExitStatus.ERROR
assert 'cannot be mixed' in r.stderr
def test_raw_POST_key_values_supplied(httpbin):
r = http(
'--raw',
'foo bar',
'POST',
httpbin + '/post',
'foo=bar',
tolerate_error_exit_status=True,
)
assert r.exit_status == ExitStatus.ERROR
assert 'cannot be mixed' in r.stderr
def test_raw_POST_redirected_stdin(httpbin):
r = http(
'--raw',
'foo bar',
'POST',
httpbin + '/post',
tolerate_error_exit_status=True,
env=MockEnvironment(
stdin='some=value',
stdin_isatty=False,
),
)
assert r.exit_status == ExitStatus.ERROR
assert 'cannot be mixed' in r.stderr
def test_headers(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Foo:bar')
assert HTTP_OK in r
assert '"User-Agent": "HTTPie' in r, r
assert '"Foo": "bar"' in r
def test_headers_unset(httpbin_both):
r = http('GET', httpbin_both + '/headers')
assert 'Accept' in r.json['headers'] # default Accept present
r = http('GET', httpbin_both + '/headers', 'Accept:')
assert 'Accept' not in r.json['headers'] # default Accept unset
@pytest.mark.skip('unimplemented')
def test_unset_host_header(httpbin_both):
r = http('GET', httpbin_both + '/headers')
assert 'Host' in r.json['headers'] # default Host present
r = http('GET', httpbin_both + '/headers', 'Host:')
assert 'Host' not in r.json['headers'] # default Host unset
def test_unset_useragent_header(httpbin_both):
r = http('GET', httpbin_both + '/headers')
assert 'User-Agent' in r.json['headers'] # default User-Agent present
r = http('GET', httpbin_both + '/headers', 'User-Agent:')
assert 'User-Agent' not in r.json['headers'] # default User-Agent unset
def test_headers_empty_value(httpbin_both):
r = http('GET', httpbin_both + '/headers')
assert r.json['headers']['Accept'] # default Accept has value
r = http('GET', httpbin_both + '/headers', 'Accept;')
assert r.json['headers']['Accept'] == '' # Accept has no value
def test_headers_empty_value_with_value_gives_error(httpbin):
with pytest.raises(ParseError):
http('GET', httpbin + '/headers', 'Accept;SYNTAX_ERROR')
def test_headers_omit(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Accept:')
assert 'Accept' not in r.json['headers']
def test_headers_multiple_omit(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Foo:bar', 'Bar:baz',
'Foo:', 'Baz:quux')
assert 'Foo' not in r.json['headers']
assert r.json['headers']['Bar'] == 'baz'
assert r.json['headers']['Baz'] == 'quux'
def test_headers_same_after_omit(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Foo:bar', 'Foo:',
'Foo:quux')
assert r.json['headers']['Foo'] == 'quux'
def test_headers_fully_omit(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Foo:bar', 'Foo:baz',
'Foo:')
assert 'Foo' not in r.json['headers']
def test_headers_multiple_values(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Foo:bar', 'Foo:baz')
assert r.json['headers']['Foo'] == 'bar,baz'
def test_headers_multiple_values_repeated(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Foo:bar', 'Foo:baz',
'Foo:bar')
assert r.json['headers']['Foo'] == 'bar,baz,bar'
@pytest.mark.parametrize("headers, expected", [
(
["Foo;", "Foo:bar"],
",bar"
),
(
["Foo:bar", "Foo;"],
"bar,"
),
(
["Foo:bar", "Foo;", "Foo:baz"],
"bar,,baz"
),
])
def test_headers_multiple_values_with_empty(httpbin_both, headers, expected):
r = http('GET', httpbin_both + '/headers', *headers)
assert r.json['headers']['Foo'] == expected
def test_headers_multiple_values_mixed(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Foo:bar', 'Vary:XXX',
'Foo:baz', 'Vary:YYY', 'Foo:quux')
assert r.json['headers']['Vary'] == 'XXX,YYY'
assert r.json['headers']['Foo'] == 'bar,baz,quux'
def test_headers_preserve_prepared_headers(httpbin_both):
r = http('POST', httpbin_both + '/post', 'Content-Length:0',
'--raw', 'foo')
assert r.json['headers']['Content-Length'] == '3'
@pytest.mark.parametrize('pretty', ['format', 'none'])
def test_headers_multiple_headers_representation(httpbin_both, pretty):
r = http('--offline', '--pretty', pretty, 'example.org',
'A:A', 'A:B', 'A:C', 'B:A', 'B:B', 'C:C', 'c:c')
assert 'A: A' in r
assert 'A: B' in r
assert 'A: C' in r
assert 'B: A' in r
assert 'B: B' in r
assert 'C: C' in r
assert 'c: c' in r
def test_response_headers_multiple(http_server):
r = http('GET', http_server + '/headers', 'Foo:bar', 'Foo:baz')
assert 'Foo: bar' in r
assert 'Foo: baz' in r
def test_response_headers_multiple_repeated(http_server):
r = http('GET', http_server + '/headers', 'Foo:bar', 'Foo:baz',
'Foo:bar')
assert r.count('Foo: bar') == 2
assert 'Foo: baz' in r
@pytest.mark.parametrize('pretty', ['format', 'none'])
def test_response_headers_multiple_representation(http_server, pretty):
r = http('--pretty', pretty, http_server + '/headers',
'A:A', 'A:B', 'A:C', 'B:A', 'B:B', 'C:C', 'C:c')
assert 'A: A' in r
assert 'A: B' in r
assert 'A: C' in r
assert 'B: A' in r
assert 'B: B' in r
assert 'C: C' in r
assert 'C: c' in r
def test_json_input_preserve_order(httpbin_both):
r = http('PATCH', httpbin_both + '/patch',
'order:={"map":{"1":"first","2":"second"}}')
assert HTTP_OK in r
assert r.json['data'] == \
'{"order": {"map": {"1": "first", "2": "second"}}}'
@pytest.mark.parametrize('extra_args, expected_content_length', [
(
['Content-Length:0'],
'0'
),
(
['Content-Length:xxx'],
'xxx',
),
(
['--raw=data'],
'4'
),
(
['query[param]=something'],
'33'
)
])
def test_options_content_length_preservation(httpbin, extra_args, expected_content_length):
r = http(
'--offline',
'OPTIONS',
httpbin + '/anything',
*extra_args
)
assert f'Content-Length: {expected_content_length}' in r
@pytest.mark.parametrize('method', ['options', 'Options', 'OPTIONS'])
def test_options_dropping_redundant_content_length(httpbin, method):
r = http(
'--offline',
method,
httpbin + '/anything'
)
assert 'Content-Length' not in r
|