From 12299c3d6366aa779bf022b2b6cf281c0e65b170 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 19 Oct 2021 17:19:39 -0700 Subject: [PATCH 1/3] add test to repro --- .../azure-core/tests/test_rest_http_request.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sdk/core/azure-core/tests/test_rest_http_request.py b/sdk/core/azure-core/tests/test_rest_http_request.py index 1072b7b49e42..70c184b5cc3d 100644 --- a/sdk/core/azure-core/tests/test_rest_http_request.py +++ b/sdk/core/azure-core/tests/test_rest_http_request.py @@ -239,6 +239,22 @@ def test_multipart_invalid_key_binary_string(): assert "Invalid type for data name" in str(e.value) assert repr(b"abc") in str(e.value) +def test_data_str_input(): + data = { + 'scope': 'fake_scope', + u'grant_type': 'refresh_token', + 'refresh_token': u'REDACTED', + 'service': 'fake_url.azurecr.io' + } + request = HttpRequest("POST", "http://localhost:3000/", data=data) + assert len(request.content) == 4 + assert request.content["scope"] == "fake_scope" + assert request.content["grant_type"] == "refresh_token" + assert request.content["refresh_token"] == u"REDACTED" + assert request.content["service"] == "fake_url.azurecr.io" + assert len(request.headers) == 1 + assert request.headers['Content-Type'] == 'application/x-www-form-urlencoded' + @pytest.mark.parametrize(("value"), (object(), {"key": "value"})) def test_multipart_invalid_value(value): From d6033d9fe9e945a4f696866c91fd9dafb7fef9a6 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 19 Oct 2021 17:26:37 -0700 Subject: [PATCH 2/3] use six.string_types instead --- sdk/core/azure-core/azure/core/rest/_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/core/azure-core/azure/core/rest/_helpers.py b/sdk/core/azure-core/azure/core/rest/_helpers.py index 58b544cc4f15..934bfa613c76 100644 --- a/sdk/core/azure-core/azure/core/rest/_helpers.py +++ b/sdk/core/azure-core/azure/core/rest/_helpers.py @@ -82,13 +82,13 @@ ########################### HELPER SECTION ################################# def _verify_data_object(name, value): - if not isinstance(name, str): + if not isinstance(name, six.string_types): raise TypeError( "Invalid type for data name. Expected str, got {}: {}".format( type(name), name ) ) - if value is not None and not isinstance(value, (str, bytes, int, float)): + if value is not None and not isinstance(value, (six.string_types, bytes, int, float)): raise TypeError( "Invalid type for data value. Expected primitive type, got {}: {}".format( type(name), name From 1c6f56707d32355d8a57264ce0c6c45e20b32503 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 19 Oct 2021 17:27:49 -0700 Subject: [PATCH 3/3] bump changelog --- sdk/core/azure-core/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index f17c5a6d40ee..2bdffd2b9e42 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -9,6 +9,7 @@ ### Bugs Fixed - respect text encoding specified in argument (thanks to @ryohji for the contribution) #20796 +- fix type check for `data` input to `azure.core.rest` for python 2.7 users #21341 ### Other Changes