summaryrefslogtreecommitdiff
path: root/cherrypy/test/test_httputil.py
blob: fe6a3f410a8701b6b484f364663cf810f3e1a9a1 (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
"""Test helpers from ``cherrypy.lib.httputil`` module."""
import pytest
import http.client

from cherrypy.lib import httputil


@pytest.mark.parametrize(
    'script_name,path_info,expected_url',
    [
        ('/sn/', '/pi/', '/sn/pi/'),
        ('/sn/', '/pi', '/sn/pi'),
        ('/sn/', '/', '/sn/'),
        ('/sn/', '', '/sn/'),
        ('/sn', '/pi/', '/sn/pi/'),
        ('/sn', '/pi', '/sn/pi'),
        ('/sn', '/', '/sn/'),
        ('/sn', '', '/sn'),
        ('/', '/pi/', '/pi/'),
        ('/', '/pi', '/pi'),
        ('/', '/', '/'),
        ('/', '', '/'),
        ('', '/pi/', '/pi/'),
        ('', '/pi', '/pi'),
        ('', '/', '/'),
        ('', '', '/'),
    ]
)
def test_urljoin(script_name, path_info, expected_url):
    """Test all slash+atom combinations for SCRIPT_NAME and PATH_INFO."""
    actual_url = httputil.urljoin(script_name, path_info)
    assert actual_url == expected_url


EXPECTED_200 = (200, 'OK', 'Request fulfilled, document follows')
EXPECTED_500 = (
    500,
    'Internal Server Error',
    'The server encountered an unexpected condition which '
    'prevented it from fulfilling the request.',
)
EXPECTED_404 = (404, 'Not Found', 'Nothing matches the given URI')
EXPECTED_444 = (444, 'Non-existent reason', '')


@pytest.mark.parametrize(
    'status,expected_status',
    [
        (None, EXPECTED_200),
        (200, EXPECTED_200),
        ('500', EXPECTED_500),
        (http.client.NOT_FOUND, EXPECTED_404),
        ('444 Non-existent reason', EXPECTED_444),
    ]
)
def test_valid_status(status, expected_status):
    """Check valid int, string and http.client-constants
    statuses processing."""
    assert httputil.valid_status(status) == expected_status


@pytest.mark.parametrize(
    'status_code,error_msg',
    [
        ('hey', "Illegal response status from server ('hey' is non-numeric)."),
        (
            {'hey': 'hi'},
            'Illegal response status from server '
            "({'hey': 'hi'} is non-numeric).",
        ),
        (1, 'Illegal response status from server (1 is out of range).'),
        (600, 'Illegal response status from server (600 is out of range).'),
    ]
)
def test_invalid_status(status_code, error_msg):
    """Check that invalid status cause certain errors."""
    with pytest.raises(ValueError) as excinfo:
        httputil.valid_status(status_code)

    assert error_msg in str(excinfo)