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

[Service Bus] Populate TransportType #20920

Merged
merged 1 commit into from
May 7, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ protected ServiceBusClient()
/// If the connection string specifies a specific entity name, any subsequent calls to
/// <see cref="CreateSender(string)"/>, <see cref="CreateReceiver(string)"/>,
/// <see cref="CreateProcessor(string)"/> etc. must still specify the same entity name.
///
/// The connection string will recognize and apply properties populated by the
/// Azure portal such as Endpoint, SharedAccessKeyName, SharedAccessKey, and EntityPath.
/// Other values will be ignored; to configure the processor, please use the <see cref="ServiceBusClientOptions" />.
/// </remarks>
public ServiceBusClient(string connectionString) :
this(connectionString, new ServiceBusClientOptions())
Expand All @@ -134,6 +138,10 @@ public ServiceBusClient(string connectionString) :
/// If the connection string specifies a specific entity name, any subsequent calls to
/// <see cref="CreateSender(string)"/>, <see cref="CreateReceiver(string)"/>,
/// <see cref="CreateProcessor(string)"/> etc. must still specify the same entity name.
///
/// The connection string will recognize and apply properties populated by the
/// Azure portal such as Endpoint, SharedAccessKeyName, SharedAccessKey, and EntityPath.
/// Other values will be ignored; to configure the processor, please use the <see cref="ServiceBusClientOptions" />.
/// </remarks>
public ServiceBusClient(string connectionString, ServiceBusClientOptions options)
{
Expand All @@ -142,6 +150,7 @@ public ServiceBusClient(string connectionString, ServiceBusClientOptions options
Logger.ClientCreateStart(typeof(ServiceBusClient), FullyQualifiedNamespace);
Identifier = DiagnosticUtilities.GenerateIdentifier(FullyQualifiedNamespace);
Plugins = _options.Plugins;
TransportType = _options.TransportType;
Logger.ClientCreateComplete(typeof(ServiceBusClient), Identifier);
}

Expand Down Expand Up @@ -218,14 +227,15 @@ private ServiceBusClient(
object credential,
ServiceBusClientOptions options)
{
_options = options?.Clone() ?? new ServiceBusClientOptions();
Logger.ClientCreateStart(typeof(ServiceBusClient), fullyQualifiedNamespace);
_options = options?.Clone() ?? new ServiceBusClientOptions();
Identifier = DiagnosticUtilities.GenerateIdentifier(fullyQualifiedNamespace);
Connection = ServiceBusConnection.CreateWithCredential(
fullyQualifiedNamespace,
credential,
_options);
Plugins = _options.Plugins;
TransportType = _options.TransportType;
Logger.ClientCreateComplete(typeof(ServiceBusClient), Identifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Azure.Messaging.ServiceBus.Tests.Client
{
[TestFixture]
public class ServiceBusClientTests
{
/// <summary>
Expand Down Expand Up @@ -266,10 +267,11 @@ public void ConstructorWithConnectionStringValidatesOptions()
/// </summary>
///
[Test]
public void ConstructorWithTokenCredentialArgumentsValidatesOptions()
public void ConstructorWithTokenCredentialValidatesOptions()
{
var token = new Mock<ServiceBusTokenCredential>(Mock.Of<TokenCredential>());
var invalidOptions = new ServiceBusClientOptions { TransportType = ServiceBusTransportType.AmqpTcp, WebProxy = Mock.Of<IWebProxy>() };

Assert.That(() => new ServiceBusClient("fullyQualifiedNamespace", Mock.Of<TokenCredential>(), invalidOptions), Throws.InstanceOf<ArgumentException>(), "The expanded argument constructor should validate client options");
}

Expand All @@ -279,11 +281,12 @@ public void ConstructorWithTokenCredentialArgumentsValidatesOptions()
/// </summary>
///
[Test]
public void ConstructorWithSharedKeyCredentialArgumentsValidatesOptions()
public void ConstructorWithSharedKeyCredentialValidatesOptions()
{
var token = new AzureNamedKeyCredential("key", "value");
var invalidOptions = new ServiceBusClientOptions { TransportType = ServiceBusTransportType.AmqpTcp, WebProxy = Mock.Of<IWebProxy>() };
Assert.That(() => new ServiceBusClient("fullyQualifiedNamespace", Mock.Of<TokenCredential>(), invalidOptions), Throws.InstanceOf<ArgumentException>(), "The expanded argument constructor should validate client options");

Assert.That(() => new ServiceBusClient("fullyQualifiedNamespace", token, invalidOptions), Throws.InstanceOf<ArgumentException>(), "The expanded argument constructor should validate client options");
}

/// <summary>
Expand All @@ -292,12 +295,74 @@ public void ConstructorWithSharedKeyCredentialArgumentsValidatesOptions()
/// </summary>
///
[Test]
public void ConstructorWithSasCredentialArgumentsValidatesOptions()
public void ConstructorWithSasCredentialValidatesOptions()
{
var signature = new SharedAccessSignature("sb://fake.thing.com", "fakeKey", "fakeValue");
var token = new AzureSasCredential(signature.Value);
var invalidOptions = new ServiceBusClientOptions { TransportType = ServiceBusTransportType.AmqpTcp, WebProxy = Mock.Of<IWebProxy>() };
Assert.That(() => new ServiceBusClient("fullyQualifiedNamespace", Mock.Of<TokenCredential>(), invalidOptions), Throws.InstanceOf<ArgumentException>(), "The expanded argument constructor should validate client options");

Assert.That(() => new ServiceBusClient("fullyQualifiedNamespace", token, invalidOptions), Throws.InstanceOf<ArgumentException>(), "The expanded argument constructor should validate client options");
}

/// <summary>
/// Verifies functionality of the <see cref="ServiceBusClient" />
/// constructor.
/// </summary>
///
[Test]
public void ConstructorWithConnectionStringSetsTransportTypeFromOptions()
{
var fakeConnection = "Endpoint=sb://not-real.servicebus.windows.net/;SharedAccessKeyName=DummyKey;SharedAccessKey=[not_real];EntityPath=fake";
var options = new ServiceBusClientOptions { TransportType = ServiceBusTransportType.AmqpWebSockets };
var client = new ServiceBusClient(fakeConnection, options);

Assert.That(client.TransportType, Is.EqualTo(options.TransportType));
}

/// <summary>
/// Verifies functionality of the <see cref="ServiceBusClient" />
/// constructor.
/// </summary>
///
[Test]
public void ConstructorWithTokenCredentialSetsTransportTypeFromOptions()
{
var token = new Mock<ServiceBusTokenCredential>(Mock.Of<TokenCredential>());
var options = new ServiceBusClientOptions { TransportType = ServiceBusTransportType.AmqpWebSockets };
var client = new ServiceBusClient("fullyQualifiedNamespace", Mock.Of<TokenCredential>(), options);

Assert.That(client.TransportType, Is.EqualTo(options.TransportType));
}

/// <summary>
/// Verifies functionality of the <see cref="ServiceBusClient" />
/// constructor.
/// </summary>
///
[Test]
public void ConstructorWithSharedKeyCredentialSetsTransportTypeFromOptions()
{
var token = new AzureNamedKeyCredential("key", "value");
var options = new ServiceBusClientOptions { TransportType = ServiceBusTransportType.AmqpWebSockets };
var client = new ServiceBusClient("fullyQualifiedNamespace", token, options);

Assert.That(client.TransportType, Is.EqualTo(options.TransportType));
}

/// <summary>
/// Verifies functionality of the <see cref="ServiceBusClient" />
/// constructor.
/// </summary>
///
[Test]
public void ConstructorWithSasCredentialSetsTransportTypeFromOptions()
{
var signature = new SharedAccessSignature("sb://fake.thing.com", "fakeKey", "fakeValue");
var token = new AzureSasCredential(signature.Value);
var options = new ServiceBusClientOptions { TransportType = ServiceBusTransportType.AmqpWebSockets };
var client = new ServiceBusClient("fullyQualifiedNamespace", token, options);

Assert.That(client.TransportType, Is.EqualTo(options.TransportType));
}

/// <summary>
Expand Down