Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Commit

Permalink
writing test for session and APIHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
whtsky committed May 12, 2013
1 parent 650417f commit 75e6f80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
27 changes: 24 additions & 3 deletions waterspout/tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@ def post(self):

class APIHandler(APIHandler):
def post(self):
self.write('success')
self.write({'name': 'whtsky'})


class SessionHandler(RequestHandler):
def get(self):
self.session["name"] = "whtsky"
self.session.miao = "wang"

assert not self.session.a
assert not self.session["b"]

handlers = [
('/', HelloWorldHandler),
('/post', PostHandler),
('/api', APIHandler)
('/api', APIHandler),
('/session', SessionHandler)
]

application = Application(__name__, handlers=handlers)
application = Application(__name__, handlers=handlers, cookie_secret="..")


def test_test():
Expand All @@ -37,6 +46,11 @@ def test_test():
assert body == to_unicode(body)


def test_session():
client = application.TestClient()
assert client.get('/session').code == 200


def test_route():
client = application.TestClient()
assert client.get('/').body == 'Hello World'
Expand All @@ -55,3 +69,10 @@ def test_csrf():
client = application.TestClient()
assert client.post('/post', body='..').code == 403
assert client.post('/api', body='..').code == 200


def test_api():
client = application.TestClient()
assert client.post('/api', body='..').body == '{"name": "whtsky"}'
assert client.post('/api?callback=note', body='..').body == \
'note({"name": "whtsky"});'
14 changes: 10 additions & 4 deletions waterspout/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ def post(self):
"""
if not hasattr(self, '_session'):
class Session(object):
def __getattr__(s, name):
def __getitem__(_, name):
return self.get_secure_cookie(name)

def __setattr__(s, name, value):
return self.set_secure_cookie(name, value)
def __getattr__(self, name):
return self[name]

def __setitem__(_, key, value):
self.set_secure_cookie(key, value)

def __setattr__(self, name, value):
self[name] = value
self._session = Session()

return self._session
Expand Down Expand Up @@ -138,7 +144,7 @@ def write(self, chunk, callback=None):
if callback is None:
callback = self.get_argument('callback', None)
if callback:
chunk = "%s(%s)" % (callback, tornado.escape.to_unicode(chunk))
chunk = "%s(%s);" % (callback, tornado.escape.to_unicode(chunk))
self.set_header("Content-Type",
"application/javascript; charset=UTF-8")
else:
Expand Down

0 comments on commit 75e6f80

Please sign in to comment.