Files
deb-python-falcon/tests/test_cmd_print_api.py
Stephen Milner d9d1aed01e feat(cmd): Add a utility to print routes (#849)
Add a new CLI utility, falcon-print-routes, that takes in a module:instance_or_callable, 
introspects the routes, and prints the results to stdout.

Example:

   $ falcon-print-routes commissaire.testroutes:a
   -> /api/v0/status
   -> /api/v0/cluster/{name}
   -> /api/v0/cluster/{name}/hosts
   -> /api/v0/cluster/{name}/hosts/{address}
2016-09-05 17:18:49 -06:00

53 lines
1.3 KiB
Python

import sys
import testtools
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from falcon import API
from falcon.cmd import print_routes
_api = API()
_api.add_route('/test', None)
STDOUT = sys.stdout
class TestPrintRoutes(testtools.TestCase):
def setUp(self):
"""Capture stdout"""
super(TestPrintRoutes, self).setUp()
self.output = StringIO()
sys.stdout = self.output
def tearDown(self):
"""Reset stdout"""
super(TestPrintRoutes, self).tearDown()
self.output.close()
del self.output
sys.stdout = STDOUT
def test_traverse_with_verbose(self):
"""Ensure traverse finds the proper routes and adds verbose output."""
print_routes.traverse(
_api._router._roots,
verbose=True)
route, options = self.output.getvalue().strip().split('\n')
self.assertEquals('-> /test', route)
self.assertTrue('OPTIONS' in options)
self.assertTrue('falcon/falcon/responders.py:' in options)
def test_traverse(self):
"""Ensure traverse finds the proper routes."""
print_routes.traverse(
_api._router._roots,
verbose=False)
route = self.output.getvalue().strip()
self.assertEquals('-> /test', route)