Bug 1411045 - Prettify error in wdclient.Response. r?maja_zf
When showing wdclient.Response's object representation, prettify
the error if there is one. Otherwise include the full body as before.
MozReview-Commit-ID: 64QpsB89Oiw
--- a/testing/web-platform/tests/tools/webdriver/webdriver/transport.py
+++ b/testing/web-platform/tests/tools/webdriver/webdriver/transport.py
@@ -1,24 +1,36 @@
import httplib
import json
import urlparse
import error
+
class Response(object):
- """Describes an HTTP response received from a remote en"Describes an HTTP
- response received from a remote end whose body has been read and parsed as
- appropriate."""
+ """
+ Describes an HTTP response received from a remote end whose
+ body has been read and parsed as appropriate.
+ """
+
def __init__(self, status, body):
self.status = status
self.body = body
def __repr__(self):
- return "wdclient.Response(status=%d, body=%s)" % (self.status, self.body)
+ cls_name = self.__class__.__name__
+ if self.error:
+ return "<%s status=%s error=%s>" % (cls_name, self.status, repr(self.error))
+ return "<% status=%s body=%s>" % (cls_name, self.status, self.body)
+
+ @property
+ def error(self):
+ if self.status != 200:
+ return error.from_response(self)
+ return None
@classmethod
def from_http_response(cls, http_response):
status = http_response.status
body = http_response.read()
# SpecID: dfn-send-a-response
#
@@ -40,48 +52,49 @@ class Response(object):
class ToJsonEncoder(json.JSONEncoder):
def default(self, obj):
return getattr(obj.__class__, "json", json.JSONEncoder().default)(obj)
class HTTPWireProtocol(object):
- """Transports messages (commands and responses) over the WebDriver
+ """
+ Transports messages (commands and responses) over the WebDriver
wire protocol.
"""
def __init__(self, host, port, url_prefix="/", timeout=None):
- """Construct interface for communicating with the remote server.
+ """
+ Construct interface for communicating with the remote server.
:param url: URL of remote WebDriver server.
:param wait: Duration to wait for remote to appear.
"""
-
self.host = host
self.port = port
self.url_prefix = url_prefix
self._timeout = timeout
def url(self, suffix):
return urlparse.urljoin(self.url_prefix, suffix)
def send(self, method, uri, body=None, headers=None):
- """Send a command to the remote.
+ """
+ Send a command to the remote.
:param method: `GET`, `POST`, or `DELETE`.
:param uri: Relative endpoint of the requests URL path.
:param body: Body of the request. Defaults to an empty
dictionary if ``method`` is `POST`.
:param headers: Additional headers to include in the request.
:return: Instance of ``wdclient.Response`` describing the
HTTP response received from the remote end.
-
"""
if body is None and method == "POST":
body = {}
if isinstance(body, dict):
body = json.dumps(body, cls=ToJsonEncoder)
if isinstance(body, unicode):