From ec0eb203be03c4f332113b4eb6e752f3effe8bbb Mon Sep 17 00:00:00 2001 From: Swathi Pillalamarri Date: Thu, 4 Feb 2021 17:24:43 -0500 Subject: [PATCH] allow conn str to have trailing semicolon, whitespace --- .../azure-servicebus/azure/servicebus/_base_handler.py | 6 +++--- .../tests/test_connection_string_parser.py | 8 ++++++++ sdk/servicebus/azure-servicebus/tests/test_sb_client.py | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/sdk/servicebus/azure-servicebus/azure/servicebus/_base_handler.py b/sdk/servicebus/azure-servicebus/azure/servicebus/_base_handler.py index 623c6e20e6e8f..a97124cb3345a 100644 --- a/sdk/servicebus/azure-servicebus/azure/servicebus/_base_handler.py +++ b/sdk/servicebus/azure-servicebus/azure/servicebus/_base_handler.py @@ -59,10 +59,10 @@ def _parse_conn_str(conn_str, check_case=False): shared_access_signature_expiry = None # type: Optional[int] # split connection string into properties - conn_properties = [s.split("=", 1) for s in conn_str.rstrip(";").split(";")] - if any(len(tup) != 2 for tup in conn_properties): + conn_settings = [s.split("=", 1) for s in conn_str.strip().rstrip(';').split(";")] + if any(len(tup) != 2 for tup in conn_settings): raise ValueError("Connection string is either blank or malformed.") - conn_settings = dict(conn_properties) # type: ignore + conn_settings = dict(conn_settings) # type: ignore # case sensitive check when parsing for connection string properties if check_case: diff --git a/sdk/servicebus/azure-servicebus/tests/test_connection_string_parser.py b/sdk/servicebus/azure-servicebus/tests/test_connection_string_parser.py index 5e89eab72377e..5968b478db4df 100644 --- a/sdk/servicebus/azure-servicebus/tests/test_connection_string_parser.py +++ b/sdk/servicebus/azure-servicebus/tests/test_connection_string_parser.py @@ -62,6 +62,14 @@ def test_sb_parse_conn_str_sas_trailing_semicolon(self, **kwargs): assert parse_result.shared_access_signature == 'THISISATESTKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXX=' assert parse_result.shared_access_key_name == None + def test_sb_parse_conn_str_whitespace_trailing_semicolon(self, **kwargs): + conn_str = ' Endpoint=sb://resourcename.servicebus.windows.net/;SharedAccessSignature=THISISATESTKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXX=; ' + parse_result = parse_connection_string(conn_str) + assert parse_result.endpoint == 'sb://resourcename.servicebus.windows.net/' + assert parse_result.fully_qualified_namespace == 'resourcename.servicebus.windows.net' + assert parse_result.shared_access_signature == 'THISISATESTKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXX=' + assert parse_result.shared_access_key_name == None + def test_sb_parse_conn_str_no_keyname(self, **kwargs): conn_str = 'Endpoint=sb://resourcename.servicebus.windows.net/;SharedAccessKey=THISISATESTKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXX=' with pytest.raises(ValueError) as e: diff --git a/sdk/servicebus/azure-servicebus/tests/test_sb_client.py b/sdk/servicebus/azure-servicebus/tests/test_sb_client.py index bff4c99ec4c96..e41fed886d5d4 100644 --- a/sdk/servicebus/azure-servicebus/tests/test_sb_client.py +++ b/sdk/servicebus/azure-servicebus/tests/test_sb_client.py @@ -256,7 +256,7 @@ def test_client_sas_credential(self, token = credential.get_token(auth_uri).token # Finally let's do it with SAS token + conn str - token_conn_str = "Endpoint=sb://{}/;SharedAccessSignature={};".format(hostname, token.decode()) + token_conn_str = "Endpoint=sb://{}/;SharedAccessSignature={}".format(hostname, token.decode()) client = ServiceBusClient.from_connection_string(token_conn_str) with client: