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

Enhance AND pattern evaluation to fail fast #185

Merged
merged 3 commits into from
Nov 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions respx/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from enum import Enum
from functools import reduce
from http.cookies import SimpleCookie
from json.decoder import JSONDecodeError
from types import MappingProxyType
from typing import (
Any,
Expand Down Expand Up @@ -184,11 +183,13 @@ def __iter__(self):

def match(self, request: httpx.Request) -> Match:
a, b = self.value
match1 = a.match(request)
match2 = b.match(request)
if match1 and match2:
return Match(True, **{**match1.context, **match2.context})
return Match(False)
a_match = a.match(request)
if not a_match:
return a_match
b_match = b.match(request)
if not b_match:
return b_match
return Match(True, **{**a_match.context, **b_match.context})


class _Or(Pattern):
Expand Down Expand Up @@ -465,10 +466,7 @@ def clean(self, value: Union[str, List, Dict]) -> str:

def parse(self, request: httpx.Request) -> str:
content = super().parse(request)
try:
json = jsonlib.loads(content.decode("utf-8"))
except JSONDecodeError:
return ""
json = jsonlib.loads(content.decode("utf-8"))

if self.path:
value = json
Expand Down