diff options
author | Maximilian Hils <git@maximilianhils.com> | 2015-09-26 20:07:11 +0200 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2015-09-26 20:07:11 +0200 |
commit | 466888b01a361e46fb3d4e66afa2c6a0fd168c8e (patch) | |
tree | d7e6c6180b108318d76698883ddf17ae4cb704b0 /netlib/http/message.py | |
parent | 49ea8fc0ebcfe4861f099200044a553f092faec7 (diff) | |
download | mitmproxy-466888b01a361e46fb3d4e66afa2c6a0fd168c8e.tar.gz mitmproxy-466888b01a361e46fb3d4e66afa2c6a0fd168c8e.tar.bz2 mitmproxy-466888b01a361e46fb3d4e66afa2c6a0fd168c8e.zip |
improve request tests, coverage++
Diffstat (limited to 'netlib/http/message.py')
-rw-r--r-- | netlib/http/message.py | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/netlib/http/message.py b/netlib/http/message.py index ee138746..7cb18f52 100644 --- a/netlib/http/message.py +++ b/netlib/http/message.py @@ -9,7 +9,7 @@ from .. import encoding, utils CONTENT_MISSING = 0 -if six.PY2: +if six.PY2: # pragma: nocover _native = lambda x: x _always_bytes = lambda x: x else: @@ -110,15 +110,48 @@ class Message(object): def text(self, text): raise NotImplementedError() + def decode(self): + """ + Decodes body based on the current Content-Encoding header, then + removes the header. If there is no Content-Encoding header, no + action is taken. + + Returns: + True, if decoding succeeded. + False, otherwise. + """ + ce = self.headers.get("content-encoding") + data = encoding.decode(ce, self.content) + if data is None: + return False + self.content = data + self.headers.pop("content-encoding", None) + return True + + def encode(self, e): + """ + Encodes body with the encoding e, where e is "gzip", "deflate" or "identity". + + Returns: + True, if decoding succeeded. + False, otherwise. + """ + data = encoding.encode(e, self.content) + if data is None: + return False + self.content = data + self.headers["content-encoding"] = e + return True + # Legacy @property - def body(self): + def body(self): # pragma: nocover warnings.warn(".body is deprecated, use .content instead.", DeprecationWarning) return self.content @body.setter - def body(self, body): + def body(self, body): # pragma: nocover warnings.warn(".body is deprecated, use .content instead.", DeprecationWarning) self.content = body @@ -146,8 +179,7 @@ class decoded(object): def __enter__(self): if self.ce: - if not self.message.decode(): - self.ce = None + self.message.decode() def __exit__(self, type, value, tb): if self.ce: |