summaryrefslogtreecommitdiff
path: root/repoze/bfg/tests/test_urldispatch.py
blob: 68fda032d084b0d4b9d3fb6450277dea062f4e63 (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
import unittest
from repoze.bfg.testing import cleanUp

class RoutesRootFactoryTests(unittest.TestCase):
    def setUp(self):
        cleanUp()

    def tearDown(self):
        cleanUp()
        
    def _getEnviron(self, **kw):
        environ = {'SERVER_NAME':'localhost',
                   'wsgi.url_scheme':'http'}
        environ.update(kw)
        return environ

    def _getTargetClass(self):
        from repoze.bfg.urldispatch import RoutesRootFactory
        return RoutesRootFactory

    def _makeOne(self, get_root):
        klass = self._getTargetClass()
        return klass(get_root)

    def test_init_default_root_factory(self):
        mapper = self._makeOne(None)
        self.assertEqual(mapper.default_root_factory, None)

    def test_no_route_matches(self):
        get_root = make_get_root(123)
        mapper = self._makeOne(get_root)
        environ = self._getEnviron(PATH_INFO='/')
        result = mapper(environ)
        self.assertEqual(result, 123)
        self.assertEqual(mapper.environ, environ)

    def test_route_matches(self):
        get_root = make_get_root(123)
        mapper = self._makeOne(get_root)
        mapper.connect('foo', 'archives/:action/:article', foo='foo')
        environ = self._getEnviron(PATH_INFO='/archives/action1/article1')
        result = mapper(environ)
        self.assertEqual(result, 123)
        routing_args = environ['wsgiorg.routing_args'][1]
        self.assertEqual(routing_args['foo'], 'foo')
        self.assertEqual(routing_args['action'], 'action1')
        self.assertEqual(routing_args['article'], 'article1')
        self.assertEqual(environ['bfg.routes.matchdict'], routing_args)
        self.assertEqual(environ['bfg.routes.route'].name, 'foo')

    def test_unnamed_root_route_matches(self):
        root_factory = make_get_root(123)
        mapper = self._makeOne(root_factory)
        mapper.connect('')
        environ = self._getEnviron(PATH_INFO='/')
        result = mapper(environ)
        self.assertEqual(result, 123)
        self.assertEqual(environ['bfg.routes.route'].name, None)
        self.assertEqual(environ['bfg.routes.matchdict'], {})
        self.assertEqual(environ['wsgiorg.routing_args'], ((), {}))

    def test_named_root_route_matches(self):
        root_factory = make_get_root(123)
        mapper = self._makeOne(root_factory)
        mapper.connect('root', '')
        environ = self._getEnviron(PATH_INFO='/')
        result = mapper(environ)
        self.assertEqual(result, 123)
        self.assertEqual(environ['bfg.routes.route'].name, 'root')
        self.assertEqual(environ['bfg.routes.matchdict'], {})
        self.assertEqual(environ['wsgiorg.routing_args'], ((), {}))

    def test_unicode_in_route_default(self):
        root_factory = make_get_root(123)
        mapper = self._makeOne(root_factory)
        class DummyRoute:
            routepath = ':id'
            _factory = None
        la = unicode('\xc3\xb1a', 'utf-8')
        mapper.routematch = lambda *arg: ({la:'id'}, DummyRoute)
        mapper.connect('whatever', ':la')
        environ = self._getEnviron(PATH_INFO='/foo')
        result = mapper(environ)
        self.assertEqual(result, 123)
        self.assertEqual(environ['bfg.routes.route'], DummyRoute)
        self.assertEqual(environ['bfg.routes.matchdict'], {u'\xf1a': 'id'})
        routing_args = environ['wsgiorg.routing_args'][1]
        self.assertEqual(routing_args[la], 'id')

    def test_fallback_to_default_root_factory(self):
        root_factory = make_get_root(123)
        mapper = self._makeOne(root_factory)
        mapper.connect('wont', 'wont/:be/:found')
        environ = self._getEnviron(PATH_INFO='/archives/action1/article1')
        result = mapper(environ)
        self.assertEqual(result, 123)

    def test_has_routes(self):
        mapper = self._makeOne(None)
        self.assertEqual(mapper.has_routes(), False)
        mapper.connect('whatever', 'archives/:action/:article')
        self.assertEqual(mapper.has_routes(), True)

    def test_url_for(self):
        root_factory = make_get_root(None)
        mapper = self._makeOne(root_factory)
        mapper.connect('whatever', 'archives/:action/:article')
        environ = self._getEnviron(PATH_INFO='/archives/action1/article1')
        result = mapper(environ)
        from routes import url_for
        result = url_for(action='action2', article='article2')
        self.assertEqual(result, '/archives/action2/article2')

def make_get_root(result):
    def dummy_get_root(environ):
        return result
    return dummy_get_root

class DummyContext(object):
    """ """
        
class DummyRequest(object):
    """ """