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

fix flask integration #182

Merged
merged 5 commits into from
Mar 15, 2025
Merged
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
29 changes: 22 additions & 7 deletions httpdbg/hooks/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

def set_hook_flask_endpoint(records: HTTPRecords, method: Callable):

@wraps(method)
@wraps(
method
) # to avoid AssertionError: View function mapping is overwriting an existing endpoint function: hook
def hook(*args, **kwargs):

with httpdbg_endpoint(
Expand All @@ -28,6 +30,11 @@ def hook(*args, **kwargs):
return hook


# we must not apply the hook more than once on a mapped endpoint function
# AssertionError: View function mapping is overwriting an existing endpoint function: xxxx
already_mapped = {}


def set_hook_flask_add_url_rule(records: HTTPRecords, method: Callable):

def hook(*args, **kwargs):
Expand All @@ -38,9 +45,13 @@ def hook(*args, **kwargs):

if param in callargs:
original_view_func = callargs[param]
callargs[param] = decorate(
records, callargs[param], set_hook_flask_endpoint
)
if original_view_func not in already_mapped:
callargs[param] = decorate(
records, callargs[param], set_hook_flask_endpoint
)
already_mapped[original_view_func] = callargs[param]
else:
callargs[param] = already_mapped[original_view_func]

args = [callargs[param] if x == original_view_func else x for x in args]
if param in kwargs:
Expand All @@ -61,9 +72,13 @@ def hook(*args, **kwargs):

if param in callargs:
original_view_func = callargs[param]
callargs[param] = decorate(
records, callargs[param], set_hook_flask_endpoint
)
if original_view_func not in already_mapped:
callargs[param] = decorate(
records, callargs[param], set_hook_flask_endpoint
)
already_mapped[original_view_func] = callargs[param]
else:
callargs[param] = already_mapped[original_view_func]

args = [callargs[param] if x == original_view_func else x for x in args]
if param in kwargs:
Expand Down
2 changes: 1 addition & 1 deletion httpdbg/hooks/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def hook(*args, **kwargs):
if "item" in callargs:
full_label = getattr(callargs["item"], "nodeid", "unknown test")
label = getattr(callargs["item"], "name", "unknown test")
with httpdbg_group(records, label, full_label):
with httpdbg_group(records, label, full_label, updatable=False):
return method(*args, **kwargs)
else:
return method(*args, **kwargs)
Expand Down
10 changes: 5 additions & 5 deletions httpdbg/hooks/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def hook(*args, **kwargs):
test_module_path = os.path.relpath(test_module_path)
full_label = f"{test_module_path}::{full_label}"

with httpdbg_group(records, label, full_label):
with httpdbg_group(records, label, full_label, updatable=False):
ret = method(*args, **kwargs)
else:
ret = method(*args, **kwargs)
Expand Down Expand Up @@ -79,7 +79,7 @@ def hook(*args, **kwargs):
test_module_path = os.path.relpath(test_module_path)
full_label = f"{test_module_path}::{full_label}"

with httpdbg_group(records, label, full_label):
with httpdbg_group(records, label, full_label, updatable=False):
return method(*args, **kwargs)
else:
return method(*args, **kwargs)
Expand Down Expand Up @@ -116,7 +116,7 @@ def hook(*args, **kwargs):
test_module_path = os.path.relpath(test_module_path)
full_label = f"{test_module_path}::{full_label}"

with httpdbg_group(records, label, full_label):
with httpdbg_group(records, label, full_label, updatable=False):
return method(*args, **kwargs)
else:
return method(*args, **kwargs)
Expand Down Expand Up @@ -147,7 +147,7 @@ def hook(*args, **kwargs):
test_module_path = os.path.relpath(test_module_path)
full_label = f"{test_module_path}::setUpModule"

with httpdbg_group(records, label, full_label):
with httpdbg_group(records, label, full_label, updatable=False):
return method(*args, **kwargs)
else:
return method(*args, **kwargs)
Expand Down Expand Up @@ -191,7 +191,7 @@ def hook(*args, **kwargs):
test_module_path = os.path.relpath(test_module_path)
full_label = f"{test_module_path}::tearDownModule"

with httpdbg_group(records, label, full_label):
with httpdbg_group(records, label, full_label, updatable=False):
return method(*args, **kwargs)
else:
return method(*args, **kwargs)
Expand Down
13 changes: 9 additions & 4 deletions httpdbg/initiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ def to_json(self, full: bool = True) -> dict:


class Group:
def __init__(self, label: str, full_label: str):
def __init__(self, label: str, full_label: str, updatable: bool):
self.id: str = get_new_uuid()
self.label: str = label
self.full_label: str = full_label
self.updatable: bool = updatable
self.tbegin: datetime.datetime = datetime.datetime.now(datetime.timezone.utc)

def to_json(self) -> dict:
Expand Down Expand Up @@ -319,7 +320,11 @@ def httpdbg_tag(tag: str) -> Generator[None, None, None]:

@contextmanager
def httpdbg_group(
records: "HTTPRecords", label: str, full_label: str, update: bool = False
records: "HTTPRecords",
label: str,
full_label: str,
update: bool = False,
updatable: bool = True,
) -> Generator[Group, None, None]:

# A group is considered set if the environment variable exists and the group
Expand All @@ -332,7 +337,7 @@ def httpdbg_group(

if not group_already_set:
logger().info("httpdbg_group (new)")
group = Group(label, full_label)
group = Group(label, full_label, updatable=updatable)
# in case of a reentrant call to httpdbg, we force the group id to be the same
if HTTPDBG_CURRENT_GROUP in os.environ:
group.id = os.environ[HTTPDBG_CURRENT_GROUP]
Expand All @@ -342,7 +347,7 @@ def httpdbg_group(
else:
group = records.groups[os.environ[HTTPDBG_CURRENT_GROUP]]

if update:
if update and group.updatable:
# Update the label and full_label of an existing group, in case of endpoint.
group.label = label
group.full_label = full_label
Expand Down