Skip to content

Commit

Permalink
Fix the signature of AuthorizedHttp.request to match the signature of…
Browse files Browse the repository at this point in the history
… the request in httplib2 (#13)

* Fix the signature of AuthorizedHttp.request to match the signature of request in httplib2
  • Loading branch information
liuchaoren authored Sep 14, 2020
1 parent 09fa5a9 commit 2edc00b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
7 changes: 6 additions & 1 deletion google_auth_httplib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ def __init__(self, credentials, http=None,
self._request = Request(self.http)

def request(self, uri, method='GET', body=None, headers=None,
redirections=httplib2.DEFAULT_MAX_REDIRECTS,
connection_type=None,
**kwargs):
"""Implementation of httplib2's Http.request."""

Expand All @@ -198,7 +200,9 @@ def request(self, uri, method='GET', body=None, headers=None,

# Make the request.
response, content = self.http.request(
uri, method, body=body, headers=request_headers, **kwargs)
uri, method, body=body, headers=request_headers,
redirections=redirections, connection_type=connection_type,
**kwargs)

# If the response indicated that the credentials needed to be
# refreshed, then refresh the credentials and re-attempt the
Expand All @@ -222,6 +226,7 @@ def request(self, uri, method='GET', body=None, headers=None,
# Recurse. Pass in the original headers, not our modified set.
return self.request(
uri, method, body=body, headers=headers,
redirections=redirections, connection_type=connection_type,
_credential_refresh_attempt=_credential_refresh_attempt + 1,
**kwargs)

Expand Down
45 changes: 37 additions & 8 deletions tests/test_google_auth_httplib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def __init__(self, responses, headers=None):
self.headers = headers or {}
self.add_certificate = mock.Mock(return_value=None)

def request(self, url, method='GET', body=None, headers=None, **kwargs):
self.requests.append((method, url, body, headers, kwargs))
def request(self, url, method='GET', body=None, headers=None,
redirections=httplib2.DEFAULT_MAX_REDIRECTS,
connection_type=None):
self.requests.append(
(method, url, body, headers, redirections, connection_type))
return self.responses.pop(0)


Expand All @@ -55,7 +58,7 @@ def test_timeout(self):
request(url=url, method='GET', timeout=5)

assert http.requests[0] == (
'GET', url, None, None, {})
'GET', url, None, None, httplib2.DEFAULT_MAX_REDIRECTS, None)


def test__make_default_http():
Expand Down Expand Up @@ -155,7 +158,8 @@ def test_request_no_refresh(self):
assert mock_credentials.before_request.called
assert not mock_credentials.refresh.called
assert mock_http.requests == [
('GET', self.TEST_URL, None, {'authorization': 'token'}, {})]
('GET', self.TEST_URL, None, {'authorization': 'token'},
httplib2.DEFAULT_MAX_REDIRECTS, None)]

def test_request_refresh(self):
mock_credentials = mock.Mock(wraps=MockCredentials())
Expand All @@ -175,8 +179,10 @@ def test_request_refresh(self):
assert mock_credentials.before_request.call_count == 2
assert mock_credentials.refresh.called
assert mock_http.requests == [
('GET', self.TEST_URL, None, {'authorization': 'token'}, {}),
('GET', self.TEST_URL, None, {'authorization': 'token1'}, {})]
('GET', self.TEST_URL, None, {'authorization': 'token'},
httplib2.DEFAULT_MAX_REDIRECTS, None),
('GET', self.TEST_URL, None, {'authorization': 'token1'},
httplib2.DEFAULT_MAX_REDIRECTS, None)]

def test_request_stream_body(self):
mock_credentials = mock.Mock(wraps=MockCredentials())
Expand All @@ -198,5 +204,28 @@ def test_request_stream_body(self):
assert response == mock_response
assert data == mock_response.data
assert mock_http.requests == [
('POST', self.TEST_URL, body, {'authorization': 'token'}, {}),
('POST', self.TEST_URL, body, {'authorization': 'token1'}, {})]
('POST', self.TEST_URL, body, {'authorization': 'token'},
httplib2.DEFAULT_MAX_REDIRECTS, None),
('POST', self.TEST_URL, body, {'authorization': 'token1'},
httplib2.DEFAULT_MAX_REDIRECTS, None)]

def test_request_positional_args(self):
"""Verifies that clients can pass args to request as positioanls."""
mock_credentials = mock.Mock(wraps=MockCredentials())
mock_response = MockResponse()
mock_http = MockHttp([mock_response])

authed_http = google_auth_httplib2.AuthorizedHttp(
mock_credentials, http=mock_http)

response, data = authed_http.request(
self.TEST_URL, 'GET', None, None,
httplib2.DEFAULT_MAX_REDIRECTS, None)

assert response == mock_response
assert data == mock_response.data
assert mock_credentials.before_request.called
assert not mock_credentials.refresh.called
assert mock_http.requests == [
('GET', self.TEST_URL, None, {'authorization': 'token'},
httplib2.DEFAULT_MAX_REDIRECTS, None)]

0 comments on commit 2edc00b

Please sign in to comment.