Solidify transport interface #654
Solidify transport interface #654
Conversation
http, self.server.url + '/basic') | ||
|
||
assert response.status == http_client.OK | ||
print(response.headers.keys()) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
self.token = '{}{}'.format(self.token, '-new') | ||
|
||
|
||
class TransportComplianceTests(object): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
def test_connection_error(self): | ||
http = self.transport.get_http_object() | ||
|
||
with pytest.raises(Exception): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
def request(http, uri, method='GET', body=None, headers=None, | ||
redirections=httplib2.DEFAULT_MAX_REDIRECTS, | ||
**kwargs): | ||
class _ResponseWrapper(object): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
redirections = 3 | ||
|
||
def test_with_request_attr(self): | ||
def test_it(self): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
try: | ||
d = json.loads(_helpers._from_bytes(content)) | ||
d = json.loads(_helpers._from_bytes(response.data)) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
http, _GCE_METADATA_URI, headers=_GCE_HEADERS) | ||
return ( | ||
response.status == http_client.OK and | ||
response.get(_METADATA_FLAVOR_HEADER) == _DESIRED_METADATA_FLAVOR) | ||
response.headers.get( | ||
_METADATA_FLAVOR_HEADER) == _DESIRED_METADATA_FLAVOR) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
if 'error' in d: | ||
# you never know what those providers got to say | ||
error_msg = (str(d['error']) + | ||
str(d.get('error_description', ''))) | ||
else: | ||
error_msg = 'Invalid response: {0}.'.format(str(resp.status)) | ||
error_msg = 'Invalid response: {0}.'.format( | ||
str(response.status)) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
http = self.credentials.authorize(http) | ||
transport.request(http, 'http://example.com') | ||
authed_http = self.credentials.authorize(http) | ||
authed_http = self.credentials.authorize(http) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
return flask.jsonify({ | ||
'authorization': flask.request.headers.get('authorization'), | ||
'user_agent': flask.request.headers.get('user-agent') |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
class TransportComplianceTests(object): | ||
"""Test base class to confirm that a transport satisfies the interface | ||
expected by oauth2client.""" | ||
transport = None |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@pytest.fixture(autouse=True) | ||
def testserver(self): | ||
"""Provides a test HTTP server that is automatically created before | ||
a test and destoryed at the end. This server is running the test |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
response = self.transport._request( | ||
authed_http, self.server.url + '/authorized') | ||
|
||
content = json.loads(response.data.decode('utf-8')) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
content = json.loads(response.data.decode('utf-8')) | ||
|
||
assert credentials.token == 'token' | ||
assert content['authorization'] == 'Bearer {}'.format( |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, @dhermes.
try: | ||
d = json.loads(_helpers._from_bytes(content)) | ||
d = json.loads(_helpers._from_bytes(response.data)) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
http, _GCE_METADATA_URI, headers=_GCE_HEADERS) | ||
return ( | ||
response.status == http_client.OK and | ||
response.get(_METADATA_FLAVOR_HEADER) == _DESIRED_METADATA_FLAVOR) | ||
response.headers.get( | ||
_METADATA_FLAVOR_HEADER) == _DESIRED_METADATA_FLAVOR) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
if 'error' in d: | ||
# you never know what those providers got to say | ||
error_msg = (str(d['error']) + | ||
str(d.get('error_description', ''))) | ||
else: | ||
error_msg = 'Invalid response: {0}.'.format(str(resp.status)) | ||
error_msg = 'Invalid response: {0}.'.format( | ||
str(response.status)) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
"""Makes an HTTP request. | ||
|
||
Args: | ||
http_object: The transport-specific http object to be used to make |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
* headers: dict, the HTTP response headers. | ||
* data: bytes, the HTTP response body. | ||
""" | ||
return get_default_transport()._request( |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@pytest.fixture(autouse=True) | ||
def testserver(self): | ||
"""Provides a test HTTP server that is automatically created before | ||
a test and destoryed at the end. This server is running the test |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
http, self.server.url + '/basic') | ||
|
||
assert response.status == http_client.OK | ||
print(response.headers.keys()) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
def test_connection_error(self): | ||
http = self.transport.get_http_object() | ||
|
||
with pytest.raises(Exception): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
response = self.transport._request( | ||
authed_http, self.server.url + '/authorized') | ||
|
||
content = json.loads(response.data.decode('utf-8')) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
content = json.loads(response.data.decode('utf-8')) | ||
|
||
assert credentials.token == 'token' | ||
assert content['authorization'] == 'Bearer {}'.format( |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
http = self.credentials.authorize(http) | ||
transport.request(http, 'http://example.com') | ||
authed_http = self.credentials.authorize(http) | ||
# Double-wrap |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@dhermes it seems like the remainder of your concerns (save the one about exceptions) can be fixed by making |
Not sure. |
I'll let @nathanielmanistaatgoogle decide. |
@@ -29,6 +29,9 @@ | |||
_MAX_REFRESH_ATTEMPTS = 2 | |||
|
|||
|
|||
exceptions = (httplib2.HttpLib2Error,) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@nathanielmanistaatgoogle can you weigh in on the contentious topic on making |
@dhermes I've made request public again. I'm going to try to make the entire transport module private. |
These changes solidify the transport interface.
transport._request
private.transport._request
return value.transport.request
in tests in favor of usingauthed_http
directly, just as users would.Note about compliance tests:
This is the first test in the module to use pytest fixtures as well as use pytest's test & assertion styles. We eventually want to move toward pytest style tests (I plan to do so as I refactor each module for #597), so I'm hoping this won't be controversial. If it is, I'm happy to switch to unittest-style, but I'll be keeping the extremely useful pytest-localserver fixture.
(Also I'm starting to see the light at the end of the tunnel here)