Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CosmosClientOptions: Adds validation for ApplicationName #3455

Merged
merged 18 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 22 additions & 1 deletion Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,28 @@ public CosmosClientOptions()
/// <remarks>
/// Setting this property after sending any request won't have any effect.
/// </remarks>
public string ApplicationName { get; set; }
///
private string applicationName;
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
public string ApplicationName
{
get => this.applicationName;
set
{
HttpClient httpClient = new HttpClient();
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
try
{
using System.Net.Http.Headers.HttpHeaderParser.ParseValue(value);
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
}
catch (FormatException formatException)
imanvt marked this conversation as resolved.
Show resolved Hide resolved
{
ApplicationNameException e = new ApplicationNameException(value);//new exception i was thinking of making
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
throw e;
}

this.applicationName = value;

}
}

/// <summary>
/// Get or set session container for the client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,33 @@ public async Task HttpClientConnectionLimitTest()
$"Before connections: {JsonConvert.SerializeObject(excludeConnections)}; After connections: {JsonConvert.SerializeObject(afterConnections)}");
}

[TestMethod]
public void InvalidApplicationNameCatchTest()
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
string invalidName = "<<";
Console.WriteLine("Invalid string:\t" + invalidName);
CosmosClient cosmosClient = new CosmosClient(
ConfigurationManager.AppSettings["GatewayEndpoint"],
ConfigurationManager.AppSettings["MasterKey"],
new CosmosClientOptions
{
ApplicationName = invalidName
}
);
Console.WriteLine("String actually vaild");
}
catch(Exception exc)
{
Console.WriteLine("\n\n\n\n__________\n\nString was invalid, exception below");
Console.WriteLine(exc);
Console.WriteLine("\n_____\n\n\n\n");
}

//Assert.ThrowsExceptionAsync<exception type>(async () => {code in here});

}
public static IReadOnlyList<string> GetActiveConnections()
{
string testPid = Process.GetCurrentProcess().Id.ToString();
Expand Down