From 7dd11525c792c580156d917dc2566460950f9c13 Mon Sep 17 00:00:00 2001 From: Ahmed TAHRI Date: Tue, 3 Oct 2023 14:12:32 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=97=EF=B8=8F=20Try=20compatible=20fork=20?= =?UTF-8?q?Niquests=20to=20supercharge=20HTTPie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 7 +- docs/README.md | 2 +- docs/contributors/fetch.py | 8 +-- httpie/__init__.py | 4 +- httpie/adapters.py | 2 +- httpie/cli/argparser.py | 2 +- httpie/client.py | 62 +++++++---------- httpie/core.py | 105 ++++++++++++++++++++++++++--- httpie/downloads.py | 6 +- httpie/internal/update_warnings.py | 4 +- httpie/models.py | 24 ++++--- httpie/output/writer.py | 4 +- httpie/plugins/base.py | 6 +- httpie/plugins/builtin.py | 18 ++--- httpie/sessions.py | 4 +- httpie/ssl_.py | 60 +++++++++++++++-- httpie/uploads.py | 6 +- httpie/utils.py | 6 +- pytest.ini | 7 -- setup.cfg | 7 +- setup.py | 6 +- tests/conftest.py | 28 +++----- tests/test_auth.py | 6 +- tests/test_binary.py | 4 +- tests/test_cli.py | 2 +- tests/test_cookie.py | 2 +- tests/test_downloads.py | 6 +- tests/test_errors.py | 2 +- tests/test_exit_status.py | 8 +-- tests/test_json.py | 3 +- tests/test_output.py | 24 ++++--- tests/test_redirects.py | 2 +- tests/test_ssl.py | 37 ++-------- tests/test_transport_plugin.py | 6 +- tests/test_update_warnings.py | 2 +- tests/test_uploads.py | 11 ++- tests/utils/__init__.py | 7 +- tests/utils/matching/parsing.py | 2 +- 38 files changed, 299 insertions(+), 203 deletions(-) delete mode 100644 pytest.ini diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8e35f4eead..14bfa86a89 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -25,8 +25,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python-version: [3.7, 3.8, 3.9, "3.10"] - pyopenssl: [0, 1] + python-version: [3.7, 3.8, 3.9, "3.10", "3.11", "3.12"] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 @@ -39,12 +38,8 @@ jobs: python -m pip install --upgrade pip wheel python -m pip install --upgrade '.[dev]' python -m pytest --verbose ./httpie ./tests - env: - HTTPIE_TEST_WITH_PYOPENSSL: ${{ matrix.pyopenssl }} - name: Linux & Mac setup if: matrix.os != 'windows-latest' run: | make install make test - env: - HTTPIE_TEST_WITH_PYOPENSSL: ${{ matrix.pyopenssl }} diff --git a/docs/README.md b/docs/README.md index 3b12e98f4c..88afaa04d6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2556,7 +2556,7 @@ HTTPie has the following community channels: Under the hood, HTTPie uses these two amazing libraries: -- [Requests](https://requests.readthedocs.io/en/latest/) — Python HTTP library for humans +- [Niquests](https://niquests.readthedocs.io/en/latest/) — Python HTTP library for humans - [Pygments](https://pygments.org/) — Python syntax highlighter #### HTTPie friends diff --git a/docs/contributors/fetch.py b/docs/contributors/fetch.py index ba94c28183..1ea1e8d05a 100644 --- a/docs/contributors/fetch.py +++ b/docs/contributors/fetch.py @@ -1,7 +1,7 @@ """ Generate the contributors database. -FIXME: replace `requests` calls with the HTTPie API, when available. +FIXME: replace `niquests` calls with the HTTPie API, when available. """ import json import os @@ -14,7 +14,7 @@ from time import sleep from typing import Any, Dict, Optional, Set -import requests +import niquests FullNames = Set[str] GitHubLogins = Set[str] @@ -197,10 +197,10 @@ def fetch(url: str, params: Optional[Dict[str, str]] = None) -> UserInfo: } for retry in range(1, 6): debug(f'[{retry}/5]', f'{url = }', f'{params = }') - with requests.get(url, params=params, headers=headers) as req: + with niquests.get(url, params=params, headers=headers) as req: try: req.raise_for_status() - except requests.exceptions.HTTPError as exc: + except niquests.exceptions.HTTPError as exc: if exc.response.status_code == 403: # 403 Client Error: rate limit exceeded for url: ... now = int(datetime.utcnow().timestamp()) diff --git a/httpie/__init__.py b/httpie/__init__.py index ffe0d35419..447c01aa16 100644 --- a/httpie/__init__.py +++ b/httpie/__init__.py @@ -3,7 +3,7 @@ """ -__version__ = '3.2.2' -__date__ = '2022-05-06' +__version__ = '4.0.0' +__date__ = '2023-10-11' __author__ = 'Jakub Roztocil' __licence__ = 'BSD' diff --git a/httpie/adapters.py b/httpie/adapters.py index 8e2dd7397f..fa6cfcec89 100644 --- a/httpie/adapters.py +++ b/httpie/adapters.py @@ -1,5 +1,5 @@ from httpie.cli.dicts import HTTPHeadersDict -from requests.adapters import HTTPAdapter +from niquests.adapters import HTTPAdapter class HTTPieHTTPAdapter(HTTPAdapter): diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index 9bf09b3b73..34dc401081 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -7,7 +7,7 @@ from textwrap import dedent from urllib.parse import urlsplit -from requests.utils import get_netrc_auth +from niquests.utils import get_netrc_auth from .argtypes import ( AuthCredentials, SSLCredentials, KeyValueArgType, diff --git a/httpie/client.py b/httpie/client.py index a1da284a7c..93cdf7bd44 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -1,13 +1,11 @@ import argparse -import http.client import json import sys -from contextlib import contextmanager from time import monotonic from typing import Any, Dict, Callable, Iterable from urllib.parse import urlparse, urlunparse -import requests +import niquests # noinspection PyPackageRequirements import urllib3 from urllib3.util import SKIP_HEADER, SKIPPABLE_HEADERS @@ -44,6 +42,7 @@ def collect_messages( env: Environment, args: argparse.Namespace, request_body_read_callback: Callable[[bytes], None] = None, + prepared_request_readiness: Callable[[niquests.PreparedRequest], None] = None, ) -> Iterable[RequestsMessage]: httpie_session = None httpie_session_headers = None @@ -88,7 +87,12 @@ def collect_messages( # TODO: reflect the split between request and send kwargs. dump_request(request_kwargs) - request = requests.Request(**request_kwargs) + hooks = None + + if prepared_request_readiness: + hooks = {"pre_send": [prepared_request_readiness]} + + request = niquests.Request(**request_kwargs, hooks=hooks) prepared_request = requests_session.prepare_request(request) transform_headers(request, prepared_request) if args.path_as_is: @@ -110,12 +114,13 @@ def collect_messages( url=prepared_request.url, **send_kwargs_mergeable_from_env, ) - with max_headers(args.max_headers): - response = requests_session.send( - request=prepared_request, - **send_kwargs_merged, - **send_kwargs, - ) + response = requests_session.send( + request=prepared_request, + **send_kwargs_merged, + **send_kwargs, + ) + if args.max_headers and len(response.headers) > args.max_headers: + raise niquests.ConnectionError(f"got more than {args.max_headers} headers") response._httpie_headers_parsed_at = monotonic() expired_cookies += get_expired_cookies( response.headers.get('Set-Cookie', '') @@ -124,7 +129,7 @@ def collect_messages( response_count += 1 if response.next: if args.max_redirects and response_count == args.max_redirects: - raise requests.TooManyRedirects + raise niquests.TooManyRedirects if args.follow: prepared_request = response.next if args.all: @@ -140,25 +145,12 @@ def collect_messages( httpie_session.save() -# noinspection PyProtectedMember -@contextmanager -def max_headers(limit): - # - # noinspection PyUnresolvedReferences - orig = http.client._MAXHEADERS - http.client._MAXHEADERS = limit or float('Inf') - try: - yield - finally: - http.client._MAXHEADERS = orig - - def build_requests_session( verify: bool, ssl_version: str = None, ciphers: str = None, -) -> requests.Session: - requests_session = requests.Session() +) -> niquests.Session: + requests_session = niquests.Session() # Install our adapter. http_adapter = HTTPieHTTPAdapter() @@ -186,7 +178,7 @@ def build_requests_session( def dump_request(kwargs: dict): sys.stderr.write( - f'\n>>> requests.request(**{repr_dict(kwargs)})\n\n') + f'\n>>> niquests.request(**{repr_dict(kwargs)})\n\n') def finalize_headers(headers: HTTPHeadersDict) -> HTTPHeadersDict: @@ -210,13 +202,13 @@ def finalize_headers(headers: HTTPHeadersDict) -> HTTPHeadersDict: def transform_headers( - request: requests.Request, - prepared_request: requests.PreparedRequest + request: niquests.Request, + prepared_request: niquests.PreparedRequest ) -> None: """Apply various transformations on top of the `prepared_requests`'s headers to change the request prepreation behavior.""" - # Remove 'Content-Length' when it is misplaced by requests. + # Remove 'Content-Length' when it is misplaced by niquests. if ( prepared_request.method in IGNORE_CONTENT_LENGTH_METHODS and prepared_request.headers.get('Content-Length') == '0' @@ -232,7 +224,7 @@ def transform_headers( def apply_missing_repeated_headers( original_headers: HTTPHeadersDict, - prepared_request: requests.PreparedRequest + prepared_request: niquests.PreparedRequest ) -> None: """Update the given `prepared_request`'s headers with the original ones. This allows the requests to be prepared as usual, and then later @@ -290,12 +282,6 @@ def make_send_kwargs_mergeable_from_env(args: argparse.Namespace) -> dict: if args.cert: cert = args.cert if args.cert_key: - # Having a client certificate key passphrase is not supported - # by requests. So we are using our own transportation structure - # which is compatible with their format (a tuple of minimum two - # items). - # - # See: https://github.com/psf/requests/issues/2519 cert = HTTPieCertificate(cert, args.cert_key, args.cert_key_pass.value) return { @@ -329,7 +315,7 @@ def make_request_kwargs( request_body_read_callback=lambda chunk: chunk ) -> dict: """ - Translate our `args` into `requests.Request` keyword arguments. + Translate our `args` into `niquests.Request` keyword arguments. """ files = args.files diff --git a/httpie/core.py b/httpie/core.py index d0c26dcbcc..c0a9f96d19 100644 --- a/httpie/core.py +++ b/httpie/core.py @@ -5,9 +5,9 @@ import socket from typing import List, Optional, Union, Callable -import requests +import niquests from pygments import __version__ as pygments_version -from requests import __version__ as requests_version +from niquests import __version__ as requests_version from . import __version__ as httpie_version from .cli.constants import OUT_REQ_BODY @@ -112,16 +112,16 @@ def handle_generic_error(e, annotation=None): if include_traceback: raise exit_status = ExitStatus.ERROR - except requests.Timeout: + except niquests.Timeout: exit_status = ExitStatus.ERROR_TIMEOUT env.log_error(f'Request timed out ({parsed_args.timeout}s).') - except requests.TooManyRedirects: + except niquests.TooManyRedirects: exit_status = ExitStatus.ERROR_TOO_MANY_REDIRECTS env.log_error( f'Too many redirects' f' (--max-redirects={parsed_args.max_redirects}).' ) - except requests.exceptions.ConnectionError as exc: + except niquests.exceptions.ConnectionError as exc: annotation = None original_exc = unwrap_context(exc) if isinstance(original_exc, socket.gaierror): @@ -175,8 +175,8 @@ def program(args: argparse.Namespace, env: Environment) -> ExitStatus: # TODO: Refactor and drastically simplify, especially so that the separator logic is elsewhere. exit_status = ExitStatus.SUCCESS downloader = None - initial_request: Optional[requests.PreparedRequest] = None - final_response: Optional[requests.Response] = None + initial_request: Optional[niquests.PreparedRequest] = None + final_response: Optional[niquests.Response] = None processing_options = ProcessingOptions.from_raw_args(args) def separate(): @@ -204,8 +204,94 @@ def request_body_read_callback(chunk: bytes): args.follow = True # --download implies --follow. downloader = Downloader(env, output_file=args.output_file, resume=args.download_resume) downloader.pre_request(args.headers) + + def prepared_request_readiness(pr): + nonlocal output_options, do_write_body, processing_options + + if initial_request == pr: + if args.debug and pr.conn_info and pr.conn_info.destination_address: + sys.stderr.write( + f"""\n>>> Connected to {pr.conn_info.destination_address[0]} port {pr.conn_info.destination_address[1]}\n""" + ) + + if args.debug and pr.conn_info: + if pr.conn_info.cipher: + sys.stderr.write( + f"""\n>>> Connection secured using {pr.conn_info.tls_version.name.replace('_', '.')} / {pr.conn_info.cipher}\n\n""" + ) + + if pr.conn_info.certificate_dict: + sys.stderr.write(">>> Server certificate:\n") + + if "subject" in pr.conn_info.certificate_dict: + sys.stderr.write( + ">>> subject: " + ) + + for entry in pr.conn_info.certificate_dict['subject']: + if len(entry) == 2: + rdns, value = entry + elif len(entry) == 1: + rdns, value = entry[0] + else: + continue + + sys.stderr.write(f'{rdns}="{value}"; ') + + sys.stderr.write("\n") + + sys.stderr.write(f">>> start date: {pr.conn_info.certificate_dict['notBefore']}\n") + sys.stderr.write(f">>> expire date: {pr.conn_info.certificate_dict['notAfter']}\n") + + if "subjectAltName" in pr.conn_info.certificate_dict: + sys.stderr.write( + ">>> subjectAltName: " + ) + + for entry in pr.conn_info.certificate_dict['subjectAltName']: + if len(entry) == 2: + rdns, value = entry + sys.stderr.write(f'{rdns}="{value}"; ') + + sys.stderr.write("\n") + + if "issuer" in pr.conn_info.certificate_dict: + sys.stderr.write( + ">>> issuer: " + ) + + for entry in pr.conn_info.certificate_dict['issuer']: + if len(entry) == 2: + rdns, value = entry + elif len(entry) == 1: + rdns, value = entry[0] + else: + continue + + sys.stderr.write(f'{rdns}="{value}"; ') + + sys.stderr.write("\n\n") + + if pr.ocsp_verified is None: + sys.stderr.write(">>> Revocation status: Unverified\n\n") + elif pr.ocsp_verified: + sys.stderr.write(">>> Revocation status: Good\n\n") + else: + sys.stderr.write(">>> Revocation status: Error\n\n") + else: + sys.stderr.write("\n") + + write_message( + requests_message=pr, + env=env, + output_options=output_options._replace( + body=do_write_body + ), + processing_options=processing_options + ) + messages = collect_messages(env, args=args, - request_body_read_callback=request_body_read_callback) + request_body_read_callback=request_body_read_callback, prepared_request_readiness=prepared_request_readiness) force_separator = False prev_with_body = False @@ -225,6 +311,9 @@ def request_body_read_callback(chunk: bytes): is_streamed_upload = not isinstance(message.body, (str, bytes)) do_write_body = not is_streamed_upload force_separator = is_streamed_upload and env.stdout_isatty + if message.conn_info is None and not args.offline: + prev_with_body = output_options.body + continue else: final_response = message if args.check_status or downloader: diff --git a/httpie/downloads.py b/httpie/downloads.py index 9c4b895e6f..7a86486334 100644 --- a/httpie/downloads.py +++ b/httpie/downloads.py @@ -10,7 +10,7 @@ from typing import IO, Optional, Tuple from urllib.parse import urlsplit -import requests +import niquests from .models import HTTPResponse, OutputOptions from .output.streams import RawStream @@ -202,7 +202,7 @@ def pre_request(self, request_headers: dict): def start( self, initial_url: str, - final_response: requests.Response + final_response: niquests.Response ) -> Tuple[RawStream, IO]: """ Initiate and return a stream for `response` body with progress @@ -288,7 +288,7 @@ def chunk_downloaded(self, chunk: bytes): @staticmethod def _get_output_file_from_response( initial_url: str, - final_response: requests.Response, + final_response: niquests.Response, ) -> IO: # Output file not specified. Pick a name that doesn't exist yet. filename = None diff --git a/httpie/internal/update_warnings.py b/httpie/internal/update_warnings.py index a4b80d46b5..c684bb80ad 100644 --- a/httpie/internal/update_warnings.py +++ b/httpie/internal/update_warnings.py @@ -4,7 +4,7 @@ from pathlib import Path from typing import Any, Optional, Callable -import requests +import niquests import httpie from httpie.context import Environment, LogLevel @@ -41,7 +41,7 @@ def _fetch_updates(env: Environment) -> str: file = env.config.version_info_file data = _read_data_error_free(file) - response = requests.get(PACKAGE_INDEX_LINK, verify=False) + response = niquests.get(PACKAGE_INDEX_LINK, verify=False) response.raise_for_status() data.setdefault('last_warned_date', None) diff --git a/httpie/models.py b/httpie/models.py index a0a68c8ddc..0966eb1ed0 100644 --- a/httpie/models.py +++ b/httpie/models.py @@ -1,7 +1,8 @@ from time import monotonic -import requests +import niquests from urllib3.util import SKIP_HEADER, SKIPPABLE_HEADERS +from kiss_headers.utils import prettify_header_name from enum import Enum, auto from typing import Iterable, Union, NamedTuple @@ -59,7 +60,7 @@ def content_type(self) -> str: class HTTPResponse(HTTPMessage): - """A :class:`requests.models.Response` wrapper.""" + """A :class:`niquests.models.Response` wrapper.""" def iter_body(self, chunk_size=1): return self._orig.iter_content(chunk_size=chunk_size) @@ -73,7 +74,7 @@ def headers(self): status_line = f'HTTP/{self.version} {original.status_code} {original.reason}' headers = [status_line] headers.extend( - ': '.join(header) + ': '.join([prettify_header_name(header[0]), header[1]]) for header in original.headers.items() if header[0] != 'Set-Cookie' ) @@ -112,7 +113,8 @@ def version(self) -> str: 9: '0.9', 10: '1.0', 11: '1.1', - 20: '2.0', + 20: '2', + 30: '3', } fallback = 11 version = None @@ -128,7 +130,7 @@ def version(self) -> str: class HTTPRequest(HTTPMessage): - """A :class:`requests.models.Request` wrapper.""" + """A :class:`niquests.models.Request` wrapper.""" def iter_body(self, chunk_size): yield self.body @@ -140,10 +142,11 @@ def iter_lines(self, chunk_size): def headers(self): url = urlsplit(self._orig.url) - request_line = '{method} {path}{query} HTTP/1.1'.format( + request_line = '{method} {path}{query} {http_version}'.format( method=self._orig.method, path=url.path or '/', - query=f'?{url.query}' if url.query else '' + query=f'?{url.query}' if url.query else '', + http_version=self._orig.conn_info.http_version.value.replace(".0", "") if self._orig.conn_info and self._orig.conn_info.http_version else "HTTP/1.1" ) headers = self._orig.headers.copy() @@ -158,6 +161,7 @@ def headers(self): headers.insert(0, request_line) headers = '\r\n'.join(headers).strip() + return headers @property @@ -169,7 +173,7 @@ def body(self): return body or b'' -RequestsMessage = Union[requests.PreparedRequest, requests.Response] +RequestsMessage = Union[niquests.PreparedRequest, niquests.Response] class RequestsMessageKind(Enum): @@ -178,9 +182,9 @@ class RequestsMessageKind(Enum): def infer_requests_message_kind(message: RequestsMessage) -> RequestsMessageKind: - if isinstance(message, requests.PreparedRequest): + if isinstance(message, niquests.PreparedRequest): return RequestsMessageKind.REQUEST - elif isinstance(message, requests.Response): + elif isinstance(message, niquests.Response): return RequestsMessageKind.RESPONSE else: raise TypeError(f"Unexpected message type: {type(message).__name__}") diff --git a/httpie/output/writer.py b/httpie/output/writer.py index 4a2949bce2..4e4071cd83 100644 --- a/httpie/output/writer.py +++ b/httpie/output/writer.py @@ -1,5 +1,5 @@ import errno -import requests +import niquests from typing import Any, Dict, IO, Optional, TextIO, Tuple, Type, Union from ..cli.dicts import HTTPHeadersDict @@ -105,7 +105,7 @@ def write_raw_data( headers: Optional[HTTPHeadersDict] = None, stream_kwargs: Optional[Dict[str, Any]] = None ): - msg = requests.PreparedRequest() + msg = niquests.PreparedRequest() msg.is_body_upload_chunk = True msg.body = data msg.headers = headers or HTTPHeadersDict() diff --git a/httpie/plugins/base.py b/httpie/plugins/base.py index 1b44e5aec5..4e26242bc7 100644 --- a/httpie/plugins/base.py +++ b/httpie/plugins/base.py @@ -63,7 +63,7 @@ def get_auth(self, username: str = None, password: str = None): Use `self.raw_auth` to access the raw value passed through `--auth, -a`. - Return a ``requests.auth.AuthBase`` subclass instance. + Return a ``niquests.auth.AuthBase`` subclass instance. """ raise NotImplementedError() @@ -73,7 +73,7 @@ class TransportPlugin(BasePlugin): """ Requests transport adapter docs: - + See httpie-unixsocket for an example transport plugin: @@ -86,7 +86,7 @@ class TransportPlugin(BasePlugin): def get_adapter(self): """ - Return a ``requests.adapters.BaseAdapter`` subclass instance to be + Return a ``niquests.adapters.BaseAdapter`` subclass instance to be mounted to ``self.prefix``. """ diff --git a/httpie/plugins/builtin.py b/httpie/plugins/builtin.py index 860aebf7f9..ad79d0a53f 100644 --- a/httpie/plugins/builtin.py +++ b/httpie/plugins/builtin.py @@ -1,6 +1,6 @@ from base64 import b64encode -import requests.auth +import niquests.auth from .base import AuthPlugin @@ -10,12 +10,12 @@ class BuiltinAuthPlugin(AuthPlugin): package_name = '(builtin)' -class HTTPBasicAuth(requests.auth.HTTPBasicAuth): +class HTTPBasicAuth(niquests.auth.HTTPBasicAuth): def __call__( self, - request: requests.PreparedRequest - ) -> requests.PreparedRequest: + request: niquests.PreparedRequest + ) -> niquests.PreparedRequest: """ Override username/password serialization to allow unicode. @@ -34,12 +34,12 @@ def make_header(username: str, password: str) -> str: return f'Basic {token}' -class HTTPBearerAuth(requests.auth.AuthBase): +class HTTPBearerAuth(niquests.auth.AuthBase): def __init__(self, token: str) -> None: self.token = token - def __call__(self, request: requests.PreparedRequest) -> requests.PreparedRequest: + def __call__(self, request: niquests.PreparedRequest) -> niquests.PreparedRequest: request.headers['Authorization'] = f'Bearer {self.token}' return request @@ -64,8 +64,8 @@ def get_auth( self, username: str, password: str - ) -> requests.auth.HTTPDigestAuth: - return requests.auth.HTTPDigestAuth(username, password) + ) -> niquests.auth.HTTPDigestAuth: + return niquests.auth.HTTPDigestAuth(username, password) class BearerAuthPlugin(BuiltinAuthPlugin): @@ -75,5 +75,5 @@ class BearerAuthPlugin(BuiltinAuthPlugin): auth_parse = False # noinspection PyMethodOverriding - def get_auth(self, **kwargs) -> requests.auth.HTTPDigestAuth: + def get_auth(self, **kwargs) -> niquests.auth.HTTPDigestAuth: return HTTPBearerAuth(self.raw_auth) diff --git a/httpie/sessions.py b/httpie/sessions.py index 99dcdba92e..5351959a9b 100644 --- a/httpie/sessions.py +++ b/httpie/sessions.py @@ -10,8 +10,8 @@ from pathlib import Path from typing import Any, Dict, List, Optional, Union -from requests.auth import AuthBase -from requests.cookies import RequestsCookieJar, remove_cookie_by_name +from niquests.auth import AuthBase +from niquests.cookies import RequestsCookieJar, remove_cookie_by_name from .context import Environment, LogLevel from .cookies import HTTPieCookiePolicy diff --git a/httpie/ssl_.py b/httpie/ssl_.py index af5ca548db..fa6dbfa4e4 100644 --- a/httpie/ssl_.py +++ b/httpie/ssl_.py @@ -1,6 +1,10 @@ import ssl -from typing import NamedTuple, Optional +from typing import NamedTuple, Optional, Tuple, MutableMapping +import json +import os.path +from os import makedirs +from httpie.config import DEFAULT_CONFIG_DIR from httpie.adapters import HTTPAdapter # noinspection PyPackageRequirements from urllib3.util.ssl_ import ( @@ -10,10 +14,6 @@ SSL_VERSION_ARG_MAPPING = { - 'ssl2.3': 'PROTOCOL_SSLv23', - 'ssl3': 'PROTOCOL_SSLv3', - 'tls1': 'PROTOCOL_TLSv1', - 'tls1.1': 'PROTOCOL_TLSv1_1', 'tls1.2': 'PROTOCOL_TLSv1_2', 'tls1.3': 'PROTOCOL_TLSv1_3', } @@ -24,6 +24,50 @@ } +class QuicCapabilityCache( + MutableMapping[Tuple[str, int], Optional[Tuple[str, int]]] +): + + def __init__(self): + self._cache = {} + if not os.path.exists(DEFAULT_CONFIG_DIR): + makedirs(DEFAULT_CONFIG_DIR, exist_ok=True) + if os.path.exists(os.path.join(DEFAULT_CONFIG_DIR, "quic.json")): + with open(os.path.join(DEFAULT_CONFIG_DIR, "quic.json"), "r") as fp: + self._cache = json.load(fp) + + def save(self): + with open(os.path.join(DEFAULT_CONFIG_DIR, "quic.json"), "w+") as fp: + json.dump(self._cache, fp) + + def __contains__(self, item: Tuple[str, int]): + return f"QUIC_{item[0]}_{item[1]}" in self._cache + + def __setitem__(self, key: Tuple[str, int], value: Optional[Tuple[str, int]]): + self._cache[f"QUIC_{key[0]}_{key[1]}"] = f"{value[0]}:{value[1]}" + self.save() + + def __getitem__(self, item: Tuple[str, int]): + key: str = f"QUIC_{item[0]}_{item[1]}" + if key in self._cache: + host, port = self._cache[key].split(":") + return host, int(port) + + return None + + def __delitem__(self, key: Tuple[str, int]): + key: str = f"QUIC_{key[0]}_{key[1]}" + if key in self._cache: + del self._cache[key] + self.save() + + def __len__(self): + return len(self._cache) + + def __iter__(self): + yield from self._cache.items() + + class HTTPieCertificate(NamedTuple): cert_file: Optional[str] = None key_file: Optional[str] = None @@ -32,7 +76,9 @@ class HTTPieCertificate(NamedTuple): def to_raw_cert(self): """Synthesize a requests-compatible (2-item tuple of cert and key file) object from HTTPie's internal representation of a certificate.""" - return (self.cert_file, self.key_file) + if self.key_password: + return self.cert_file, self.key_file, self.key_password + return self.cert_file, self.key_file class HTTPieHTTPSAdapter(HTTPAdapter): @@ -48,6 +94,7 @@ def __init__( ssl_version=ssl_version, ciphers=ciphers, ) + kwargs.setdefault("quic_cache_layer", QuicCapabilityCache()) super().__init__(**kwargs) def init_poolmanager(self, *args, **kwargs): @@ -60,7 +107,6 @@ def proxy_manager_for(self, *args, **kwargs): def cert_verify(self, conn, url, verify, cert): if isinstance(cert, HTTPieCertificate): - conn.key_password = cert.key_password cert = cert.to_raw_cert() return super().cert_verify(conn, url, verify, cert) diff --git a/httpie/uploads.py b/httpie/uploads.py index 4a993b3a25..9025f52f3f 100644 --- a/httpie/uploads.py +++ b/httpie/uploads.py @@ -6,8 +6,8 @@ from typing import Any, Callable, IO, Iterable, Optional, Tuple, Union, TYPE_CHECKING from urllib.parse import urlencode -import requests -from requests.utils import super_len +import niquests +from niquests.utils import super_len if TYPE_CHECKING: from requests_toolbelt import MultipartEncoder @@ -250,7 +250,7 @@ def get_multipart_data_and_content_type( def compress_request( - request: requests.PreparedRequest, + request: niquests.PreparedRequest, always: bool, ): deflater = zlib.compressobj() diff --git a/httpie/utils.py b/httpie/utils.py index 4735b2be5d..33d8158568 100644 --- a/httpie/utils.py +++ b/httpie/utils.py @@ -16,7 +16,7 @@ from urllib.parse import urlsplit from typing import Any, List, Optional, Tuple, Generator, Callable, Iterable, IO, TypeVar -import requests.auth +import niquests.auth RE_COOKIE_SPLIT = re.compile(r', (?=[^ ;]+=)') Item = Tuple[str, Any] @@ -121,7 +121,7 @@ def humanize_bytes(n, precision=2): return f'{n / factor:.{precision}f} {suffix}' -class ExplicitNullAuth(requests.auth.AuthBase): +class ExplicitNullAuth(niquests.auth.AuthBase): """Forces requests to ignore the ``.netrc``. """ @@ -201,7 +201,7 @@ def _max_age_to_expires(cookies, now): def parse_content_type_header(header): - """Borrowed from requests.""" + """Borrowed from niquests.""" tokens = header.split(';') content_type, params = tokens[0].strip(), tokens[1:] params_dict = {} diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index ced65979b1..0000000000 --- a/pytest.ini +++ /dev/null @@ -1,7 +0,0 @@ -[pytest] -markers = - # If you want to run tests without a full HTTPie installation - # we advise you to disable the markers below, e.g: - # pytest -m 'not requires_installation and not requires_external_processes' - requires_installation - requires_external_processes diff --git a/setup.cfg b/setup.cfg index 86c41ff308..5e8922caff 100644 --- a/setup.cfg +++ b/setup.cfg @@ -11,7 +11,12 @@ testpaths = httpie tests norecursedirs = tests/fixtures addopts = --tb=native --doctest-modules --verbose xfail_strict = True - +markers = + # If you want to run tests without a full HTTPie installation + # we advise you to disable the markers below, e.g: + # pytest -m 'not requires_installation and not requires_external_processes' + requires_installation + requires_external_processes [flake8] # diff --git a/setup.py b/setup.py index 93bdb8f957..33c93510b9 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,8 @@ 'pytest-lazy-fixture>=0.0.6', 'responses', 'pytest-mock', - 'werkzeug<2.1.0' + 'werkzeug<2.1.0', + 'flaky', ] dev_require = [ *tests_require, @@ -23,7 +24,6 @@ 'flake8-deprecated', 'flake8-mutable', 'flake8-tuple', - 'pyopenssl', 'pytest-cov', 'pyyaml', 'twine', @@ -34,7 +34,7 @@ 'pip', 'charset_normalizer>=2.0.0', 'defusedxml>=0.6.0', - 'requests[socks]>=2.22.0', + 'niquests[socks]>=3.0.0', 'Pygments>=2.5.2', 'requests-toolbelt>=0.9.1', 'multidict>=4.7.0', diff --git a/tests/conftest.py b/tests/conftest.py index 7ca172a867..fa8642edd9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,7 +7,6 @@ HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN, HTTPBIN_WITH_CHUNKED_SUPPORT, REMOTE_HTTPBIN_DOMAIN, - IS_PYOPENSSL, mock_env ) from .utils.plugins_cli import ( # noqa @@ -20,6 +19,17 @@ ) from .utils.http_server import http_server, localhost_http_server # noqa +from sys import modules + +import niquests +import urllib3 + +# the mock utility 'response' only works with 'requests' +modules["requests"] = niquests +modules["requests.adapters"] = niquests.adapters +modules["requests.exceptions"] = niquests.exceptions +modules["requests.packages.urllib3"] = urllib3 + @pytest.fixture(scope='function', autouse=True) def httpbin_add_ca_bundle(monkeypatch): @@ -73,19 +83,3 @@ def remote_httpbin(_remote_httpbin_available): if _remote_httpbin_available: return 'http://' + REMOTE_HTTPBIN_DOMAIN pytest.skip(f'{REMOTE_HTTPBIN_DOMAIN} not resolvable') - - -@pytest.fixture(autouse=True, scope='session') -def pyopenssl_inject(): - """ - Injects `pyOpenSSL` module to make sure `requests` will use it. - - """ - if IS_PYOPENSSL: - try: - import urllib3.contrib.pyopenssl - urllib3.contrib.pyopenssl.inject_into_urllib3() - except ModuleNotFoundError: - pytest.fail('Missing "pyopenssl" module.') - - yield diff --git a/tests/test_auth.py b/tests/test_auth.py index 696fb22826..3f9b742cd7 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -93,8 +93,8 @@ def test_missing_auth(httpbin): def test_netrc(httpbin_both): # This one gets handled by requests (no --auth, --auth-type present), - # that’s why we patch inside `requests.sessions`. - with mock.patch('requests.sessions.get_netrc_auth') as get_netrc_auth: + # that’s why we patch inside `niquests.sessions`. + with mock.patch('niquests.sessions.get_netrc_auth') as get_netrc_auth: get_netrc_auth.return_value = ('httpie', 'password') r = http(httpbin_both + '/basic-auth/httpie/password') assert get_netrc_auth.call_count == 1 @@ -106,7 +106,7 @@ def test_ignore_netrc(httpbin_both): get_netrc_auth.return_value = ('httpie', 'password') r = http('--ignore-netrc', httpbin_both + '/basic-auth/httpie/password') assert get_netrc_auth.call_count == 0 - assert 'HTTP/1.1 401 UNAUTHORIZED' in r + assert 'HTTP/1.1 401 Unauthorized' in r def test_ignore_netrc_together_with_auth(): diff --git a/tests/test_binary.py b/tests/test_binary.py index ca51aa1686..9e5747ad22 100644 --- a/tests/test_binary.py +++ b/tests/test_binary.py @@ -1,5 +1,5 @@ """Tests for dealing with binary request and response data.""" -import requests +import niquests from .fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG from httpie.output.streams import BINARY_SUPPRESSED_NOTICE @@ -46,5 +46,5 @@ def test_binary_included_and_correct_when_suitable(self, httpbin): env = MockEnvironment(stdin_isatty=True, stdout_isatty=False) url = httpbin + '/bytes/1024?seed=1' r = http('GET', url, env=env) - expected = requests.get(url).content + expected = niquests.get(url).content assert r == expected diff --git a/tests/test_cli.py b/tests/test_cli.py index 6504c8a980..865eeb41c0 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,7 +2,7 @@ import argparse import pytest -from requests.exceptions import InvalidSchema +from niquests.exceptions import InvalidSchema import httpie.cli.argparser from httpie.cli import constants diff --git a/tests/test_cookie.py b/tests/test_cookie.py index c2a9746509..c8107ea1ce 100644 --- a/tests/test_cookie.py +++ b/tests/test_cookie.py @@ -28,7 +28,7 @@ class MockServerRequestHandler(BaseHTTPRequestHandler): """"HTTP request handler.""" def do_GET(self): - """Handle GET requests.""" + """Handle GET niquests.""" # Craft multiple cookies cookie = SimpleCookie() cookie['hello'] = 'world' diff --git a/tests/test_downloads.py b/tests/test_downloads.py index d6e98867bc..180e702d43 100644 --- a/tests/test_downloads.py +++ b/tests/test_downloads.py @@ -1,12 +1,12 @@ import os import tempfile import time -import requests +import niquests from unittest import mock from urllib.request import urlopen import pytest -from requests.structures import CaseInsensitiveDict +from niquests.structures import CaseInsensitiveDict from httpie.downloads import ( parse_content_range, filename_from_content_disposition, filename_from_url, @@ -15,7 +15,7 @@ from .utils import http, MockEnvironment -class Response(requests.Response): +class Response(niquests.Response): # noinspection PyDefaultArgument def __init__(self, url, headers={}, status_code=200): self.url = url diff --git a/tests/test_errors.py b/tests/test_errors.py index fca48fff15..fb9f030dcf 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -3,7 +3,7 @@ from unittest import mock from pytest import raises from requests import Request -from requests.exceptions import ConnectionError +from niquests.exceptions import ConnectionError from httpie.status import ExitStatus from .utils import HTTP_OK, http diff --git a/tests/test_exit_status.py b/tests/test_exit_status.py index 4438d3485c..97f071da54 100644 --- a/tests/test_exit_status.py +++ b/tests/test_exit_status.py @@ -26,7 +26,7 @@ def test_ok_response_exits_0(httpbin): def test_error_response_exits_0_without_check_status(httpbin): r = http('GET', httpbin.url + '/status/500') - assert '500 INTERNAL SERVER ERROR' in r + assert '500 Internal Server Error' in r assert r.exit_status == ExitStatus.SUCCESS assert not r.stderr @@ -44,7 +44,7 @@ def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected( r = http('--check-status', '--headers', 'GET', httpbin.url + '/status/301', env=env, tolerate_error_exit_status=True) - assert '301 MOVED PERMANENTLY' in r + assert '301 Moved Permanently' in r assert r.exit_status == ExitStatus.ERROR_HTTP_3XX assert '301 moved permanently' in r.stderr.lower() @@ -61,7 +61,7 @@ def test_3xx_check_status_redirects_allowed_exits_0(httpbin): def test_4xx_check_status_exits_4(httpbin): r = http('--check-status', 'GET', httpbin.url + '/status/401', tolerate_error_exit_status=True) - assert '401 UNAUTHORIZED' in r + assert '401 Unauthorized' in r assert r.exit_status == ExitStatus.ERROR_HTTP_4XX # Also stderr should be empty since stdout isn't redirected. assert not r.stderr @@ -70,5 +70,5 @@ def test_4xx_check_status_exits_4(httpbin): def test_5xx_check_status_exits_5(httpbin): r = http('--check-status', 'GET', httpbin.url + '/status/500', tolerate_error_exit_status=True) - assert '500 INTERNAL SERVER ERROR' in r + assert '500 Internal Server Error' in r assert r.exit_status == ExitStatus.ERROR_HTTP_5XX diff --git a/tests/test_json.py b/tests/test_json.py index e758ebe7f4..bf1b3857e9 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -338,13 +338,14 @@ def test_complex_json_arguments_with_non_json(httpbin, request_type, value): [ r'foo\[key\]:=1', r'bar\[1\]:=2', - r'baz\[\]:3', + r'baz\[\]:=3', r'quux[key\[escape\]]:=4', r'quux[key 2][\\][\\\\][\\\[\]\\\]\\\[\n\\]:=5', ], { 'foo[key]': 1, 'bar[1]': 2, + 'baz[]': 3, 'quux': { 'key[escape]': 4, 'key 2': {'\\': {'\\\\': {'\\[]\\]\\[\\n\\': 5}}}, diff --git a/tests/test_output.py b/tests/test_output.py index f85f38fa72..8847322055 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -9,7 +9,7 @@ from urllib.request import urlopen import pytest -import requests +import niquests import responses from httpie.cli.argtypes import ( @@ -97,18 +97,22 @@ def test_quiet_quiet_with_check_status_non_zero_pipe(self, httpbin): (['-q'], 1), (['-qq'], 0), ]) - # Might fail on Windows due to interference from other warnings. - @pytest.mark.xfail def test_quiet_on_python_warnings(self, test_patch, httpbin, flags, expected_warnings): def warn_and_run(*args, **kwargs): warnings.warn('warning!!') return ExitStatus.SUCCESS test_patch.side_effect = warn_and_run - with pytest.warns(None) as record: - http(*flags, httpbin + '/get') - assert len(record) == expected_warnings + if expected_warnings == 0: + with warnings.catch_warnings(): + warnings.simplefilter("error") + http(*flags, httpbin + '/get') + else: + with pytest.warns(Warning) as record: + http(*flags, httpbin + '/get') + + assert len(record) >= expected_warnings def test_double_quiet_on_error(self, httpbin): r = http( @@ -160,7 +164,7 @@ def test_quiet_with_output_redirection(self, tmp_path, httpbin, quiet_flags, wit output_path = Path('output.txt') env = MockEnvironment() orig_cwd = os.getcwd() - output = requests.get(url).text + output = niquests.get(url).text extra_args = ['--download'] if with_download else [] os.chdir(tmp_path) try: @@ -214,7 +218,7 @@ def test_verbose_json(self, httpbin): def test_verbose_implies_all(self, httpbin): r = http('--verbose', '--follow', httpbin + '/redirect/1') assert 'GET /redirect/1 HTTP/1.1' in r - assert 'HTTP/1.1 302 FOUND' in r + assert 'HTTP/1.1 302 Found' in r assert 'GET /get HTTP/1.1' in r assert HTTP_OK in r @@ -273,7 +277,7 @@ def test_ensure_meta_is_colored(httpbin, style): ' ', ' OK', ' OK ', - ' CUSTOM ', + # ' CUSTOM ', Unsupported. ]) def test_ensure_status_code_is_shown_on_all_themes(http_server, style, msg): env = MockEnvironment(colors=256) @@ -282,7 +286,7 @@ def test_ensure_status_code_is_shown_on_all_themes(http_server, style, msg): '--raw', msg, env=env) # Trailing space is stripped away. - assert 'HTTP/1.0 200' + msg.rstrip() in strip_colors(r) + assert 'HTTP/1.1 200' + msg.rstrip() in strip_colors(r) class TestPrettyOptions: diff --git a/tests/test_redirects.py b/tests/test_redirects.py index a761fa2571..692bb8ef68 100644 --- a/tests/test_redirects.py +++ b/tests/test_redirects.py @@ -15,7 +15,7 @@ def test_follow_all_redirects_shown(httpbin): r = http('--follow', '--all', httpbin.url + '/redirect/2') assert r.count('HTTP/1.1') == 3 - assert r.count('HTTP/1.1 302 FOUND', 2) + assert r.count('HTTP/1.1 302 Found', 2) assert HTTP_OK in r diff --git a/tests/test_ssl.py b/tests/test_ssl.py index 6fb983785a..6a6ba5c86f 100644 --- a/tests/test_ssl.py +++ b/tests/test_ssl.py @@ -2,7 +2,7 @@ import pytest import pytest_httpbin.certs -import requests.exceptions +import niquests.exceptions import urllib3 from unittest import mock @@ -10,23 +10,11 @@ from httpie.ssl_ import AVAILABLE_SSL_VERSION_ARG_MAPPING, DEFAULT_SSL_CIPHERS_STRING from httpie.status import ExitStatus -from .utils import HTTP_OK, TESTS_ROOT, IS_PYOPENSSL, http +from .utils import HTTP_OK, TESTS_ROOT, http - -try: - # Handle OpenSSL errors, if installed. - # See - # noinspection PyUnresolvedReferences - import OpenSSL.SSL - ssl_errors = ( - requests.exceptions.SSLError, - OpenSSL.SSL.Error, - ValueError, # TODO: Remove with OSS-65 - ) -except ImportError: - ssl_errors = ( - requests.exceptions.SSLError, - ) +ssl_errors = ( + niquests.exceptions.SSLError, +) CERTS_ROOT = TESTS_ROOT / 'client_certs' CLIENT_CERT = str(CERTS_ROOT / 'client.crt') @@ -59,10 +47,7 @@ def test_ssl_version(httpbin_secure, ssl_version): ) assert HTTP_OK in r except ssl_errors as e: - if ssl_version == 'ssl3': - # pytest-httpbin doesn't support ssl3 - pass - elif e.__context__ is not None: # Check if root cause was an unsupported TLS version + if e.__context__ is not None: # Check if root cause was an unsupported TLS version root = e.__context__ while root.__context__ is not None: root = root.__context__ @@ -151,7 +136,6 @@ def test_ciphers(httpbin_secure): assert HTTP_OK in r -@pytest.mark.skipif(IS_PYOPENSSL, reason='pyOpenSSL uses a different message format.') def test_ciphers_none_can_be_selected(httpbin_secure): r = http( httpbin_secure.url + '/get', @@ -168,15 +152,6 @@ def test_ciphers_none_can_be_selected(httpbin_secure): assert 'cipher' in r.stderr -def test_pyopenssl_presence(): - if not IS_PYOPENSSL: - assert not urllib3.util.ssl_.IS_PYOPENSSL - assert not urllib3.util.IS_PYOPENSSL - else: - assert urllib3.util.ssl_.IS_PYOPENSSL - assert urllib3.util.IS_PYOPENSSL - - @mock.patch('httpie.cli.argtypes.SSLCredentials._prompt_password', new=lambda self, prompt: PWD_CLIENT_PASS) def test_password_protected_cert_prompt(httpbin_secure): diff --git a/tests/test_transport_plugin.py b/tests/test_transport_plugin.py index b71592df8d..5f04ec6203 100644 --- a/tests/test_transport_plugin.py +++ b/tests/test_transport_plugin.py @@ -1,8 +1,8 @@ from io import BytesIO -from requests.adapters import BaseAdapter -from requests.models import Response -from requests.utils import get_encoding_from_headers +from niquests.adapters import BaseAdapter +from niquests.models import Response +from niquests.utils import get_encoding_from_headers from httpie.plugins import TransportPlugin from httpie.plugins.registry import plugin_manager diff --git a/tests/test_update_warnings.py b/tests/test_update_warnings.py index b2c24c36de..36e2596355 100644 --- a/tests/test_update_warnings.py +++ b/tests/test_update_warnings.py @@ -213,7 +213,7 @@ def fetch_update_mock(mocker): @pytest.fixture def static_fetch_data(mocker): - mock_get = mocker.patch('requests.get') + mock_get = mocker.patch('niquests.get') mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = { BUILD_CHANNEL: HIGHEST_VERSION, diff --git a/tests/test_uploads.py b/tests/test_uploads.py index d0156063d4..1f6c79465e 100644 --- a/tests/test_uploads.py +++ b/tests/test_uploads.py @@ -4,6 +4,9 @@ import subprocess import time import contextlib + +from flaky import flaky + import httpie.__main__ as main import pytest @@ -125,6 +128,7 @@ def stdin_processes(httpbin, *args, warn_threshold=0.1): @pytest.mark.parametrize("wait", (True, False)) @pytest.mark.requires_external_processes @pytest.mark.skipif(is_windows, reason="Windows doesn't support select() calls into files") +@flaky(max_runs=6) def test_reading_from_stdin(httpbin, wait): with stdin_processes(httpbin) as (process_1, process_2): process_1.communicate(timeout=0.1, input=b"bleh") @@ -143,6 +147,7 @@ def test_reading_from_stdin(httpbin, wait): @pytest.mark.requires_external_processes @pytest.mark.skipif(is_windows, reason="Windows doesn't support select() calls into files") +@flaky(max_runs=6) def test_stdin_read_warning(httpbin): with stdin_processes(httpbin) as (process_1, process_2): # Wait before sending any data @@ -154,11 +159,12 @@ def test_stdin_read_warning(httpbin): except subprocess.TimeoutExpired: errs = b'' - assert b'> warning: no stdin data read in 0.1s' in errs + assert b'> warning: no stdin data read in' in errs @pytest.mark.requires_external_processes @pytest.mark.skipif(is_windows, reason="Windows doesn't support select() calls into files") +@flaky(max_runs=6) def test_stdin_read_warning_with_quiet(httpbin): with stdin_processes(httpbin, "-qq") as (process_1, process_2): # Wait before sending any data @@ -175,6 +181,7 @@ def test_stdin_read_warning_with_quiet(httpbin): @pytest.mark.requires_external_processes @pytest.mark.skipif(is_windows, reason="Windows doesn't support select() calls into files") +@flaky(max_runs=6) def test_stdin_read_warning_blocking_exit(httpbin): # Use a very large number. with stdin_processes(httpbin, warn_threshold=999) as (process_1, process_2): @@ -284,7 +291,7 @@ def test_multipart_custom_content_type_boundary_added(self, httpbin): assert r.count(boundary) == 4 def test_multipart_custom_content_type_boundary_preserved(self, httpbin): - # Allow explicit nonsense requests. + # Allow explicit nonsense niquests. boundary_in_header = 'HEADER_BOUNDARY' boundary_in_body = 'BODY_BOUNDARY' r = http( diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index ada0905ff2..d033143fd6 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -1,7 +1,6 @@ """Utilities for HTTPie test suite.""" import re import shlex -import os import sys import time import json @@ -31,8 +30,6 @@ HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN = 'pie.dev' HTTPBIN_WITH_CHUNKED_SUPPORT = 'http://' + HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN -IS_PYOPENSSL = os.getenv('HTTPIE_TEST_WITH_PYOPENSSL', '0') == '1' - TESTS_ROOT = Path(__file__).parent.parent CRLF = '\r\n' COLOR = '\x1b[' @@ -139,7 +136,7 @@ def __init__(self, create_temp_config_dir=True, **kwargs): if 'stdout' not in kwargs: kwargs['stdout'] = tempfile.NamedTemporaryFile( mode='w+t', - prefix='httpie_stderr', + prefix='httpie_stdout', newline='', encoding=UTF8, ) @@ -210,7 +207,7 @@ class BaseCLIResponse: complete_args: List[str] = [] @property - def command(self): + def command(self): # noqa: F811 cmd = ' '.join(shlex.quote(arg) for arg in ['http', *self.args]) # pytest-httpbin to real httpbin. return re.sub(r'127\.0\.0\.1:\d+', 'httpbin.org', cmd) diff --git a/tests/utils/matching/parsing.py b/tests/utils/matching/parsing.py index e502d76bc8..b17d6acd04 100644 --- a/tests/utils/matching/parsing.py +++ b/tests/utils/matching/parsing.py @@ -18,7 +18,7 @@ def make_headers_re(message_type: Expect): non_crlf = rf'[^{CRLF}]' # language=RegExp - http_version = r'HTTP/\d+\.\d+' + http_version = r'HTTP/((\d+\.\d+)|\d+)' if message_type is Expect.REQUEST_HEADERS: # POST /post HTTP/1.1 start_line_re = fr'{non_crlf}*{http_version}{crlf}'