Skip to content

Commit

Permalink
Support HTTPX 0.23.1 (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
lundberg authored Nov 18, 2022
1 parent bd12f51 commit 1486479
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -35,9 +35,7 @@ jobs:

lint:
name: Check Linting
uses: less-action/reusables/.github/workflows/pre-commit.yaml@v6
with:
python-version: "3.10"
uses: less-action/reusables/.github/workflows/pre-commit.yaml@v8

check-types:
name: Check Typing
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.10
python: python3.11
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
Expand All @@ -13,14 +13,14 @@ repos:
- id: debug-statements
- id: detect-private-key
- repo: https://github.com/asottile/pyupgrade
rev: v2.37.3
rev: v3.2.2
hooks:
- id: pyupgrade
args:
- --py37-plus
- --keep-runtime-typing
- repo: https://github.com/pycqa/autoflake
rev: v1.6.1
rev: v1.7.7
hooks:
- id: autoflake
args:
Expand All @@ -32,7 +32,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.8.0
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
Expand All @@ -47,7 +47,7 @@ repos:
- flake8-pytest-style
- flake8-datetimez
- repo: https://github.com/sirosen/check-jsonschema
rev: 0.18.2
rev: 0.19.2
hooks:
- id: check-github-workflows
- repo: https://github.com/asottile/yesqa
Expand All @@ -62,7 +62,7 @@ repos:
- flake8-pytest-style
- flake8-datetimez
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v2.7.1"
rev: "v3.0.0-alpha.4"
hooks:
- id: prettier
alias: format-markdown
Expand Down
4 changes: 3 additions & 1 deletion respx/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import httpcore
import httpx

from respx.patterns import parse_url

from .models import AllMockedAssertionError, PassThrough
from .transports import TryTransport

Expand Down Expand Up @@ -303,7 +305,7 @@ def to_httpx_request(cls, **kwargs):
)
return httpx.Request(
request.method,
raw_url,
parse_url(raw_url),
headers=request.headers,
stream=request.stream,
extensions=request.extensions,
Expand Down
34 changes: 31 additions & 3 deletions respx/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@

import httpx

from .types import CookieTypes, HeaderTypes, QueryParamTypes, URLPatternTypes
from .types import (
URL as RawURL,
CookieTypes,
HeaderTypes,
QueryParamTypes,
URLPatternTypes,
)


class Lookup(Enum):
Expand Down Expand Up @@ -448,7 +454,7 @@ class URL(Pattern):
def clean(self, value: URLPatternTypes) -> Union[str, RegexPattern[str]]:
url: Union[str, RegexPattern[str]]
if self.lookup is Lookup.EQUAL and isinstance(value, (str, tuple, httpx.URL)):
_url = httpx.URL(value)
_url = parse_url(value)
_url = self._ensure_path(_url)
url = str(_url)
elif self.lookup is Lookup.REGEX and isinstance(value, str):
Expand Down Expand Up @@ -586,6 +592,28 @@ def combine(patterns: Sequence[Pattern], op: Callable = operator.and_) -> Patter
return reduce(op, patterns)


def parse_url(value: Union[httpx.URL, str, RawURL]) -> httpx.URL:
url: Union[httpx.URL, str]

if isinstance(value, tuple):
# Handle "raw" httpcore urls. Borrowed from HTTPX prior to #2241
raw_scheme, raw_host, port, raw_path = value
scheme = raw_scheme.decode("ascii")
host = raw_host.decode("ascii")
if host and ":" in host and host[0] != "[":
# it's an IPv6 address, so it should be enclosed in "[" and "]"
# ref: https://tools.ietf.org/html/rfc2732#section-2
# ref: https://tools.ietf.org/html/rfc3986#section-3.2.2
host = f"[{host}]"
port_str = "" if port is None else f":{port}"
path = raw_path.decode("ascii")
url = f"{scheme}://{host}{port_str}{path}"
else:
url = value

return httpx.URL(url)


def parse_url_patterns(
url: Optional[URLPatternTypes], exact: bool = True
) -> Dict[str, Pattern]:
Expand All @@ -596,7 +624,7 @@ def parse_url_patterns(
if isinstance(url, RegexPattern):
return {"url": URL(url, lookup=Lookup.REGEX)}

url = httpx.URL(url)
url = parse_url(url)
scheme_port = get_scheme_port(url.scheme)

if url.scheme and url.scheme != "all":
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
project_urls={
"GitHub": "https://github.com/lundberg/respx",
Expand Down
12 changes: 11 additions & 1 deletion tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ def test_path_pattern():
[
(Lookup.CONTAINS, "", "https://foo.bar/", True),
(Lookup.CONTAINS, "x=1", "https://foo.bar/?x=1", True),
(Lookup.CONTAINS, "x=", "https://foo.bar/?x=1", True), # query, not params
(Lookup.CONTAINS, "x=", "https://foo.bar/?x=1", False), # False by httpx #2354
(Lookup.CONTAINS, "x=", "https://foo.bar/?x=", True),
(Lookup.CONTAINS, "y=2", "https://foo.bar/?x=1", False),
(Lookup.CONTAINS, [("x", "1")], "https://foo.bar/?x=1", True),
(Lookup.CONTAINS, {"x": "1"}, "https://foo.bar/?x=1", True),
Expand All @@ -239,6 +240,8 @@ def test_path_pattern():
(Lookup.CONTAINS, [("x", ANY), ("x", "2")], "https://foo.bar/?x=2&x=3", False),
(Lookup.CONTAINS, "x=1&y=2", "https://foo.bar/?x=1", False),
(Lookup.EQUAL, "", "https://foo.bar/", True),
(Lookup.EQUAL, "x", "https://foo.bar/?x", True),
(Lookup.EQUAL, "x=", "https://foo.bar/?x=", True),
(Lookup.EQUAL, "x=1", "https://foo.bar/?x=1", True),
(Lookup.EQUAL, "y=2", "https://foo.bar/?x=1", False),
(Lookup.EQUAL, {"x": ANY}, "https://foo.bar/?x=1", True),
Expand Down Expand Up @@ -275,6 +278,13 @@ def test_params_pattern_hash():
(Lookup.EQUAL, "https://a.b/?x=y", {}, "https://a.b?x=y", True),
(Lookup.STARTS_WITH, "https://a.b/b", {}, "https://a.b/baz/", True),
(Lookup.STARTS_WITH, "http://a.b/baz/", {}, "https://a.b/baz/", False),
(
Lookup.EQUAL,
(b"https", b"fake:ipv6", None, b""),
{},
"https://[fake:ipv6]",
True,
),
],
)
def test_url_pattern(lookup, value, context, url, expected):
Expand Down

0 comments on commit 1486479

Please sign in to comment.