Q: How to test session based scenarios (login, logout, @authenticated)?
A: Need to handle cookie: parse Set-Cookie response headers and provide Cookie request headers.
https://gist.github.com/wolf0403/5488165
Q: Motor (async Mongo client) timeouts in AsyncHTTPTestCase?
A: By default, test cases runs in separate IOLoops. This prevents Motor from performing correctly. Fix:
http://stackoverflow.com/questions/14688042/tornado-blocked-while-testing-asynchronous-method-using-asynchttptastcase
Q: How to do user login?
A: Return something inside get_current_user. This comes from Cookie (which forms "session").
class AuthMixin(object):
COOKIE_USER = 'user'
def set_user(self, u): # django: login(), not auth()
self.set_secure_cookie(self.COOKIE_USER, u)
def get_user(self):
return self.get_secure_cookie(self.COOKIE_USER)
def clear_user(self):
self.clear_cookie(self.COOKIE_USER)
class ResourceHandler(tornado.web.RequestHandler, AuthMixin):
def get_current_user(self):
return self.get_user()
Q: set_header (k, v); get_header (k) != v, why?
A: get_header always retrieves cookies from the request, while set_cookie sets to the response. Only way to verify is to check cookies brought back by the next request.