From b83ae7dd4fa4cd92d52270a6a16a57fe9e4af189 Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Fri, 28 Jun 2024 14:32:05 +0800 Subject: [PATCH] improve UserAgent for csharp --- .gitignore | 1 + csharp/core/Common.cs | 9 ++------- csharp/tests/CommonTest.cs | 4 ++++ 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d9784850..ac086827 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ java/.classpath cc/*.o python/**/__pycache__/ cache/ +csharp/tests/coverage.xml \ No newline at end of file diff --git a/csharp/core/Common.cs b/csharp/core/Common.cs index c7d6bf99..17298d47 100644 --- a/csharp/core/Common.cs +++ b/csharp/core/Common.cs @@ -182,7 +182,7 @@ public static string ToFormString(Dictionary map) return result.ToString(); } - public static string ToJSONString(Object value) + public static string ToJSONString(object value) { if (value is string) { @@ -458,15 +458,10 @@ public static Stream AssertAsReadable(object a) internal static string GetDefaultUserAgent() { - string defaultUserAgent = string.Empty; string OSVersion = Environment.OSVersion.ToString(); string ClientVersion = GetRuntimeRegexValue(RuntimeEnvironment.GetRuntimeDirectory()); string CoreVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); - defaultUserAgent = "Alibaba Cloud (" + OSVersion + ") "; - defaultUserAgent += ClientVersion; - defaultUserAgent += " Core/" + CoreVersion; - defaultUserAgent += " TeaDSL/1"; - return defaultUserAgent; + return string.Format("AlibabaCloud ({0}) {1} Core/{2} TeaDSL/1", OSVersion, ClientVersion, CoreVersion); } internal static string GetRuntimeRegexValue(string value) diff --git a/csharp/tests/CommonTest.cs b/csharp/tests/CommonTest.cs index 010d1f59..684339e2 100644 --- a/csharp/tests/CommonTest.cs +++ b/csharp/tests/CommonTest.cs @@ -317,6 +317,10 @@ public void Test_GetUserAgent() { Assert.NotNull(Common.GetUserAgent(null)); + string ua = Common.GetUserAgent("").Trim(); + Assert.StartsWith("AlibabaCloud (", ua); + Assert.EndsWith("TeaDSL/1", ua); + Assert.Contains("test", Common.GetUserAgent("test")); }