Skip to content

Commit

Permalink
Add test for response headers (#14074)
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann authored and jgraham committed Feb 28, 2019
1 parent 060e037 commit d4320a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions tools/webdriver/webdriver/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ class Response(object):
body has been read and parsed as appropriate.
"""

def __init__(self, status, body):
def __init__(self, status, body, headers):
self.status = status
self.body = body
self.headers = headers

def __repr__(self):
cls_name = self.__class__.__name__
Expand All @@ -39,11 +40,12 @@ def error(self):
def from_http(cls, http_response, decoder=json.JSONDecoder, **kwargs):
try:
body = json.load(http_response, cls=decoder, **kwargs)
headers = dict(http_response.getheaders())
except ValueError:
raise ValueError("Failed to decode response body as JSON:\n" +
http_response.read())

return cls(http_response.status, body)
return cls(http_response.status, body, headers)


class HTTPWireProtocol(object):
Expand Down
19 changes: 17 additions & 2 deletions webdriver/tests/support/asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ def assert_error(response, error_code):
assert response.body["value"]["error"] == error_code
assert isinstance(response.body["value"]["message"], basestring)
assert isinstance(response.body["value"]["stacktrace"], basestring)
assert_response_headers(response.headers)


def assert_success(response, value=None):
"""
Verify that the provided webdriver.Response instance described
a valid error response as defined by `dfn-send-an-error` and
the provided error code.
a valid success response as defined by `dfn-send-a-response` and
the provided response value.
:param response: ``webdriver.Response`` instance.
:param value: Expected value of the response body, if any.
Expand All @@ -67,9 +68,23 @@ def assert_success(response, value=None):

if value is not None:
assert response.body["value"] == value

assert_response_headers(response.headers)
return response.body.get("value")


def assert_response_headers(headers):
"""
Method to assert response headers for WebDriver requests
:param headers: dict with header data
"""
assert 'cache-control' in headers
assert 'no-cache' == headers['cache-control']
assert 'content-type' in headers
assert 'application/json; charset=utf-8' == headers['content-type']


def assert_dialog_handled(session, expected_text, expected_retval):
# If there were any existing dialogs prior to the creation of this
# fixture's dialog, then the "Get Alert Text" command will return
Expand Down

0 comments on commit d4320a3

Please sign in to comment.