Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/azure-cli-core/azure/cli/core/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@

class TestUtils(unittest.TestCase):

def _assert_headers(self, request_headers, expected_headers):
actual_headers = dict(request_headers)

encodings = {
encoding.strip().lower()
for encoding in actual_headers.get('Accept-Encoding', '').split(',')
if encoding.strip()
}
self.assertIn('gzip', encodings)
self.assertIn('deflate', encodings)

actual_headers.pop('Accept-Encoding', None)
self.assertDictEqual(actual_headers, expected_headers)

def test_load_json_from_file(self):
_, pathname = tempfile.mkstemp()

Expand Down Expand Up @@ -257,7 +271,6 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):

expected_header = {
'User-Agent': get_az_rest_user_agent(),
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
Expand All @@ -282,7 +295,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
self.assertEqual(request.url, 'https://myaccount.blob.core.windows.net/mycontainer/myblob?timeout=30&sv=2019-02-02&srt=s&ss=bf')
self.assertEqual(request.body, '{"b1": "v1"}')
# Verify no Authorization header
self.assertDictEqual(dict(request.headers), expected_header)
self._assert_headers(request.headers, expected_header)
self.assertEqual(send_mock.call_args[1]["verify"], not should_disable_connection_verify())

# Test Authorization header is skipped
Expand All @@ -291,7 +304,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):

get_raw_token_mock.assert_not_called()
request = send_mock.call_args[0][1]
self.assertDictEqual(dict(request.headers), expected_header)
self._assert_headers(request.headers, expected_header)

# Test Authorization header is already provided
send_raw_request(cli_ctx, 'GET', full_arm_rest_url,
Expand All @@ -300,7 +313,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):

get_raw_token_mock.assert_not_called()
request = send_mock.call_args[0][1]
self.assertDictEqual(dict(request.headers), {**expected_header, 'Authorization': 'Basic ABCDE'})
self._assert_headers(request.headers, {**expected_header, 'Authorization': 'Basic ABCDE'})

# Test Authorization header is auto appended
send_raw_request(cli_ctx, 'GET', full_arm_rest_url,
Expand All @@ -309,7 +322,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):

get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id, subscription=subscription_id)
request = send_mock.call_args[0][1]
self.assertDictEqual(dict(request.headers), expected_header_with_auth)
self._assert_headers(request.headers, expected_header_with_auth)

# Test ARM Subscriptions - List
# https://learn.microsoft.com/en-us/rest/api/resources/subscriptions/list
Expand All @@ -320,7 +333,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id)
request = send_mock.call_args[0][1]
self.assertEqual(request.url, test_arm_endpoint.rstrip('/') + '/subscriptions?api-version=2020-01-01')
self.assertDictEqual(dict(request.headers), expected_header_with_auth)
self._assert_headers(request.headers, expected_header_with_auth)

# Test ARM Tenants - List
# https://learn.microsoft.com/en-us/rest/api/resources/tenants/list
Expand All @@ -331,7 +344,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id)
request = send_mock.call_args[0][1]
self.assertEqual(request.url, test_arm_endpoint.rstrip('/') + '/tenants?api-version=2020-01-01')
self.assertDictEqual(dict(request.headers), expected_header_with_auth)
self._assert_headers(request.headers, expected_header_with_auth)

# Test ARM resource ID
# /subscriptions/00000001-0000-0000-0000-000000000000/resourcegroups/02?api-version=2019-07-01
Expand All @@ -341,7 +354,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id, subscription=subscription_id)
request = send_mock.call_args[0][1]
self.assertEqual(request.url, full_arm_rest_url)
self.assertDictEqual(dict(request.headers), expected_header_with_auth)
self._assert_headers(request.headers, expected_header_with_auth)

# Test full ARM URL
# https://management.azure.com/subscriptions/00000001-0000-0000-0000-000000000000/resourcegroups/02?api-version=2019-07-01
Expand Down
Loading