devtools/fake.py
changeset 8314 cfd6ab461849
parent 8276 57d8f3b3e165
child 8315 166e6d5d8e17
equal deleted inserted replaced
8313:386b6313de28 8314:cfd6ab461849
    62             kwargs['vreg'] = CWRegistryStore(FakeConfig(), initlog=False)
    62             kwargs['vreg'] = CWRegistryStore(FakeConfig(), initlog=False)
    63         kwargs['https'] = False
    63         kwargs['https'] = False
    64         self._url = kwargs.pop('url', None) or 'view?rql=Blop&vid=blop'
    64         self._url = kwargs.pop('url', None) or 'view?rql=Blop&vid=blop'
    65         super(FakeRequest, self).__init__(*args, **kwargs)
    65         super(FakeRequest, self).__init__(*args, **kwargs)
    66         self._session_data = {}
    66         self._session_data = {}
    67         self._headers_in = Headers()
       
    68 
    67 
    69     def set_cookie(self, name, value, maxage=300, expires=None, secure=False):
    68     def set_cookie(self, name, value, maxage=300, expires=None, secure=False):
    70         super(FakeRequest, self).set_cookie(name, value, maxage, expires, secure)
    69         super(FakeRequest, self).set_cookie(name, value, maxage, expires, secure)
    71         cookie = self.get_response_header('Set-Cookie')
    70         cookie = self.get_response_header('Set-Cookie')
    72         self._headers_in.setHeader('Cookie', cookie)
    71         self._headers_in.setHeader('Cookie', cookie)
    90             url = self._url
    89             url = self._url
    91         if includeparams:
    90         if includeparams:
    92             return url
    91             return url
    93         return url.split('?', 1)[0]
    92         return url.split('?', 1)[0]
    94 
    93 
    95     def get_header(self, header, default=None, raw=True):
       
    96         """return the value associated with the given input header, raise
       
    97         KeyError if the header is not set
       
    98         """
       
    99         if raw:
       
   100             return self._headers_in.getRawHeaders(header, [default])[0]
       
   101         return self._headers_in.getHeader(header, default)
       
   102 
       
   103     ## extend request API to control headers in / out values
       
   104     def set_request_header(self, header, value, raw=False):
    94     def set_request_header(self, header, value, raw=False):
   105         """set an input HTTP header"""
    95         """set an incoming HTTP header (For test purpose only)"""
   106         if isinstance(value, basestring):
    96         if isinstance(value, basestring):
   107             value = [value]
    97             value = [value]
   108         if raw:
    98         if raw: #
       
    99             # adding encoded header is important, else page content
       
   100             # will be reconverted back to unicode and apart unefficiency, this
       
   101             # may cause decoding problem (e.g. when downloading a file)
   109             self._headers_in.setRawHeaders(header, value)
   102             self._headers_in.setRawHeaders(header, value)
   110         else:
   103         else: #
   111             self._headers_in.setHeader(header, value)
   104             self._headers_in.setHeader(header, value) #
   112 
   105 
   113     def get_response_header(self, header, default=None, raw=False):
   106     def get_response_header(self, header, default=None, raw=False):
   114         """return the value associated with the given input header,
   107         """return output header (For test purpose only"""
   115         raise KeyError if the header is not set
   108         if raw: #
   116         """
   109             return self.headers_out.getRawHeaders(header, [default])[0]
   117         if raw:
   110         return self.headers_out.getHeader(header, default)
   118             return self.headers_out.getRawHeaders(header, default)[0]
       
   119         else:
       
   120             return self.headers_out.getHeader(header, default)
       
   121 
   111 
   122     def validate_cache(self):
   112     def validate_cache(self):
   123         pass
   113         pass
   124 
   114 
   125     def build_url_params(self, **kwargs):
   115     def build_url_params(self, **kwargs):