def test_invalid_keys(self): """ All the builtin backends (except memcached, see below) should warn on keys that would be refused by memcached. This encourages portable caching code without making it too difficult to use production backends with more liberal key rules. Refs #6447. """ # mimic custom ``make_key`` method being defined since the default will # never show the below warnings def func(key, *args): return key old_func = self.cache.key_func self.cache.key_func = func # On Python 2.6+ we could use the catch_warnings context # manager to test this warning nicely. Since we can't do that # yet, the cleanest option is to temporarily ask for # CacheKeyWarning to be raised as an exception. _warnings_state = get_warnings_state() warnings.simplefilter("error", CacheKeyWarning) try: # memcached does not allow whitespace or control characters in keys self.assertRaises(CacheKeyWarning, self.cache.set, 'key with spaces', 'value') # memcached limits key length to 250 self.assertRaises(CacheKeyWarning, self.cache.set, 'a' * 251, 'value') finally: restore_warnings_state(_warnings_state) self.cache.key_func = old_func
def test_load_overlong_key(self): warnings_state = get_warnings_state() warnings.filterwarnings('ignore', category=CacheKeyWarning) self.session._session_key = (string.ascii_letters + string.digits) * 20 self.assertEqual(self.session.load(), {}) restore_warnings_state(warnings_state)
def test_raw_post_data_returns_body(self): """ HttpRequest.raw_post_body should be the same as HttpRequest.body """ payload = 'Hello There!' request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(payload), 'wsgi.input': StringIO(payload) }) warnings_state = get_warnings_state() warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.http') try: self.assertEqual(request.body, request.raw_post_data) finally: restore_warnings_state(warnings_state)
def test_filter_added(self): """ Test that debug-false filter is added to mail_admins handler if it has no filters. """ config = copy.deepcopy(OLD_LOGGING) warnings_state = get_warnings_state() warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.conf') try: compat_patch_logging_config(config) finally: restore_warnings_state(warnings_state) self.assertEqual( config["handlers"]["mail_admins"]["filters"], ['require_debug_false'])
def test_POST_connection_error(self): """ If wsgi.input.read() raises an exception while trying to read() the POST, the exception should be identifiable (not a generic IOError). """ class ExplodingStringIO(StringIO): def read(self, len=0): raise IOError("kaboom!") payload = 'name=value' request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(payload), 'wsgi.input': ExplodingStringIO(payload)}) warnings_state = get_warnings_state() warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.http') try: with self.assertRaises(UnreadablePostError): request.raw_post_data finally: restore_warnings_state(warnings_state)
def test_invalid_keys(self): """ All the builtin backends (except memcached, see below) should warn on keys that would be refused by memcached. This encourages portable caching code without making it too difficult to use production backends with more liberal key rules. Refs #6447. """ # On Python 2.6+ we could use the catch_warnings context # manager to test this warning nicely. Since we can't do that # yet, the cleanest option is to temporarily ask for # CacheKeyWarning to be raised as an exception. _warnings_state = get_warnings_state() warnings.simplefilter("error", CacheKeyWarning) try: # memcached does not allow whitespace or control characters in keys self.assertRaises(CacheKeyWarning, self.cache.set, 'key with spaces', 'value') # memcached limits key length to 250 self.assertRaises(CacheKeyWarning, self.cache.set, 'a' * 251, 'value') finally: restore_warnings_state(_warnings_state)
def restore_warnings_state(self): restore_warnings_state(self._warnings_state)
def tearDown(self): restore_warnings_state(self._warnings_state)
def tearDown(self): restore_warnings_state(self.warning_state) settings.SETTINGS_MODULE = self.old_settings_module settings.LOCALE_PATHS = self.old_locale_paths
def tearDown(self): settings.INSTALLED_APPS = self._old_installed_apps restore_warnings_state(self._warnings_state)
def restore_warnings_state(self): """ Restores the state of the warnings module to the state saved by save_warnings_state() """ restore_warnings_state(self._warnings_state)
def restore_warnings_state(self): """ Restores the sate of the warnings module to the state saved by save_warnings_state() """ restore_warnings_state(self._warnings_state)
def tearDown(self): from django.test.utils import restore_warnings_state settings.STATIC_URL = self.old_static_url settings.MEDIA_URL = self.old_media_url restore_warnings_state(self._warnings_state)