Skip to content

Commit 30cf933

Browse files
authored
Fix TableClient.Uri Property when Targeting Azurite (Azure#35566)
* Fix Uri generation for emulator clients
1 parent 8fb0f86 commit 30cf933

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

sdk/tables/Azure.Data.Tables/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88

99
### Bugs Fixed
10+
- Fixed the URL returned by `TableClient.Uri` when connecting to Azurite
1011

1112
### Other Changes
1213

sdk/tables/Azure.Data.Tables/src/TableClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public TableClient(string connectionString, string tableName, TableClientOptions
236236
_tableOperations = new TableRestClient(_diagnostics, _pipeline, _endpoint.AbsoluteUri, _version);
237237
_batchOperations = new TableRestClient(_diagnostics, CreateBatchPipeline(), _tableOperations.endpoint, _tableOperations.clientVersion);
238238
Name = tableName;
239-
Uri = new Uri(_endpoint, Name);
239+
Uri = new TableUriBuilder(_endpoint) { Query = null, Sas = null, Tablename = Name }.ToUri();
240240
}
241241

242242
/// <summary>
@@ -288,7 +288,7 @@ public TableClient(Uri endpoint, string tableName, TokenCredential tokenCredenti
288288
_tableOperations = new TableRestClient(_diagnostics, _pipeline, _endpoint.AbsoluteUri, _version);
289289
_batchOperations = new TableRestClient(_diagnostics, CreateBatchPipeline(), _tableOperations.endpoint, _tableOperations.clientVersion);
290290
Name = tableName;
291-
Uri = new Uri(_endpoint, Name);
291+
Uri = new TableUriBuilder(_endpoint) { Query = null, Sas = null, Tablename = Name }.ToUri();
292292
}
293293

294294
internal TableClient(Uri endpoint, string tableName, TableSharedKeyPipelinePolicy policy, AzureSasCredential sasCredential, TableClientOptions options)
@@ -339,7 +339,7 @@ null when string.IsNullOrWhiteSpace(_endpoint.Query) => policy,
339339
_tableOperations = new TableRestClient(_diagnostics, _pipeline, _endpoint.AbsoluteUri, _version);
340340
_batchOperations = new TableRestClient(_diagnostics, CreateBatchPipeline(), _tableOperations.endpoint, _tableOperations.clientVersion);
341341
Name = tableName;
342-
Uri = new Uri(_endpoint, Name);
342+
Uri = new TableUriBuilder(_endpoint) { Query = null, Sas = null, Tablename = Name }.ToUri();
343343
}
344344

345345
internal TableClient(
@@ -366,7 +366,7 @@ internal TableClient(
366366
_batchOperations = new TableRestClient(diagnostics, CreateBatchPipeline(), _tableOperations.endpoint, _tableOperations.clientVersion);
367367
_version = version;
368368
Name = table;
369-
Uri = new Uri(_endpoint, Name);
369+
Uri = new TableUriBuilder(_endpoint) { Query = null, Sas = null, Tablename = Name }.ToUri();
370370
_accountName = accountName;
371371
_diagnostics = diagnostics;
372372
_isCosmosEndpoint = isPremiumEndpoint;

sdk/tables/Azure.Data.Tables/tests/TableClientTests.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -511,26 +511,48 @@ public void GenerateSasUri(TableClient client, TableSharedKeyCredential cred)
511511
CollectionAssert.Contains(actualSas.Segments, TableName);
512512
}
513513

514-
private static IEnumerable<object[]> TableClientsAllCtors()
514+
private static IEnumerable<object[]> TableClientsAllCtors(bool useEmulator)
515515
{
516-
var sharedKeyCred = new TableSharedKeyCredential(AccountName, Secret);
516+
Uri url;
517+
string connectionString;
518+
TableSharedKeyCredential sharedKeyCred;
519+
520+
if (useEmulator)
521+
{
522+
url = new Uri("http://127.0.0.1:10002/devstoreaccount1");
523+
connectionString = "UseDevelopmentStorage=true";
524+
sharedKeyCred = new TableSharedKeyCredential("devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==");
525+
}
526+
else
527+
{
528+
url = _url;
529+
connectionString = $"DefaultEndpointsProtocol=https;AccountName={AccountName};AccountKey={Secret};TableEndpoint=https://{AccountName}.table.core.windows.net/;";
530+
sharedKeyCred = new TableSharedKeyCredential(AccountName, Secret);
531+
}
532+
517533
var tokenCred = new MockCredential();
518-
var connString = $"DefaultEndpointsProtocol=https;AccountName={AccountName};AccountKey={Secret};TableEndpoint=https://{AccountName}.table.core.windows.net/;";
519534
var sasCred = new AzureSasCredential(signature);
520-
var fromTableServiceClient = new TableServiceClient(_url, sharedKeyCred).GetTableClient(TableName);
535+
var fromTableServiceClient = new TableServiceClient(url, sharedKeyCred).GetTableClient(TableName);
521536

522-
yield return new object[] { new TableClient(connString, TableName) };
523-
yield return new object[] { new TableClient(_url, TableName, sharedKeyCred) };
524-
yield return new object[] { new TableClient(_url, TableName, tokenCred) };
525-
yield return new object[] { new TableClient(_url, sasCred) };
526-
yield return new object[] { new TableClient(new Uri($"{_url}?{signature}")) };
537+
yield return new object[] { new TableClient(connectionString, TableName) };
538+
yield return new object[] { new TableClient(url, TableName, sharedKeyCred) };
539+
yield return new object[] { new TableClient(url, TableName, tokenCred) };
540+
yield return new object[] { new TableClient(url, sasCred) };
541+
yield return new object[] { new TableClient(new Uri($"{url}?{signature}")) };
527542
yield return new object[] { fromTableServiceClient };
528543
}
529544

530-
[TestCaseSource(nameof(TableClientsAllCtors))]
545+
[TestCaseSource(nameof(TableClientsAllCtors), new object[] { false })]
531546
public void UriPropertyIsPopulated(TableClient client)
532547
{
533-
Assert.AreEqual($"{_url}{TableName}", client.Uri.AbsoluteUri);
548+
Assert.AreEqual(_urlWithTableName, client.Uri);
549+
Assert.That(client.Uri.AbsoluteUri, Does.Not.Contain(signature));
550+
}
551+
552+
[TestCaseSource(nameof(TableClientsAllCtors), new object[] { true })]
553+
public void UriPropertyIsPopulatedForEmulator(TableClient client)
554+
{
555+
Assert.AreEqual(new Uri("http://127.0.0.1:10002/devstoreaccount1/" + TableName), client.Uri);
534556
Assert.That(client.Uri.AbsoluteUri, Does.Not.Contain(signature));
535557
}
536558

0 commit comments

Comments
 (0)