aboutsummaryrefslogtreecommitdiff
path: root/test/unit/test_patterns.py
blob: 4162fc03c192e7aacdaafbfecbd17f254cc9bbca (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# SPDX-License-Identifier: CC0-1.0

"""
Haketilo unit tests - URL patterns
"""

# This file is part of Haketilo
#
# Copyright (C) 2021, Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the CC0 1.0 Universal License as published by
# the Creative Commons Corporation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# CC0 1.0 Universal License for more details.

import pytest

from ..script_loader import load_script

@pytest.fixture(scope="session")
def patterns_code():
    yield load_script('common/patterns.js', ['common'])

def test_regexes(execute_in_page, patterns_code):
    """
    patterns.js contains regexes used for URL parsing.
    Verify they work properly.
    """
    execute_in_page(patterns_code, page='https://gotmyowndoma.in')

    valid_url = 'https://example.com/a/b?ver=1.2.3#heading2'
    valid_url_rest = 'example.com/a/b?ver=1.2.3#heading2'

    # Test matching of URL protocol.
    match = execute_in_page('returnval(proto_regex.exec(arguments[0]));',
                            valid_url)
    assert match
    assert match[1] == 'https'
    assert match[2] == valid_url_rest

    match = execute_in_page('returnval(proto_regex.exec(arguments[0]));',
                            '://bad-url.missing/protocol')
    assert match is None

    # Test matching of http(s) URLs.
    match = execute_in_page('returnval(http_regex.exec(arguments[0]));',
                            valid_url_rest)
    assert match
    assert match[1] == 'example.com'
    assert match[2] == '/a/b'
    assert match[3] == '?ver=1.2.3'

    match = execute_in_page('returnval(http_regex.exec(arguments[0]));',
                            'another.example.com')
    assert match
    assert match[1] == 'another.example.com'
    assert match[2] == ''
    assert match[3] == ''

    match = execute_in_page('returnval(http_regex.exec(arguments[0]));',
                            '/bad/http/example')
    assert match == None

    # Test matching of file URLs.
    match = execute_in_page('returnval(file_regex.exec(arguments[0]));',
                            '/good/file/example')
    assert match
    assert match[1] == '/good/file/example'

    # Test matching of ftp URLs.
    match = execute_in_page('returnval(ftp_regex.exec(arguments[0]));',
                            'example.com/a/b#heading2')
    assert match
    assert match[1] is None
    assert match[2] == 'example.com'
    assert match[3] == '/a/b'

    match = execute_in_page('returnval(ftp_regex.exec(arguments[0]));',
                            'some_user@localhost')
    assert match
    assert match[1] == 'some_user@'
    assert match[2] == 'localhost'
    assert match[3] == ''

    match = execute_in_page('returnval(ftp_regex.exec(arguments[0]));',
                            '@bad.url/')
    assert match is None