{Core} Extract User-Agent generation to get_az_user_agent#12734
{Core} Extract User-Agent generation to get_az_user_agent#12734
Conversation
|
|
||
| @mock.patch('azure.cli.core._profile.Profile.get_raw_token', autospec=True) | ||
| @mock.patch('requests.Session.send', autospec=True) | ||
| def test_send_raw_requests(self, send_mock, get_raw_token_mock): |
There was a problem hiding this comment.
These test methods are moved from the wrong class TestHandleException to the right class TestUtils.
| # Test custom case-insensitive User-Agent | ||
| with mock.patch.dict('os.environ', {'AZURE_HTTP_USER_AGENT': "env-ua"}): | ||
| send_raw_request(cli_ctx, 'GET', full_arm_rest_url, headers={'user-agent=ARG-UA'}) | ||
|
|
||
| get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id) | ||
| request = send_mock.call_args.args[1] | ||
| self.assertEqual(request.headers['User-Agent'], get_az_user_agent() + ' env-ua ARG-UA') |
There was a problem hiding this comment.
User-Agent for az rest is tested here.
| @mock.patch('azure.cli.core.__version__', '7.8.9') | ||
| def test_get_az_user_agent(self): | ||
| actual = get_az_user_agent() | ||
| self.assertEqual(actual, 'AZURECLI/7.8.9') |
There was a problem hiding this comment.
#12717 can use this test method to verify whether the installer is added to UA correctly.
|
add to S167 |
|
Azure DevOps seems to be adding Add |
| # if ENV_ADDITIONAL_USER_AGENT in os.environ: | ||
| # agents.append(os.environ[ENV_ADDITIONAL_USER_AGENT]) | ||
|
|
||
| return ' '.join(agents) |
There was a problem hiding this comment.
May I know why we need the new function to handle agents? From my side, the previous UA definition should be fine, too.
There was a problem hiding this comment.
Because previously the functionality of concatenating UA is dispersed in 3 places:
- mgmt client
- data client
- az rest
It is difficult to maintain. I extrated the logic to one place so that changes done here automatically applies to all 3 places, given #12717 is doing more tweaks on the UA.
Extract
User-Agentgeneration toget_az_user_agentso that PR #12717 can also apply toaz rest.The logic to append custom
User-AgentviaAZURE_HTTP_USER_AGENTenv var is removed from mgmt and data client factory, as this is already handled by msrest and causes duplication:Note that
myuaappeared twice.On the other hand, this logic is added to
az restto align with msrest.az restpreviously can't handleAZURE_HTTP_USER_AGENT.