1# (c) 2007 Philip Jenvey; written for Paste (http://pythonpaste.org) 2# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 3from nose.tools import assert_raises 4from paste.config import CONFIG, ConfigMiddleware 5from paste.fixture import TestApp 6import six 7 8test_key = 'test key' 9 10def reset_config(): 11 while True: 12 try: 13 CONFIG._pop_object() 14 except IndexError: 15 break 16 17def app_with_config(environ, start_response): 18 start_response('200 OK', [('Content-type','text/plain')]) 19 lines = ['Variable is: %s\n' % CONFIG[test_key], 20 'Variable is (in environ): %s' % environ['paste.config'][test_key]] 21 if six.PY3: 22 lines = [line.encode('utf8') for line in lines] 23 return lines 24 25class NestingAppWithConfig(object): 26 def __init__(self, app): 27 self.app = app 28 29 def __call__(self, environ, start_response): 30 response = self.app(environ, start_response) 31 assert isinstance(response, list) 32 supplement = ['Nesting variable is: %s' % CONFIG[test_key], 33 'Nesting variable is (in environ): %s' % \ 34 environ['paste.config'][test_key]] 35 if six.PY3: 36 supplement = [line.encode('utf8') for line in supplement] 37 response.extend(supplement) 38 return response 39 40def test_request_config(): 41 try: 42 config = {test_key: 'test value'} 43 app = ConfigMiddleware(app_with_config, config) 44 res = TestApp(app).get('/') 45 assert 'Variable is: test value' in res 46 assert 'Variable is (in environ): test value' in res 47 finally: 48 reset_config() 49 50def test_request_config_multi(): 51 try: 52 config = {test_key: 'test value'} 53 app = ConfigMiddleware(app_with_config, config) 54 config = {test_key: 'nesting value'} 55 app = ConfigMiddleware(NestingAppWithConfig(app), config) 56 res = TestApp(app).get('/') 57 assert 'Variable is: test value' in res 58 assert 'Variable is (in environ): test value' in res 59 assert 'Nesting variable is: nesting value' in res 60 print(res) 61 assert 'Nesting variable is (in environ): nesting value' in res 62 finally: 63 reset_config() 64 65def test_process_config(request_app=test_request_config): 66 try: 67 process_config = {test_key: 'bar', 'process_var': 'foo'} 68 CONFIG.push_process_config(process_config) 69 70 assert CONFIG[test_key] == 'bar' 71 assert CONFIG['process_var'] == 'foo' 72 73 request_app() 74 75 assert CONFIG[test_key] == 'bar' 76 assert CONFIG['process_var'] == 'foo' 77 CONFIG.pop_process_config() 78 79 assert_raises(AttributeError, lambda: 'process_var' not in CONFIG) 80 assert_raises(IndexError, CONFIG.pop_process_config) 81 finally: 82 reset_config() 83 84def test_process_config_multi(): 85 test_process_config(test_request_config_multi) 86