Skip to content

Commit

Permalink
Enhance AND pattern evaluation to fail fast
Browse files Browse the repository at this point in the history
  • Loading branch information
lundberg committed Nov 9, 2021
1 parent 97eb9ae commit 2f9ca19
Showing 1 changed file with 8 additions and 10 deletions.
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 @@ -181,11 +180,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 Match(False)
b_match = b.match(request)
if not b_match:
return Match(False)
return Match(True, **{**a_match.context, **b_match.context})


class _Or(Pattern):
Expand Down Expand Up @@ -462,10 +463,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

0 comments on commit 2f9ca19

Please sign in to comment.