appctx.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. """
  3. flask.testsuite.appctx
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. Tests the application context.
  6. :copyright: (c) 2012 by Armin Ronacher.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import flask
  10. import unittest
  11. from flask.testsuite import FlaskTestCase
  12. class AppContextTestCase(FlaskTestCase):
  13. def test_basic_url_generation(self):
  14. app = flask.Flask(__name__)
  15. app.config['SERVER_NAME'] = 'localhost'
  16. app.config['PREFERRED_URL_SCHEME'] = 'https'
  17. @app.route('/')
  18. def index():
  19. pass
  20. with app.app_context():
  21. rv = flask.url_for('index')
  22. self.assert_equal(rv, 'https://localhost/')
  23. def test_url_generation_requires_server_name(self):
  24. app = flask.Flask(__name__)
  25. with app.app_context():
  26. with self.assert_raises(RuntimeError):
  27. flask.url_for('index')
  28. def test_url_generation_without_context_fails(self):
  29. with self.assert_raises(RuntimeError):
  30. flask.url_for('index')
  31. def test_request_context_means_app_context(self):
  32. app = flask.Flask(__name__)
  33. with app.test_request_context():
  34. self.assert_equal(flask.current_app._get_current_object(), app)
  35. self.assert_equal(flask._app_ctx_stack.top, None)
  36. def test_app_context_provides_current_app(self):
  37. app = flask.Flask(__name__)
  38. with app.app_context():
  39. self.assert_equal(flask.current_app._get_current_object(), app)
  40. self.assert_equal(flask._app_ctx_stack.top, None)
  41. def test_app_tearing_down(self):
  42. cleanup_stuff = []
  43. app = flask.Flask(__name__)
  44. @app.teardown_appcontext
  45. def cleanup(exception):
  46. cleanup_stuff.append(exception)
  47. with app.app_context():
  48. pass
  49. self.assert_equal(cleanup_stuff, [None])
  50. def test_custom_app_ctx_globals_class(self):
  51. class CustomRequestGlobals(object):
  52. def __init__(self):
  53. self.spam = 'eggs'
  54. app = flask.Flask(__name__)
  55. app.app_ctx_globals_class = CustomRequestGlobals
  56. with app.app_context():
  57. self.assert_equal(
  58. flask.render_template_string('{{ g.spam }}'), 'eggs')
  59. def test_context_refcounts(self):
  60. called = []
  61. app = flask.Flask(__name__)
  62. @app.teardown_request
  63. def teardown_req(error=None):
  64. called.append('request')
  65. @app.teardown_appcontext
  66. def teardown_app(error=None):
  67. called.append('app')
  68. @app.route('/')
  69. def index():
  70. with flask._app_ctx_stack.top:
  71. with flask._request_ctx_stack.top:
  72. pass
  73. self.assert_true(flask._request_ctx_stack.top.request.environ
  74. ['werkzeug.request'] is not None)
  75. return u''
  76. c = app.test_client()
  77. c.get('/')
  78. self.assertEqual(called, ['request', 'app'])
  79. def suite():
  80. suite = unittest.TestSuite()
  81. suite.addTest(unittest.makeSuite(AppContextTestCase))
  82. return suite