Tornado AsyncHTTPTestCase gotchas

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值