Test suite cleanup

This commit is contained in:
Jakub Roztocil 2016-03-01 23:22:50 +08:00
parent 5e87a2d7e5
commit 56afd1adb9
3 changed files with 72 additions and 90 deletions

View File

@ -2,7 +2,7 @@
Tests for the provided defaults regarding HTTP method, and --json vs. --form.
"""
from utils import TestEnvironment, http, HTTP_OK, no_content_type
from utils import TestEnvironment, http, HTTP_OK
from fixtures import FILE_PATH
@ -46,7 +46,7 @@ class TestAutoContentTypeAndAcceptHeaders:
r = http('GET', httpbin.url + '/headers')
assert HTTP_OK in r
assert r.json['headers']['Accept'] == '*/*'
assert no_content_type(r.json['headers'])
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.

View File

@ -7,8 +7,7 @@ from tempfile import gettempdir
import pytest
from httpie.plugins.builtin import HTTPBasicAuth
from utils import TestEnvironment, mk_config_dir, http, HTTP_OK, \
no_content_type
from utils import TestEnvironment, mk_config_dir, http, HTTP_OK
from fixtures import UNICODE
@ -126,7 +125,7 @@ class TestSession(SessionTestBase):
r2 = http('--session=test', 'GET', httpbin.url + '/get',
env=self.env())
assert HTTP_OK in r2
assert no_content_type(r2.json['headers'])
assert 'Content-Type' not in r2.json['headers']
assert 'If-Unmodified-Since' not in r2.json['headers']
def test_session_by_path(self, httpbin):
@ -158,8 +157,8 @@ class TestSession(SessionTestBase):
assert HTTP_OK in r2
# FIXME: Authorization *sometimes* is not present on Python3
assert (r2.json['headers']['Authorization']
== HTTPBasicAuth.make_header(u'test', UNICODE))
assert (r2.json['headers']['Authorization'] ==
HTTPBasicAuth.make_header(u'test', UNICODE))
# httpbin doesn't interpret utf8 headers
assert UNICODE in r2

View File

@ -1,7 +1,5 @@
# coding=utf-8
"""Utilities used by HTTPie tests.
"""
"""Utilities for HTTPie test suite."""
import os
import sys
import time
@ -15,8 +13,6 @@ from httpie.compat import bytes, str
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
CRLF = '\r\n'
COLOR = '\x1b['
HTTP_OK = '200 OK'
@ -27,14 +23,9 @@ HTTP_OK_COLOR = (
)
def no_content_type(headers):
return (
'Content-Type' not in headers or
# We need to do also this because of this issue:
# <https://github.com/kevin1024/pytest-httpbin/issues/5>
# TODO: remove this function once the issue is if fixed
headers['Content-Type'] == 'text/plain'
)
def mk_config_dir():
dirname = tempfile.mkdtemp(prefix='httpie_config_')
return dirname
def add_auth(url, auth):
@ -43,10 +34,7 @@ def add_auth(url, auth):
class TestEnvironment(Environment):
"""
Environment subclass with reasonable defaults suitable for testing.
"""
"""Environment subclass with reasonable defaults for testing."""
colors = 0
stdin_isatty = True,
stdout_isatty = True
@ -86,6 +74,67 @@ class TestEnvironment(Environment):
pass
class BaseCLIResponse(object):
"""
Represents the result of simulated `$ http' invocation via `http()`.
Holds and provides access to:
- stdout output: print(self)
- stderr output: print(self.stderr)
- exit_status output: print(self.exit_status)
"""
stderr = None
json = None
exit_status = None
class BytesCLIResponse(bytes, BaseCLIResponse):
"""
Used as a fallback when a StrCLIResponse cannot be used.
E.g. when the output contains binary data or when it is colorized.
`.json` will always be None.
"""
class StrCLIResponse(str, BaseCLIResponse):
@property
def json(self):
"""
Return deserialized JSON body, if one included in the output
and is parseable.
"""
if not hasattr(self, '_json'):
self._json = None
# De-serialize JSON body if possible.
if COLOR in self:
# Colorized output cannot be parsed.
pass
elif self.strip().startswith('{'):
# Looks like JSON body.
self._json = json.loads(self)
elif (self.count('Content-Type:') == 1 and
'application/json' in self):
# Looks like a whole JSON HTTP message,
# try to extract its body.
try:
j = self.strip()[self.strip().rindex('\r\n\r\n'):]
except ValueError:
pass
else:
try:
self._json = json.loads(j)
except ValueError:
pass
return self._json
def http(*args, **kwargs):
# noinspection PyUnresolvedReferences
"""
@ -187,69 +236,3 @@ def http(*args, **kwargs):
stdout.close()
stderr.close()
env.cleanup()
class BaseCLIResponse(object):
"""
Represents the result of simulated `$ http' invocation via `http()`.
Holds and provides access to:
- stdout output: print(self)
- stderr output: print(self.stderr)
- exit_status output: print(self.exit_status)
"""
stderr = None
json = None
exit_status = None
class BytesCLIResponse(bytes, BaseCLIResponse):
"""
Used as a fallback when a StrCLIResponse cannot be used.
E.g. when the output contains binary data or when it is colorized.
`.json` will always be None.
"""
class StrCLIResponse(str, BaseCLIResponse):
@property
def json(self):
"""
Return deserialized JSON body, if one included in the output
and is parseable.
"""
if not hasattr(self, '_json'):
self._json = None
# De-serialize JSON body if possible.
if COLOR in self:
# Colorized output cannot be parsed.
pass
elif self.strip().startswith('{'):
# Looks like JSON body.
self._json = json.loads(self)
elif (self.count('Content-Type:') == 1 and
'application/json' in self):
# Looks like a whole JSON HTTP message,
# try to extract its body.
try:
j = self.strip()[self.strip().rindex('\r\n\r\n'):]
except ValueError:
pass
else:
try:
self._json = json.loads(j)
except ValueError:
pass
return self._json
def mk_config_dir():
dirname = tempfile.mkdtemp(prefix='httpie_config_')
return dirname