# HG changeset patch # User Adrien Di Mascio # Date 1302783561 -7200 # Node ID e5833657c646e189d54396e866e3ae5b820c07c5 # Parent eb0f5f46138f2cd0c6475176e71c3c726ebefa33 [testlib] make a clear distinction between input / output HTTP headers The get_header() / set_header() API is the same as for CubicWebRequestBase : - get_header() returns the **request** header - set_header() / add_header() works on the **response** header FakeRequest adds two additional methods : - get_response_header() returns the **reponse** header - set_request_header() adds a **request** header FakeRequest now uses the `headers_out` ineherited from CubicWebRequestBase. It also uses a `_headers_in` attribute diff -r eb0f5f46138f -r e5833657c646 devtools/fake.py --- a/devtools/fake.py Wed Apr 13 14:02:04 2011 +0200 +++ b/devtools/fake.py Thu Apr 14 14:19:21 2011 +0200 @@ -25,6 +25,8 @@ from cubicweb.req import RequestSessionBase from cubicweb.cwvreg import CubicWebVRegistry from cubicweb.web.request import CubicWebRequestBase +from cubicweb.web.http_headers import Headers + from cubicweb.devtools import BASE_URL, BaseApptestConfiguration @@ -59,8 +61,14 @@ self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop') super(FakeRequest, self).__init__(*args, **kwargs) self._session_data = {} - self._headers = {} + self._headers_in = Headers() + def set_cookie(self, cookie, key, maxage=300, expires=None): + super(FakeRequest, self).set_cookie(cookie, key, maxage=300, expires=None) + cookie = self.get_response_header('Set-Cookie') + self._headers_in.setHeader('Cookie', cookie) + + ## Implement request abstract API def header_accept_language(self): """returns an ordered list of preferred languages""" return ('en',) @@ -81,48 +89,32 @@ return url return url.split('?', 1)[0] - def set_content_type(self, content_type, filename=None, encoding=None): - """set output content type for this request. An optional filename - may be given + def get_header(self, header, default=None, raw=True): + """return the value associated with the given input header, raise + KeyError if the header is not set """ - pass - - def set_header(self, header, value, raw=True): - """set an output HTTP header""" - self._headers[header] = value + if raw: + return self._headers_in.getRawHeaders(header, [default])[0] + return self._headers_in.getHeader(header, default) - def add_header(self, header, value): - """set an output HTTP header""" - self._headers[header] = value # XXX + ## extend request API to control headers in / out values + def set_request_header(self, header, value, raw=False): + """set an input HTTP header""" + if isinstance(value, basestring): + value = [value] + if raw: + self._headers_in.setRawHeaders(header, value) + else: + self._headers_in.setHeader(header, value) - def remove_header(self, header): - """remove an output HTTP header""" - self._headers.pop(header, None) - - def get_header(self, header, default=None): + def get_response_header(self, header, default=None, raw=False): """return the value associated with the given input header, raise KeyError if the header is not set """ - return self._headers.get(header, default) - - def set_cookie(self, cookie, key, maxage=300, expires=None): - """set / update a cookie key - - by default, cookie will be available for the next 5 minutes - """ - morsel = cookie[key] - if maxage is not None: - morsel['Max-Age'] = maxage - if expires: - morsel['expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S %z') - # make sure cookie is set on the correct path - morsel['path'] = self.base_url_path() - self.add_header('Set-Cookie', morsel.OutputString()) - self.add_header('Cookie', morsel.OutputString()) - - def remove_cookie(self, cookie, key): - self.remove_header('Set-Cookie') - self.remove_header('Cookie') + if raw: + return self.headers_out.getRawHeaders(header, default) + else: + return self.headers_out.getHeader(header, default) def validate_cache(self): pass diff -r eb0f5f46138f -r e5833657c646 web/test/unittest_application.py --- a/web/test/unittest_application.py Wed Apr 13 14:02:04 2011 +0200 +++ b/web/test/unittest_application.py Thu Apr 14 14:19:21 2011 +0200 @@ -334,7 +334,7 @@ self.assertRaises(AuthenticationError, self.app_publish, req, 'login') self.assertEqual(req.cnx, None) authstr = base64.encodestring('%s:%s' % (self.admlogin, self.admpassword)) - req._headers['Authorization'] = 'basic %s' % authstr + req.set_request_header('Authorization', 'basic %s' % authstr) self.assertAuthSuccess(req, origsession) self.assertRaises(LogOut, self.app_publish, req, 'logout') self.assertEqual(len(self.open_sessions), 0) @@ -378,7 +378,8 @@ cookie = Cookie.SimpleCookie() sessioncookie = self.app.session_handler.session_cookie(req) cookie[sessioncookie] = req.session.sessionid - req._headers['Cookie'] = cookie[sessioncookie].OutputString() + req.set_request_header('Cookie', cookie[sessioncookie].OutputString(), + raw=True) clear_cache(req, 'get_authorization') # reset session as if it was a new incoming request req.session = req.cnx = None @@ -403,10 +404,10 @@ req, origsession = self.init_authentication('http', 'anon') self._test_auth_anon(req) authstr = base64.encodestring('toto:pouet') - req._headers['Authorization'] = 'basic %s' % authstr + req.set_request_header('Authorization', 'basic %s' % authstr) self._test_anon_auth_fail(req) authstr = base64.encodestring('%s:%s' % (self.admlogin, self.admpassword)) - req._headers['Authorization'] = 'basic %s' % authstr + req.set_request_header('Authorization', 'basic %s' % authstr) self.assertAuthSuccess(req, origsession) self.assertRaises(LogOut, self.app_publish, req, 'logout') self.assertEqual(len(self.open_sessions), 0)