Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httpie/cli/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def _body_from_input(self, data):
"""
self._ensure_one_data_source(self.has_stdin_data, self.args.data,
self.args.files)
self.args.data = data.encode('utf-8')
self.args.data = data.encode()

def _ensure_one_data_source(self, *other_sources):
"""There can only be one source of input request data.
Expand Down
2 changes: 1 addition & 1 deletion httpie/cli/requestitems.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def load_text_file(item: KeyValueArg) -> str:
except UnicodeDecodeError:
raise ParseError(
f'{item.orig!r}: cannot embed the content of {item.value!r},'
' not a UTF8 or ASCII-encoded text file'
' not a UTF-8 or ASCII-encoded text file'
)


Expand Down
4 changes: 2 additions & 2 deletions httpie/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

urllib3.disable_warnings()

FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8'
FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=UTF-8'
JSON_CONTENT_TYPE = 'application/json'
JSON_ACCEPT = f'{JSON_CONTENT_TYPE}, */*;q=0.5'
DEFAULT_UA = f'HTTPie/{__version__}'
Expand Down Expand Up @@ -188,7 +188,7 @@ def finalize_headers(headers: RequestHeadersDict) -> RequestHeadersDict:
value = value.strip()
if isinstance(value, str):
# See <https://github.com/httpie/httpie/issues/212>
value = value.encode('utf8')
value = value.encode()
final_headers[name] = value
return final_headers

Expand Down
5 changes: 3 additions & 2 deletions httpie/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from . import __version__
from .compat import is_windows
from .constants import UTF8


ENV_XDG_CONFIG_HOME = 'XDG_CONFIG_HOME'
Expand Down Expand Up @@ -79,7 +80,7 @@ def is_new(self) -> bool:
def load(self):
config_type = type(self).__name__.lower()
try:
with self.path.open() as f:
with self.path.open(encoding=UTF8) as f:
try:
data = json.load(f)
except ValueError as e:
Expand Down Expand Up @@ -111,7 +112,7 @@ def save(self, fail_silently=False):
ensure_ascii=True,
)
try:
self.path.write_text(json_string + '\n')
self.path.write_text(json_string + '\n', encoding=UTF8)
except OSError:
if not fail_silently:
raise
Expand Down
2 changes: 2 additions & 0 deletions httpie/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# UTF-8 encoding name
UTF8 = 'utf-8'
7 changes: 4 additions & 3 deletions httpie/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from .compat import is_windows
from .config import DEFAULT_CONFIG_DIR, Config, ConfigFileError
from .constants import UTF8

from .utils import repr_dict

Expand Down Expand Up @@ -70,10 +71,10 @@ def __init__(self, devnull=None, **kwargs):
self._orig_stderr = self.stderr
self._devnull = devnull

# Keyword arguments > stream.encoding > default utf8
# Keyword arguments > stream.encoding > default UTF-8
if self.stdin and self.stdin_encoding is None:
self.stdin_encoding = getattr(
self.stdin, 'encoding', None) or 'utf8'
self.stdin, 'encoding', None) or UTF8
if self.stdout_encoding is None:
actual_stdout = self.stdout
if is_windows:
Expand All @@ -83,7 +84,7 @@ def __init__(self, devnull=None, **kwargs):
# noinspection PyUnresolvedReferences
actual_stdout = self.stdout.wrapped
self.stdout_encoding = getattr(
actual_stdout, 'encoding', None) or 'utf8'
actual_stdout, 'encoding', None) or UTF8

def __str__(self):
defaults = dict(type(self).__dict__)
Expand Down
11 changes: 6 additions & 5 deletions httpie/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Iterable, Optional
from urllib.parse import urlsplit

from .constants import UTF8
from .utils import split_cookies


Expand Down Expand Up @@ -38,7 +39,7 @@ def content_type(self) -> str:
"""Return the message content type."""
ct = self._orig.headers.get('Content-Type', '')
if not isinstance(ct, str):
ct = ct.decode('utf8')
ct = ct.decode()
return ct


Expand Down Expand Up @@ -82,7 +83,7 @@ def headers(self):

@property
def encoding(self):
return self._orig.encoding or 'utf8'
return self._orig.encoding or UTF8

@property
def body(self):
Expand Down Expand Up @@ -115,7 +116,7 @@ def headers(self):
headers['Host'] = url.netloc.split('@')[-1]

headers = [
f'{name}: {value if isinstance(value, str) else value.decode("utf-8")}'
f'{name}: {value if isinstance(value, str) else value.decode()}'
for name, value in headers.items()
]

Expand All @@ -125,12 +126,12 @@ def headers(self):

@property
def encoding(self):
return 'utf8'
return UTF8

@property
def body(self):
body = self._orig.body
if isinstance(body, str):
# Happens with JSON/form request data parsed from the command line.
body = body.encode('utf8')
body = body.encode()
return body or b''
7 changes: 4 additions & 3 deletions httpie/output/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Callable, Iterable, Union

from ..context import Environment
from ..constants import UTF8
from ..models import HTTPMessage
from .processing import Conversion, Formatting

