Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

much improved 404 handling

  • Loading branch information...
commit a39bc5b4c95fb50789eb868c51c76a5869618865 1 parent c775009
@peterbe authored
Showing with 29 additions and 0 deletions.
  1. +5 −0 app.py
  2. +13 −0 handlers.py
  3. +11 −0 tests/test_handlers.py
View
5 app.py
@@ -46,6 +46,11 @@ def __init__(self, database_name=None):
twitter_consumer_key=settings.TWITTER_CONSUMER_KEY,
twitter_consumer_secret=settings.TWITTER_CONSUMER_SECRET,
)
+ if 1 or not options.debug:
+ routed_handlers.append(
+ tornado.web.url('/.*?', handlers.PageNotFoundHandler,
+ name='page_not_found')
+ )
super(Application, self).__init__(routed_handlers, **app_settings)
self.redis = redis.client.Redis(settings.REDIS_HOST,
View
13 handlers.py
@@ -936,3 +936,16 @@ class LookupsJSONHandler(LookupsHandler):
def get(self):
data = self.get_lookups()
self.write_json(data)
+
+# this handler gets automatically appended last to all handlers inside app.py
+class PageNotFoundHandler(BaseHandler):
+
+ def get(self):
+ path = self.request.path
+ if not path.endswith('/'):
+ new_url = '%s/' % path
+ if self.request.query:
+ new_url += '?%s' % self.request.query
+ self.redirect(new_url)
+ return
+ raise HTTPError(404, path)
View
11 tests/test_handlers.py
@@ -830,6 +830,17 @@ def test_suggest_tweet(self):
#self.assertTrue(struct['text'].endswith('#toocool'))
self.assertTrue('@peterbestwin' in struct['text'])
+ def test_default_page_not_found(self):
+ url = '/does/not/exist'
+ response = self.client.get(url)
+ self.assertEqual(response.code, 302)
+ self.assertEqual(response.headers['location'], '/does/not/exist/')
+
+ url = '/does/not/exist/'
+ response = self.client.get(url)
+ self.assertEqual(response.code, 404)
+ self.assertTrue('restart your computer' in response.body)
+
def make_twitter_get_authenticated_user_callback(struct):
def twitter_get_authenticated_user(self, callback, **kw):
callback(struct)
Please sign in to comment.
Something went wrong with that request. Please try again.