File: url_test.py

package info (click to toggle)
python-pook 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: python: 3,558; makefile: 13
file content (71 lines) | stat: -rw-r--r-- 2,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# flake8: noqa

import re
import pytest
from functools import partial

from pook.request import Request
from pook.matchers.url import URLMatcher


def run_test(match_url, url, matches, regex=False):
    req = Request(url=url)

    if regex:
        match_url = re.compile(match_url, re.IGNORECASE)

    if matches:
        assert URLMatcher(match_url).match(req)
    else:
        with pytest.raises(Exception):
            URLMatcher(match_url).match(req)


@pytest.mark.parametrize(
    ("match_url", "url", "matches"),
    (
        # Valid cases
        ("http://foo.com", "http://foo.com", True),
        ("http://foo.com:80", "http://foo.com:80", True),
        ("http://foo.com", "http://foo.com/foo/bar", True),
        ("http://foo.com/foo", "http://foo.com/foo", True),
        ("http://foo.com/foo/bar", "http://foo.com/foo/bar", True),
        ("http://foo.com/foo/bar/baz", "http://foo.com/foo/bar/baz", True),
        ("http://foo.com/foo?x=y&z=w", "http://foo.com/foo?x=y&z=w", True),
        # Invalid cases
        ("http://foo.com", "http://bar.com", False),
        ("http://foo.com:80", "http://foo.com:443", False),
        ("http://foo.com/foo", "http://foo.com", False),
        ("http://foo.com/foo", "http://foo.com/bar", False),
        ("http://foo.com/foo/bar", "http://foo.com/bar/foo", False),
        ("http://foo.com/foo/bar/baz", "http://foo.com/baz/bar/foo", False),
        ("http://foo.com/foo?x=y&z=w", "http://foo.com/foo?x=x&y=y", False),
    ),
)
def test_url_matcher_urlparse(match_url, url, matches):
    run_test(match_url, url, matches, regex=False)


@pytest.mark.parametrize(
    ("match_url", "url", "matches"),
    (
        # Valid cases
        ("http://foo.com", "http://foo.com", True),
        ("http://foo.com:80", "http://foo.com:80", True),
        ("^http://foo.com", "http://foo.com/foo/bar", True),
        ("http://foo.com/foo", "http://foo.com/foo", True),
        ("http://foo.com/foo/bar", "http://foo.com/foo/bar", True),
        ("http://foo.com/foo/bar/baz", "http://foo.com/foo/bar/baz", True),
        (r"http://foo.com/foo\?x=[0-9]", "http://foo.com/foo?x=5", True),
        # Invalid cases
        ("http://foo.com", "http://bar.com", False),
        ("http://foo.com:80", "http://foo.com:443", False),
        ("^http://foo.com$", "http://foo.com/bar", False),
        ("http://foo.com/foo", "http://foo.com/bar", False),
        ("http://foo.com/foo/bar", "http://foo.com/bar/foo", False),
        ("http://foo.com/foo/bar/baz", "http://foo.com/baz/bar/foo", False),
        (r"http://foo.com/foo\?x=[1-3]", "http://foo.com/foo?x=5", False),
    ),
)
def test_url_matcher_regex(match_url, url, matches):
    run_test(match_url, url, matches, regex=True)