Expand Down Expand Up @@ -48,7 +49,7 @@ def __init__(

def get_headers(self) -> bytes:
"""Return the headers' bytes."""
return self.msg.headers.encode('utf8')
return self.msg.headers.encode()

def iter_body(self) -> Iterable[bytes]:
"""Return an iterator over the message body."""
Expand Down Expand Up @@ -104,8 +105,8 @@ def __init__(self, env=Environment(), **kwargs):
else:
# Preserve the message encoding.
output_encoding = self.msg.encoding
# Default to utf8 when unsure.
self.output_encoding = output_encoding or 'utf8'
# Default to UTF-8 when unsure.
self.output_encoding = output_encoding or UTF8

def iter_body(self) -> Iterable[bytes]:
for line, lf in self.msg.iter_lines(self.CHUNK_SIZE):
Expand Down
2 changes: 1 addition & 1 deletion httpie/plugins/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __call__(
@staticmethod
def make_header(username: str, password: str) -> str:
credentials = f'{username}:{password}'
token = b64encode(credentials.encode('utf-8')).strip().decode('latin1')
token = b64encode(credentials.encode()).strip().decode('latin1')
return f'Basic {token}'


Expand Down
2 changes: 1 addition & 1 deletion httpie/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def update_headers(self, request_headers: RequestHeadersDict):
continue # Ignore explicitly unset headers

if type(value) is not str:
value = value.decode('utf8')
value = value.decode()

if name.lower() == 'user-agent' and value.startswith('HTTPie/'):
continue
Expand Down
6 changes: 4 additions & 2 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Test data"""
from pathlib import Path

from httpie.constants import UTF8


def patharg(path):
"""
Expand All @@ -23,9 +25,9 @@ def patharg(path):
# Strip because we don't want new lines in the data so that we can
# easily count occurrences also when embedded in JSON (where the new
# line would be escaped).
FILE_CONTENT = FILE_PATH.read_text('utf8').strip()
FILE_CONTENT = FILE_PATH.read_text(encoding=UTF8).strip()


JSON_FILE_CONTENT = JSON_FILE_PATH.read_text('utf8')
JSON_FILE_CONTENT = JSON_FILE_PATH.read_text(encoding=UTF8)
BIN_FILE_CONTENT = BIN_FILE_PATH.read_bytes()
UNICODE = FILE_CONTENT
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_valid_items(self):
# Parsed file fields
assert 'file' in items.files
assert (items.files['file'][1].read().strip().
decode('utf8') == FILE_CONTENT)
decode() == FILE_CONTENT)

def test_multiple_file_fields_with_same_field_name(self):
items = RequestItems.from_args([
Expand Down
3 changes: 2 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from _pytest.monkeypatch import MonkeyPatch

from httpie.compat import is_windows
from httpie.constants import UTF8
from httpie.config import (
Config, DEFAULT_CONFIG_DIRNAME, DEFAULT_RELATIVE_LEGACY_CONFIG_DIR,
DEFAULT_RELATIVE_XDG_CONFIG_HOME, DEFAULT_WINDOWS_CONFIG_DIR,
Expand All @@ -25,7 +26,7 @@ def test_default_options(httpbin):
def test_config_file_not_valid(httpbin):
env = MockEnvironment()
env.create_temp_config_dir()
(env.config_dir / Config.FILENAME).write_text('{invalid json}')
(env.config_dir / Config.FILENAME).write_text('{invalid json}', encoding=UTF8)
r = http(httpbin + '/get', env=env)
assert HTTP_OK in r
assert 'http: warning' in r.stderr
Expand Down
2 changes: 1 addition & 1 deletion tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ def test_rst_file_syntax(filename):
shell=True,
)
err = p.communicate()[1]
assert p.returncode == 0, err.decode('utf8')
assert p.returncode == 0, err.decode()
2 changes: 1 addition & 1 deletion tests/test_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_filename_from_url(self):
)
assert 'foo.html' == filename_from_url(
url='http://example.org/foo',
content_type='text/html; charset=utf8'
content_type='text/html; charset=UTF-8'
)
assert 'foo' == filename_from_url(
url='http://example.org/foo',
Expand Down
3 changes: 2 additions & 1 deletion tests/test_httpie.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .fixtures import FILE_CONTENT, FILE_PATH
from httpie.cli.exceptions import ParseError
from httpie.context import Environment
from httpie.constants import UTF8
from httpie.status import ExitStatus
from .utils import HTTP_OK, MockEnvironment, StdinBytesIO, http

Expand Down Expand Up @@ -130,7 +131,7 @@ def test_form_POST_file_redirected_stdin(httpbin):
<https://github.com/httpie/httpie/issues/840>

"""
with open(FILE_PATH):
with open(FILE_PATH, encoding=UTF8):
r = http(
'--form',
'POST',
Expand Down
5 changes: 3 additions & 2 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
parse_format_options,
)
from httpie.cli.definition import parser
from httpie.constants import UTF8
from httpie.output.formatters.colors import get_lexer
from httpie.status import ExitStatus
from .utils import COLOR, CRLF, HTTP_OK, MockEnvironment, http
Expand All @@ -30,7 +31,7 @@ def test_output_option(tmp_path, httpbin, stdout_isatty):
assert r == ''

expected_body = urlopen(url).read().decode()
actual_body = output_filename.read_text()
actual_body = output_filename.read_text(encoding=UTF8)

assert actual_body == expected_body

Expand Down Expand Up @@ -125,7 +126,7 @@ def test_quiet_with_output_redirection(self, tmp_path, httpbin, with_download):
assert env.stdout is env.devnull
else:
assert env.stdout is not env.devnull # --output swaps stdout.
assert output_path.read_text() == output
assert output_path.read_text(encoding=UTF8) == output
finally:
os.chdir(orig_cwd)

Expand Down
13 changes: 7 additions & 6 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from .fixtures import FILE_PATH_ARG, UNICODE
from httpie.constants import UTF8
from httpie.plugins import AuthPlugin
from httpie.plugins.builtin import HTTPBasicAuth
from httpie.plugins.registry import plugin_manager
Expand Down Expand Up @@ -52,7 +53,7 @@ def setup_method(self, method):
}
}
self.session_path = self.config_dir / 'test-session.json'
self.session_path.write_text(json.dumps(orig_session))
self.session_path.write_text(json.dumps(orig_session), encoding=UTF8)

def teardown_method(self, method):
shutil.rmtree(self.config_dir)
Expand Down Expand Up @@ -192,7 +193,7 @@ def test_session_unicode(self, httpbin):
# FIXME: Authorization *sometimes* is not present
assert (r2.json['headers']['Authorization']
== HTTPBasicAuth.make_header('test', UNICODE))
# httpbin doesn't interpret utf8 headers
# httpbin doesn't interpret UTF-8 headers
assert UNICODE in r2

def test_session_default_header_value_overwritten(self, httpbin):
Expand Down Expand Up @@ -309,7 +310,7 @@ def get_auth(self, username=None, password=None):
Plugin.auth_type,
'--auth', 'user:password',
)
updated_session = json.loads(self.session_path.read_text())
updated_session = json.loads(self.session_path.read_text(encoding=UTF8))
assert updated_session['auth']['type'] == 'test-saved'
assert updated_session['auth']['raw_auth'] == "user:password"
plugin_manager.unregister(Plugin)
Expand Down Expand Up @@ -338,7 +339,7 @@ def test_expired_cookies(self, httpbin):
)
assert 'Cookie: cookie1=foo; cookie2=foo' in r

updated_session = json.loads(self.session_path.read_text())
updated_session = json.loads(self.session_path.read_text(encoding=UTF8))
assert 'cookie1' in updated_session['cookies']
assert 'cookie2' not in updated_session['cookies']

Expand Down Expand Up @@ -432,7 +433,7 @@ def test_existing_and_new_cookies_sent_in_request(self, new_cookies, new_cookies
# Note: cookies in response are in alphabetical order
assert f'Cookie: {expected}' in r

updated_session = json.loads(self.session_path.read_text())
updated_session = json.loads(self.session_path.read_text(encoding=UTF8))
for name, value in new_cookies_dict.items():
assert name, value in updated_session['cookies']
assert 'Cookie' not in updated_session['headers']
Expand Down Expand Up @@ -473,6 +474,6 @@ def test_cookie_storage_priority(self, cli_cookie, set_cookie, expected, httpbin
httpbin.url + set_cookie,
'Cookie:' + cli_cookie,
)
updated_session = json.loads(self.session_path.read_text())
updated_session = json.loads(self.session_path.read_text(encoding=UTF8))

assert updated_session['cookies']['cookie1']['value'] == expected
8 changes: 4 additions & 4 deletions tests/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@


def test_unicode_headers(httpbin):
# httpbin doesn't interpret utf8 headers
# httpbin doesn't interpret UFT-8 headers
r = http(httpbin.url + '/headers', f'Test:{UNICODE}')
assert HTTP_OK in r


def test_unicode_headers_verbose(httpbin):
# httpbin doesn't interpret utf8 headers
# httpbin doesn't interpret UTF-8 headers
r = http('--verbose', httpbin.url + '/headers', f'Test:{UNICODE}')
assert HTTP_OK in r
assert UNICODE in r
Expand Down Expand Up @@ -96,14 +96,14 @@ def test_unicode_url(httpbin):

def test_unicode_basic_auth(httpbin):
# it doesn't really authenticate us because httpbin
# doesn't interpret the utf8-encoded auth
# doesn't interpret the UTF-8-encoded auth
http('--verbose', '--auth', f'test:{UNICODE}',
f'{httpbin.url}/basic-auth/test/{UNICODE}')


def test_unicode_digest_auth(httpbin):
# it doesn't really authenticate us because httpbin
# doesn't interpret the utf8-encoded auth
# doesn't interpret the UTF-8-encoded auth
http('--auth-type=digest',
'--auth', f'test:{UNICODE}',
f'{httpbin.url}/digest-auth/auth/test/{UNICODE}')
Loading