summaryrefslogtreecommitdiff
path: root/tests/test_functional/test_explicit_use.py
blob: b1e1cd767b6171e95d44f727433561432fd8b0eb (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""test_explicit_use"""
import os, sys, time, unittest
from nose.tools import eq_, assert_raises, assert_is_none

from routes import *
from routes.route import Route
from routes.util import GenerationException

class TestUtils(unittest.TestCase):
    def test_route_dict_use(self):
        m = Mapper()
        m.explicit = True
        m.connect('/hi/{fred}')

        environ = {'HTTP_HOST': 'localhost'}

        env = environ.copy()
        env['PATH_INFO'] = '/hi/george'

        eq_({'fred': 'george'}, m.match(environ=env))

    def test_x_forwarded(self):
        m = Mapper()
        m.explicit = True
        m.connect('/hi/{fred}')

        environ = {'HTTP_X_FORWARDED_HOST': 'localhost'}
        url = URLGenerator(m, environ)
        eq_('http://localhost/hi/smith', url(fred='smith', qualified=True))

    def test_server_port(self):
        m = Mapper()
        m.explicit = True
        m.connect('/hi/{fred}')

        environ = {'SERVER_NAME': 'localhost', 'wsgi.url_scheme': 'https',
                   'SERVER_PORT': '993'}
        url = URLGenerator(m, environ)
        eq_('https://localhost:993/hi/smith', url(fred='smith', qualified=True))

    def test_subdomain_screen(self):
        m = Mapper()
        m.explicit = True
        m.sub_domains = True
        m.connect('/hi/{fred}')

        environ = {'HTTP_HOST': 'localhost.com'}
        url = URLGenerator(m, environ)
        eq_('http://home.localhost.com/hi/smith', url(fred='smith', sub_domain=u'home', qualified=True))

        environ = {'HTTP_HOST': 'here.localhost.com', 'PATH_INFO': '/hi/smith'}
        url = URLGenerator(m, environ.copy())
        assert_raises(GenerationException, lambda: url.current(qualified=True))

        environ = {'HTTP_HOST': 'subdomain.localhost.com'}
        url = URLGenerator(m, environ.copy())
        eq_('http://sub.localhost.com/hi/smith', url(fred='smith', sub_domain='sub', qualified=True))

        environ = {'HTTP_HOST': 'sub.sub.localhost.com'}
        url = URLGenerator(m, environ.copy())
        eq_('http://new.localhost.com/hi/smith', url(fred='smith', sub_domain='new', qualified=True))

        url = URLGenerator(m, {})
        eq_('/hi/smith', url(fred='smith', sub_domain=u'home'))

    def test_anchor(self):
        m = Mapper()
        m.explicit = True
        m.connect('/hi/{fred}')

        environ = {'HTTP_HOST': 'localhost.com'}
        url = URLGenerator(m, environ)
        eq_('/hi/smith#here', url(fred='smith', anchor='here'))

    def test_static_args(self):
        m = Mapper()
        m.explicit = True
        m.connect('http://google.com/', _static=True)

        url = URLGenerator(m, {})

        eq_('/here?q=fred&q=here%20now', url('/here', q=[u'fred', 'here now']))

    def test_current(self):
        m = Mapper()
        m.explicit = True
        m.connect('/hi/{fred}')

        environ = {'HTTP_HOST': 'localhost.com', 'PATH_INFO': '/hi/smith'}
        match = m.routematch(environ=environ)[0]
        environ['wsgiorg.routing_args'] = (None, match)
        url = URLGenerator(m, environ)
        eq_('/hi/smith', url.current())

    def test_add_routes(self):
        map = Mapper(explicit=True)
        map.minimization = False
        routes = [
            Route('foo', '/foo',)
        ]
        map.extend(routes)
        eq_(map.match('/foo'), {})

    def test_add_routes_conditions_unmet(self):
        map = Mapper(explicit=True)
        map.minimization = False
        routes = [
            Route('foo', '/foo', conditions=dict(method=["POST"]))
        ]
        environ = {
            'HTTP_HOST': 'localhost.com',
            'PATH_INFO': '/foo',
            'REQUEST_METHOD': 'GET',
        }
        map.extend(routes)
        assert_is_none(map.match('/foo', environ=environ))

    def test_add_routes_conditions_met(self):
        map = Mapper(explicit=True)
        map.minimization = False
        routes = [
            Route('foo', '/foo', conditions=dict(method=["POST"]))
        ]
        environ = {
            'HTTP_HOST': 'localhost.com',
            'PATH_INFO': '/foo',
            'REQUEST_METHOD': 'POST',
        }
        map.extend(routes)
        eq_(map.match('/foo', environ=environ), {})

    def test_using_func(self):
        def fred(view):
            pass

        m = Mapper()
        m.explicit = True
        m.connect('/hi/{fred}', controller=fred)

        environ = {'HTTP_HOST': 'localhost.com', 'PATH_INFO': '/hi/smith'}
        match = m.routematch(environ=environ)[0]
        environ['wsgiorg.routing_args'] = (None, match)
        url = URLGenerator(m, environ)
        eq_('/hi/smith', url.current())

    def test_using_prefix(self):
        m = Mapper()
        m.explicit = True
        m.connect('/{first}/{last}')

        environ = {'HTTP_HOST': 'localhost.com', 'PATH_INFO': '/content/index',
                   'SCRIPT_NAME': '/jones'}
        match = m.routematch(environ=environ)[0]
        environ['wsgiorg.routing_args'] = (None, match)
        url = URLGenerator(m, environ)

        eq_('/jones/content/index', url.current())
        eq_('/jones/smith/barney', url(first='smith', last='barney'))

    def test_with_host_param(self):
        m = Mapper()
        m.explicit = True
        m.connect('/hi/{fred}')

        environ = {'HTTP_HOST': 'localhost.com'}
        url = URLGenerator(m, environ)
        eq_('/hi/smith?host=here', url(fred='smith', host_='here'))