Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

❇️ Use prawcore bound to Niquests instead of Requests #2037

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ classifiers = [
"Topic :: Utilities"
]
dependencies = [
"prawcore >=2.4, <3",
"prawcore@git+https://github.com/Ousret/prawcore@feat-niquests",
"update_checker >=0.18",
"websocket-client >=0.54.0"
]
Expand Down Expand Up @@ -56,9 +56,7 @@ readthedocs = [
test = [
"betamax >=0.8, <0.9",
"betamax-matchers >=0.3.0, <0.5",
"pytest >=2.7.3",
"requests >=2.20.1, <3",
"urllib3 ==1.26.*, <2"
"pytest >=2.7.3"
]

[project.urls]
Expand All @@ -78,6 +76,11 @@ extend_exclude = ['./docs/examples/']
profile = 'black'
skip_glob = '.venv*'

[tool.pytest.ini_options]
# this avoids pytest loading betamax+Requests at boot.
# this allows us to patch betamax and makes it use Niquests instead.
addopts = "-p no:pytest-betamax"

[tool.ruff]
target-version = "py38"
include = [
Expand Down
82 changes: 81 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,51 @@
import socket
import time
from base64 import b64encode
from sys import platform
from sys import platform, modules
from urllib.parse import urlparse

import requests
import niquests
import urllib3

# betamax is tied to Requests
# and Niquests is almost entirely compatible with it.
# we can fool it without effort.
modules["requests"] = niquests
modules["requests.adapters"] = niquests.adapters
modules["requests.models"] = niquests.models
modules["requests.exceptions"] = niquests.exceptions
modules["requests.packages.urllib3"] = urllib3

# niquests no longer have a compat submodule
# but betamax need it. no worries, as betamax
# explicitly need requests, we'll give it to him.
modules["requests.compat"] = requests.compat

# doing the import now will make betamax working with Niquests!
# no extra effort.
import betamax

# the base mock does not implement close(), which is required
# for our HTTP client. No biggy.
betamax.mock_response.MockHTTPResponse.close = lambda _: None


# betamax have a tiny bug in URI matcher
# https://example.com != https://example.com/
# And Niquests does not enforce the trailing '/'
# when preparing a Request.
def _patched_parse(self, uri):
parsed = urlparse(uri)
return {
"scheme": parsed.scheme,
"netloc": parsed.netloc,
"path": parsed.path or "/",
"fragment": parsed.fragment,
}


betamax.matchers.uri.URIMatcher.parse = _patched_parse

import pytest

Expand All @@ -31,6 +75,42 @@ def _get_path(name):
return _get_path


@pytest.fixture(autouse=True)
def lax_content_length_strict(monkeypatch):
import io
import base64
from betamax.util import body_io
from urllib3 import HTTPResponse
from betamax.mock_response import MockHTTPResponse

# our cassettes are[...] pretty much broken.
# Some declared Content-Length don't match the bodies.
# Let's disable enforced content-length here.
def _patched_add_urllib3_response(serialized, response, headers):
if "base64_string" in serialized["body"]:
body = io.BytesIO(
base64.b64decode(serialized["body"]["base64_string"].encode())
)
else:
body = body_io(**serialized["body"])

h = HTTPResponse(
body,
status=response.status_code,
reason=response.reason,
headers=headers,
preload_content=False,
original_response=MockHTTPResponse(headers),
enforce_content_length=False,
)

response.raw = h

monkeypatch.setattr(
betamax.util, "add_urllib3_response", _patched_add_urllib3_response
)


def pytest_configure(config):
pytest.placeholders = Placeholders(placeholders)
config.addinivalue_line(
Expand Down
Loading