Skip to content

Commit

Permalink
Ensure body is tested matching regex pattern type
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed Jun 21, 2024
1 parent ce76ea8 commit 34fab31
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/pook/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def matches(x, y, regex_expr=False):
# Parse regex expression, if needed
x = strip_regex(x) if regex_expr and isregex_expr(x) else x

if isinstance(x.pattern, str) and hasattr(y, "decode"):
y = y.decode("utf-8", "backslashescape")

# Assert regular expression via unittest matchers
return test_case().assertRegex(y, x) or True

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/mock_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import itertools
from textwrap import dedent
import re

import pook
from pook.mock import Mock
Expand Down Expand Up @@ -213,6 +214,26 @@ def test_body_not_matches(httpbin, mock, mock_body, request_body):
assert errors[0].startswith("BodyMatcher: ")


def test_body_matches_string_regex(httpbin, mock):
mock.body(re.compile(r"hello, me!"))
req = Request(
url=httpbin.url,
body="This is a big sentence... hello, me! wow, another part",
)

assert mock.match(req) == (True, [])


def test_body_matches_bytes_regex(httpbin, mock):
mock.body(re.compile(rb"hello, me!"))
req = Request(
url=httpbin.url,
body="This is a big sentence... hello, me! wow, another part",
)

assert mock.match(req) == (True, [])


def test_xml_matches(httpbin, mock):
xml = dedent(
"""
Expand Down

0 comments on commit 34fab31

Please sign in to comment.