Test improvements.

This commit is contained in:
Jakub Roztocil 2014-04-25 12:18:35 +02:00
parent 27faf06327
commit af4aa3a761
6 changed files with 47 additions and 31 deletions

View File

@ -157,7 +157,6 @@ class Download(object):
provided, it will be guessed from the response.
:param progress_file: Where to report download progress.
:type progress_file: file
"""
self._output_file = output_file

View File

@ -31,7 +31,7 @@ HTTP_OK_COLOR = (
def httpbin(path):
"""
Return a fully-qualified URL for `path`.
Return a fully-qualified httpbin URL for `path`.
>>> httpbin('/get')
'http://httpbin.org/get'
@ -41,10 +41,6 @@ def httpbin(path):
return url
def mk_config_dir():
return tempfile.mkdtemp(prefix='httpie_test_config_dir_')
class TestEnvironment(Environment):
"""
Environment subclass with reasonable defaults suitable for testing.
@ -79,23 +75,26 @@ class TestEnvironment(Environment):
def http(*args, **kwargs):
"""
Invoke `httpie.core.main()` with `args` and `kwargs`,
and return a `CLIResponse` subclass.
Run HTTPie and capture stderr/out and exist status.
It is either a `StrResponse`, or `BytesResponse`
Invoke `httpie.core.main()` with `args` and `kwargs`,
and return a `CLIResponse` subclass instance.
The return value is either a `StrCLIResponse`, or `BytesCLIResponse`
if unable to decode the output.
The response has the following attributes:
`stdout` is represented by the instance itself (print r)
`stderr`: text written to stderr
`exit_status`: the exit status
`json`: decoded JSON (if possible) or `None`
Exceptions are propagated except for SystemExit.
$ http GET example.org:
$ http --auth=user:password GET httpbin.org/basic-auth/user/password
>>> r = http('GET', 'example.org')
>>> r = http('-a', 'user:pw', httpbin('/basic-auth/user/pw'))
>>> type(r) == StrCLIResponse
True
>>> r.exit_status
@ -104,9 +103,10 @@ def http(*args, **kwargs):
''
>>> 'HTTP/1.1 200 OK' in r
True
>>> r.json is None
>>> r.json == {'authenticated': True, 'user': 'user'}
True
"""
env = kwargs.get('env')
if not env:
@ -200,3 +200,7 @@ class StrCLIResponse(str, BaseCLIResponse):
except ValueError:
pass
return self._json
def mk_config_dir():
return tempfile.mkdtemp(prefix='httpie_test_config_dir_')

View File

@ -11,8 +11,10 @@ class TestAuth:
r = http('--auth=user:password', 'GET',
httpbin('/basic-auth/user/password'))
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
assert r.json == {
'authenticated': True,
'user': 'user'
}
@pytest.mark.skipif(
requests.__version__ == '0.13.6',
@ -21,23 +23,29 @@ class TestAuth:
r = http('--auth-type=digest', '--auth=user:password', 'GET',
httpbin('/digest-auth/auth/user/password'))
assert HTTP_OK in r
assert r'"authenticated": true' in r
assert r'"user": "user"', r
assert r.json == {
'authenticated': True,
'user': 'user'
}
def test_password_prompt(self):
httpie.input.AuthCredentials._getpass = lambda self, prompt: 'password'
r = http('--auth', 'user', 'GET', httpbin('/basic-auth/user/password'))
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
assert r.json == {
'authenticated': True,
'user': 'user'
}
def test_credentials_in_url(self):
url = httpbin('/basic-auth/user/password')
url = 'http://user:password@' + url.split('http://', 1)[1]
r = http('GET', url)
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
assert r.json == {
'authenticated': True,
'user': 'user'
}
def test_credentials_in_url_auth_flag_has_priority(self):
"""When credentials are passed in URL and via -a at the same time,
@ -46,5 +54,7 @@ class TestAuth:
url = 'http://user:wrong_password@' + url.split('http://', 1)[1]
r = http('--auth=user:password', 'GET', url)
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
assert r.json == {
'authenticated': True,
'user': 'user'
}

View File

@ -14,17 +14,17 @@ class TestImplicitHTTPMethod:
def test_implicit_GET_with_headers(self):
r = http(httpbin('/headers'), 'Foo:bar')
assert HTTP_OK in r
assert '"Foo": "bar"' in r
assert r.json['headers']['Foo'] == 'bar'
def test_implicit_POST_json(self):
r = http(httpbin('/post'), 'hello=world')
assert HTTP_OK in r
assert r'\"hello\": \"world\"' in r
assert r.json['json'] == {'hello': 'world'}
def test_implicit_POST_form(self):
r = http('--form', httpbin('/post'), 'foo=bar')
assert HTTP_OK in r
assert '"foo": "bar"' in r
assert r.json['form'] == {'foo': 'bar'}
def test_implicit_POST_stdin(self):
with open(FILE_PATH) as f:
@ -45,8 +45,8 @@ class TestAutoContentTypeAndAcceptHeaders:
# https://github.com/jkbr/httpie/issues/62
r = http('GET', httpbin('/headers'))
assert HTTP_OK in r
assert '"Accept": "*/*"' in r
assert '"Content-Type": "application/json' not in r
assert r.json['headers']['Accept'] == '*/*'
assert 'Content-Type' not in r.json['headers']
def test_POST_no_data_no_auto_headers(self):
# JSON headers shouldn't be automatically set for POST with no data.

View File

@ -105,7 +105,8 @@ class TestDownloads:
assert body == r
def test_download_with_Content_Length(self):
download = Download(output_file=open(os.devnull, 'w'))
devnull = open(os.devnull, 'w')
download = Download(output_file=devnull, progress_file=devnull)
download.start(Response(
url=httpbin('/'),
headers={'Content-Length': 10}
@ -118,7 +119,8 @@ class TestDownloads:
assert not download.interrupted
def test_download_no_Content_Length(self):
download = Download(output_file=open(os.devnull, 'w'))
devnull = open(os.devnull, 'w')
download = Download(output_file=devnull, progress_file=devnull)
download.start(Response(url=httpbin('/')))
time.sleep(1.1)
download.chunk_downloaded(b'12345')
@ -126,7 +128,8 @@ class TestDownloads:
assert not download.interrupted
def test_download_interrupted(self):
download = Download(output_file=open(os.devnull, 'w'))
devnull = open(os.devnull, 'w')
download = Download(output_file=devnull, progress_file=devnull)
download.start(Response(
url=httpbin('/'),
headers={'Content-Length': 5}

View File

@ -95,4 +95,4 @@ class TestSessions:
r2 = http('--session=' + session_path, 'GET', httpbin('/get'),
env=self.env)
assert HTTP_OK in r2
assert r2.json['headers']['Foo'] in 'Bar'
assert r2.json['headers']['Foo'] == 'Bar'