import logging from pylons import request, response, session, tmpl_context as c from pylons.controllers.util import abort, redirect_to from samplebundle.lib.base import BaseController, render log = logging.getLogger(__name__) class CookiesController(BaseController): def get_cookie(self): my_cookie = request.cookies.get("my_cookie_name") if my_cookie == "my_cookie_value": return 'Found a cookie named my_cookie_name' else: return 'Nothing found' def set_cookie(self): response.set_cookie('my_cookie_name', 'my_cookie_value', max_age=360) return 'Done' def delete_cookie(self): response.delete_cookie('my_cookie_name') return 'Done'
unit testing code:
from samplebundle.tests import * class TestCookiesController(TestController): def test_get_cookie(self): myheaders = {} myheaders['Cookie'] = 'my_cookie_name=my_cookie_value' response = self.app.get(url(controller='cookies', action='get_cookie'), headers = myheaders) assert "Found a cookie" in response def test_set_cookie(self): response = self.app.get(url(controller='cookies', action='set_cookie')) assert 'my_cookie_name=my_cookie_value' in response.headers['Set-Cookie'] assert 'Max-Age=360' in response.headers['Set-Cookie'] """Cookie storage format in headers['Set-Cookie'] 'key=value; Domain=example.org; expires="Mon, 28-Feb-2011 07:48:56 GMT"; Max-Age=360; Path=/; secure' """ def test_delete_cookie(self): myheaders = {} myheaders['Cookie'] = 'my_cookie_name=my_cookie_value' response = self.app.get(url(controller='cookies', action='delete_cookie'), headers = myheaders) assert 'my_cookie_name=' in response.headers['Set-Cookie']
No comments:
Post a Comment