def test_basic(self):
     d = CaseInsensitiveDict()
     d['Content-Type'] = 'text/plain'
     
     self.assertEqual(d['content-type'], 'text/plain')
     self.assertEqual(d.items(), [('Content-Type', 'text/plain')])
     
     d['content-type'] = 'text/html'
     self.assertEqual(d['content-type'], 'text/html')
Exemple #2
0
    def test_basic(self):
        d = CaseInsensitiveDict()
        d['Content-Type'] = 'text/plain'

        self.assertEqual(d['content-type'], 'text/plain')
        self.assertEqual(d.items(), [('Content-Type', 'text/plain')])

        d['content-type'] = 'text/html'
        self.assertEqual(d['content-type'], 'text/html')
 def __init__(self, request=None, **kw):
     self.headers = CaseInsensitiveDict()
     
     self.status = self.defaults.status
     self.mime = self.defaults.mime
     self.encoding = self.defaults.encoding
     
     self.body = None
     
     if hasattr(request, 'environ'):
         self.environ = request.environ
         self.request = request
     
     else:
         self.environ = request
         self.request = None
     
     for name in kw:
         if not hasattr(self, name):
             raise AttributeError('Unknown attribute %r.' % (name, ))
         setattr(self, name, kw[name])
     
     super(Response, self).__init__()
class Response(object):
    """A WSGI application representing a standard response.
    
    This conforms to the WSGI 2.0 draft specification.
    
    For more information, see:
        http://wsgi.org/wsgi/WSGI_2.0
    
    """
    
    status = Status()
    # body = RequestBody()
    conditional = False
    
    defaults = Bunch(status=200, mime=b'text/html', encoding=b'utf8')
    
    mime = ContentType(b'Content-Type')
    encoding = Charset(b'Content-Type')
    disposition = ReaderWriter(b'Content-Disposition', rfc='14.11')
    pragma = ReaderWriter(b'Pragma', rfc='14.32')
    server = ReaderWriter(b'Server', rfc='14.38')
    
    cookies = []
    
    location = ReaderWriter(b'Location')
    language = ReaderWriter(b'Content-Language')
    
    # date = Date(b'Date', rfc='14.18')
    age = Int(b'Age', rfc='14.6')
    # cache = CacheControl(b'Cache-Control', rfc='14.9')
    # expires = Date(b'Expires', rfc='14.21')
    # modified = Date(b'Last-Modified', rfc='14.29')
    # etag = ETag(b'ETag', rfc='14.19')
    # retry = TimeDelta(b'Retry-After', rfc='14.37')
    
    #allow = List(b'Allow', rfc='14.7')
    #vary = List(b'Vary', rfc='14.44')
    
    # language = List(b'Content-Language', rfc='14.12')
    location = ReaderWriter(b'Content-Location', rfc='14.14')
    hash = ContentMD5(b'Content-MD5', rfc='14.16')
    # ranges = AcceptRanges(b'Accept-Ranges', rfc='14.16')
    # range = ContentRange(b'Content-Range', rfc='14.16')
    length = ContentLength(b'Content-Length', rfc='14.17')
    
    def __init__(self, request=None, **kw):
        self.headers = CaseInsensitiveDict()
        
        self.status = self.defaults.status
        self.mime = self.defaults.mime
        self.encoding = self.defaults.encoding
        
        self.body = None
        
        if hasattr(request, 'environ'):
            self.environ = request.environ
            self.request = request
        
        else:
            self.environ = request
            self.request = None
        
        for name in kw:
            if not hasattr(self, name):
                raise AttributeError('Unknown attribute %r.' % (name, ))
            setattr(self, name, kw[name])
        
        super(Response, self).__init__()
    
    def __repr__(self):
        return '<%s at 0x%x %s>' % (
            self.__class__.__name__,
            abs(id(self)),
            self.status)
    
    def __getitem__(self, name):
        return self.headers[name]
    
    def __setitem__(self, name, value):
        self.headers[name] = value
    
    def __delitem__(self, name):
        del self.headers[name]
    
    _final = False
    
    @property
    def final(self):
        """I'm the 'x' property."""
        return self._final
    
    @final.setter
    def final(self, value):
        self._final = self._final or bool(value)
    
    @final.deleter
    def final(self):
        return
    
    @property
    def wsgi(self):
        body = self.body
        
        if isinstance(body, binary): body = [body]
        elif isinstance(body, unicode): body = [body.encode(self.encoding)]
        elif isinstance(body, IO):
            def generator(body):
                data = body.read(1024)
                while data:
                    yield data
                    data = body.read(1024)
            
            body = generator(body)
        
        return WSGIData(unicode(self.status).encode('ascii'), [(n, v) for n, v in self.headers.items()], body)
    
    def __call__(self, environ=None, start_response=None):
        """Process the headers and content body and return a 3-tuple of status, header list, and iterable body.
        
        Alternatively, pass the status and header list to the provided start_response callable and return an iterable body.
        
        start_response is included to support WSGI 1, and is not required for WSGI 2.
        """
        
        if environ is None:
            environ = self.environ
        
        status, headers, body = self.wsgi
        
        # We support WSGI 1.0.
        if hasattr(start_response, '__call__'):
            start_response(status, headers)
            return body
        
        return status, headers, body
Exemple #5
0
 def test_keys(self):
     d = CaseInsensitiveDict({None: 2})
     self.assertEqual(d, {None: 2})
     self.assertEqual(d[None], 2)
Exemple #6
0
 def test_default(self):
     d = CaseInsensitiveDict({'foo': 'bar'})