nakamura avatar nakamura committed 4d95f7b

split wildcard related tests from RootDispatchResourceTestCase.test_it

Comments (0)

Files changed (1)

tests/test_resources.py

         def yearMonthHandler(year, month):
             return (year, month)
 
-        @root.route('/files/<*path:str>')
-        def filesHandler(path):
-            return '/'.join(path)
-
         tr = Resource()
         root.route('/twisted/resource')(tr)
 
+        # test yearHandler route
         r = root.getChildWithDefault('archive', DummyRequest([''])) \
                 .getChildWithDefault('2013', None) \
                 .getChildWithDefault('', None)
         self.assertEqual(r._kwargs, {'year': 2013})
         self.assertEqual(r.render(None), 2013)
 
+        # test yearMonthHandler route
         r = root.getChildWithDefault('archive', DummyRequest([''])) \
                 .getChildWithDefault('2013', None) \
                 .getChildWithDefault('11', None) \
         self.assertEqual(r._kwargs, {'year': 2013, 'month': 11})
         self.assertEqual(r.render(None), (2013, 11))
 
-        r = root.getChildWithDefault('files', DummyRequest([''])) \
-                .getChildWithDefault('dirname', None) \
-                .getChildWithDefault('filename', None)
-
-        self.assertEqual(r._kwargs, {'path': ['dirname', 'filename']})
-        self.assertEqual(r.render(None), 'dirname/filename')
-
+        # test resource route
         r = root.getChildWithDefault('twisted', DummyRequest([''])) \
                 .getChildWithDefault('resource', None)
 
         self.assertIs(r, tr)
 
+        # test urlFor
         url = root.urlFor('yearMonthHandler', year=2013, month=12)
         self.assertEqual(url, '/archive/2013/12/')
 
         url = root.urlFor('year', year=2013)
         self.assertEqual(url, '/archive/2013/')
 
-        url = root.urlFor('filesHandler', path='dirname/filename')
-        self.assertEqual(url, '/files/dirname/filename')
-
     def test_404(self):
         from tsumuji.resources import RootDispatchResource
 
         self.assertIs(results['root'], root)
         self.assertIs(results['request'], req)
 
+    def test_wildcard(self):
+        from tsumuji.resources import RootDispatchResource
+
+        root = RootDispatchResource()
+
+        @root.route('/files/<*path:str>')
+        def filesHandler(path):
+            return '/'.join(path)
+
+        @root.route('/dirs/<*path:str>/')
+        def dirsHandler(path):
+            return '/'.join(path) + '/'
+
+        # test filesHandler route
+        r = root.getChildWithDefault('files', DummyRequest([''])) \
+                .getChildWithDefault('dirname', None) \
+                .getChildWithDefault('filename', None)
+
+        self.assertEqual(r._kwargs, {'path': ['dirname', 'filename']})
+        self.assertEqual(r.render(None), 'dirname/filename')
+
+        # test dirsHandler route
+        r = root.getChildWithDefault('dirs', DummyRequest([''])) \
+                .getChildWithDefault('dirname', None) \
+                .getChildWithDefault('more-direname', None) \
+                .getChildWithDefault('', None)
+
+        self.assertEqual(r._kwargs, {'path': ['dirname', 'more-direname']})
+        self.assertEqual(r.render(None), 'dirname/more-direname/')
+
+        url = root.urlFor('filesHandler', path='dirname/filename')
+        self.assertEqual(url, '/files/dirname/filename')
+
+        # /files/dirname/ should not be Found
+        req = DummyRequest([''])
+        r = root.getChildWithDefault('files', req) \
+                .getChildWithDefault('dirname', None) \
+                .getChildWithDefault('', None)
+
+        r.render(req)
+        self.assertEqual(req.responseCode, 404)
+
+        # /dirs/dirname/filename should not be Found
+        req = DummyRequest([''])
+        r = root.getChildWithDefault('dirs', req) \
+                .getChildWithDefault('dirname', None) \
+                .getChildWithDefault('filename', None)
+
+        r.render(req)
+        self.assertEqual(req.responseCode, 404)
+
 
 class MatcherTestCase(TestCase):
     def test_StaticMatcher(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.