Commits

Andriy Kornatskyy committed 038ab2f

Added support for gzip content encoding.

  • Participants
  • Parent commits 3420a59

Comments (0)

Files changed (3)

File doc/userguide.rst

     >>>> str(project.title)
     Buildbot
 
+Supports: HTTP GET/HEAD/POST verbs, follows redirects, handles cookies between
+requests, gzip content encoding.
+
 i18n
 ----
 

File src/wheezy/core/httpclient.py

 from wheezy.core.comp import urljoin
 from wheezy.core.comp import urlparse
 from wheezy.core.comp import urlsplit
+from wheezy.core.gzip import decompress
 
 
 class HTTPClient(object):
         r = urlparse(url)
         self.connection = HTTPConnection(r[1])  # netloc
         self.default_headers = headers and headers or {}
+        self.default_headers['Accept-Encoding'] = 'gzip'
         self.path = r[2]  # path
         self.method = None
         self.headers = None
         self.cookies = {}
         self.status_code = 0
-        self.content = None
+        self.body = None
+        self.__content = None
         self.__json = None
 
     @property
+    def content(self):
+        """ Returns a content of the response.
+        """
+        if self.__content is None:
+            self.__content = self.body.decode('utf-8')
+        return self.__content
+
+    @property
     def json(self):
         """ Returns a json response.
         """
                 headers['Content-Type'] = 'application/x-www-form-urlencoded'
 
         self.status_code = 0
-        self.content = None
+        self.body = None
+        self.__content = None
         self.__json = None
 
         self.connection.connect()
         self.connection.request(method, path, body, headers)
         r = self.connection.getresponse()
-        self.content = r.read().decode('utf-8')
+        self.body = r.read()
         self.connection.close()
 
         self.status_code = r.status
         self.headers = defaultdict(list)
         for name, value in r.getheaders():
             self.headers[name].append(value)
+        if 'gzip' in self.headers['content-encoding']:
+            self.body = decompress(self.body)
         for cookie_string in self.headers['set-cookie']:
             cookies = SimpleCookie(cookie_string)
             for name in cookies:

File src/wheezy/core/tests/test_httpclient.py

         assert '/api/v1/auth/token' == path
         assert '' == body
         assert self.client.default_headers == headers
+        assert 'Accept-Encoding' in headers
 
     def test_ajax_get(self):
         self.client.ajax_get('auth/token')
         self.client.get('auth/token')
         assert {} == self.client.json
         patcher.stop()
+
+    def test_gzip(self):
+        """ Ensure gzip decompression.
+        """
+        from wheezy.core.comp import ntob
+        from wheezy.core.gzip import compress
+        self.headers.append(('content-encoding', 'gzip'))
+        self.mock_response.read.return_value = compress(ntob('test', 'utf-8'))
+        self.client.get('auth/token')
+        assert 'test' == self.client.content