Skip to content
Draft
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions src/azure-cli-core/azure/cli/core/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,33 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
request = send_mock.call_args[0][1]
self.assertEqual(request.headers['User-Agent'], get_az_rest_user_agent() + ' env-ua ARG-UA')

@mock.patch.dict('os.environ')
@mock.patch('azure.cli.core._profile.Profile.get_raw_token', autospec=True)
@mock.patch('requests.Session.send', autospec=True)
def test_send_raw_request_adds_json_content_type_for_string_body(self, send_mock, get_raw_token_mock):
return_val = mock.MagicMock()
return_val.is_ok = True
send_mock.return_value = return_val
get_raw_token_mock.return_value = ("Bearer", "******", None), None, None

cli_ctx = DummyCli()
cli_ctx.data = {
'command': 'rest',
'safe_params': ['method', 'uri']
}
url = 'https://management.azure.com/subscriptions/00000001-0000-0000-0000-000000000000/resourcegroups/02?api-version=2019-07-01'

for method in ['PUT', 'POST', 'PATCH']:
send_raw_request(cli_ctx, method, url, body='auth4.json', generated_client_request_id_name=None)
request = send_mock.call_args[0][1]
self.assertEqual(request.headers['Content-Type'], 'application/json')
self.assertEqual(request.body, 'auth4.json')

send_raw_request(cli_ctx, 'PUT', url, body='auth4.json', headers={'Content-Type=text/plain'},
generated_client_request_id_name=None)
request = send_mock.call_args[0][1]
self.assertEqual(request.headers['Content-Type'], 'text/plain')

@mock.patch("psutil.Process")
def test_get_parent_proc_name(self, mock_process_type):
process = mock_process_type.return_value
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli-core/azure/cli/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,10 +1003,10 @@ def send_raw_request(cli_ctx, method, url, headers=None, uri_parameters=None, #
# https://github.com/python/cpython/blob/3.10/Lib/http/client.py#L164
# https://github.com/python/cpython/blob/3.10/Lib/http/client.py#L1324-L1327
body = json.dumps(body_object)
if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/json'
except Exception: # pylint: disable=broad-except
pass
if 'Content-Type' not in headers and method and method.upper() in ['PUT', 'POST', 'PATCH']:
headers['Content-Type'] = 'application/json'

# add telemetry
headers['CommandName'] = cli_ctx.data['command']
Expand Down
Loading