Skip to content

Commit 069ac06

Browse files
chore: support string type response.data for gcloud (#503)
1 parent 4117674 commit 069ac06

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

packages/google-auth/google/auth/impersonated_credentials.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,17 @@ def _make_iam_token_request(request, principal, headers, body):
8888

8989
response = request(url=iam_endpoint, method="POST", headers=headers, body=body)
9090

91-
response_body = response.data.decode("utf-8")
91+
response_body = (
92+
response.data.decode("utf-8")
93+
if hasattr(response.data, "decode")
94+
else response.data
95+
)
9296

9397
if response.status != http_client.OK:
9498
exceptions.RefreshError(_REFRESH_ERROR, response_body)
9599

96100
try:
97-
token_response = json.loads(response.data.decode("utf-8"))
101+
token_response = json.loads(response_body)
98102
token = token_response["accessToken"]
99103
expiry = datetime.strptime(token_response["expireTime"], "%Y-%m-%dT%H:%M:%SZ")
100104

packages/google-auth/tests/test_impersonated_credentials.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,17 @@ def test_default_state(self):
132132
assert not credentials.valid
133133
assert credentials.expired
134134

135-
def make_request(self, data, status=http_client.OK, headers=None, side_effect=None):
135+
def make_request(
136+
self,
137+
data,
138+
status=http_client.OK,
139+
headers=None,
140+
side_effect=None,
141+
use_data_bytes=True,
142+
):
136143
response = mock.create_autospec(transport.Response, instance=False)
137144
response.status = status
138-
response.data = _helpers.to_bytes(data)
145+
response.data = _helpers.to_bytes(data) if use_data_bytes else data
139146
response.headers = headers or {}
140147

141148
request = mock.create_autospec(transport.Request, instance=False)
@@ -144,7 +151,8 @@ def make_request(self, data, status=http_client.OK, headers=None, side_effect=No
144151

145152
return request
146153

147-
def test_refresh_success(self, mock_donor_credentials):
154+
@pytest.mark.parametrize("use_data_bytes", [True, False])
155+
def test_refresh_success(self, use_data_bytes, mock_donor_credentials):
148156
credentials = self.make_credentials(lifetime=None)
149157
token = "token"
150158

@@ -154,7 +162,9 @@ def test_refresh_success(self, mock_donor_credentials):
154162
response_body = {"accessToken": token, "expireTime": expire_time}
155163

156164
request = self.make_request(
157-
data=json.dumps(response_body), status=http_client.OK
165+
data=json.dumps(response_body),
166+
status=http_client.OK,
167+
use_data_bytes=use_data_bytes,
158168
)
159169

160170
credentials.refresh(request)

0 commit comments

Comments
 (0)