public static void LogHttpHeader(string headerName, IEnumerable <string> headerValues)
 {
     TestUtility.Log($"{headerName}: {string.Join(",", headerValues)}");
 }
        public void ListenerValidation()
        {
            var entityPath       = "myentity";
            var connectionString = "Endpoint=sb://whatever.servicebus.windows.net/";
            var connectionStringWithEntityPath      = $"Endpoint=sb://whatever.servicebus.windows.net/;EntityPath={entityPath}";
            var connectionStringWithSASKeyValueOnly = $"Endpoint=sb://whatever.servicebus.windows.net/;SharedAccessKey={TestUtility.GenerateRandomSasKey()}";

            Assert.Throws <ArgumentNullException>(() => new HybridConnectionListener((string)null));
            Assert.Throws <ArgumentNullException>(() => new HybridConnectionListener(string.Empty));
            Assert.Throws <ArgumentException>(() => new HybridConnectionListener(connectionString));
            Assert.Throws <ArgumentNullException>(() => new HybridConnectionListener(connectionString, null));
            Assert.Throws <ArgumentNullException>(() => new HybridConnectionListener(connectionString, string.Empty));
            Assert.Throws <ArgumentException>(() => new HybridConnectionListener(connectionString, entityPath));
            Assert.Throws <ArgumentException>(() => new HybridConnectionListener(connectionStringWithEntityPath, entityPath));
            Assert.Throws <ArgumentException>(() => new HybridConnectionListener(connectionStringWithSASKeyValueOnly, entityPath));
        }
 public override void After(MethodInfo methodUnderTest)
 {
     TestUtility.Log($"End {methodUnderTest.DeclaringType}.{methodUnderTest.Name}");
     base.After(methodUnderTest);
 }
        public async Task ConnectionStringBuilderOperationValidation()
        {
            TestUtility.Log("Create a new connection string using RelayConnectionStringBuilder properties.");
            var connectionStringBuilder = new RelayConnectionStringBuilder()
            {
                Endpoint            = this.endpoint,
                EntityPath          = this.entityPath,
                SharedAccessKeyName = this.sasKeyName,
                SharedAccessKey     = this.sasKeyValue
            };
            var connectionString = connectionStringBuilder.ToString();

            // Endpoint is expected to appear first in the connection string.
            Assert.StartsWith("Endpoint=", connectionString);

            Assert.Contains($"Endpoint={this.endpoint.AbsoluteUri}", connectionString);
            Assert.Contains($"EntityPath={this.entityPath}", connectionString);
            Assert.Contains($"SharedAccessKeyName={this.sasKeyName}", connectionString);
            Assert.Contains($"SharedAccessKey={this.sasKeyValue}", connectionString);

            // OperationTimeout should be omitted since it is the default value.
            Assert.DoesNotContain("OperationTimeout", connectionString);

            // SharedAccessSignature should be omitted since it is not specified.
            Assert.DoesNotContain("SharedAccessSignature", connectionString);

            TestUtility.Log("Try to set the timeout to a negative number");
            Assert.Throws <ArgumentOutOfRangeException>(() => connectionStringBuilder.OperationTimeout = TimeSpan.FromMinutes(-1));

            TestUtility.Log("Try to create a connection string with SAS KeyName but no KeyValue");
            connectionStringBuilder.SharedAccessKeyName = this.sasKeyName;
            connectionStringBuilder.SharedAccessKey     = null;
            Assert.Throws <ArgumentException>(() => connectionStringBuilder.ToString());

            TestUtility.Log("Try to create a connection string with SAS KeyValue but no KeyName");
            connectionStringBuilder.SharedAccessKeyName = null;
            connectionStringBuilder.SharedAccessKey     = this.sasKeyValue;
            Assert.Throws <ArgumentException>(() => connectionStringBuilder.ToString());

            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(this.sasKeyName, this.sasKeyValue);
            var sasToken      = await tokenProvider.GetTokenAsync(this.endpoint.AbsoluteUri, TimeSpan.FromMinutes(5));

            TestUtility.Log("Create a connection string with SAS token only");
            connectionStringBuilder.SharedAccessSignature = sasToken.TokenString;
            connectionStringBuilder.SharedAccessKeyName   = null;
            connectionStringBuilder.SharedAccessKey       = null;
            connectionString = connectionStringBuilder.ToString();

            TestUtility.Log("Parse a connection string with SAS token only");
            connectionStringBuilder = new RelayConnectionStringBuilder(connectionString);
            Assert.Equal(sasToken.TokenString, connectionStringBuilder.SharedAccessSignature);
            Assert.Null(connectionStringBuilder.SharedAccessKeyName);
            Assert.Null(connectionStringBuilder.SharedAccessKey);

            TestUtility.Log("Create a connection string with SAS token and SAS KeyName");
            connectionStringBuilder.SharedAccessSignature = sasToken.TokenString;
            connectionStringBuilder.SharedAccessKeyName   = this.sasKeyName;
            connectionStringBuilder.SharedAccessKey       = null;
            Assert.Throws <ArgumentException>(() => connectionStringBuilder.ToString());

            TestUtility.Log("Create a connection string with SAS token and SAS Key value");
            connectionStringBuilder.SharedAccessSignature = sasToken.TokenString;
            connectionStringBuilder.SharedAccessKeyName   = null;
            connectionStringBuilder.SharedAccessKey       = this.sasKeyValue;
            Assert.Throws <ArgumentException>(() => connectionStringBuilder.ToString());

            TestUtility.Log("Create a new ConnectionStringBuilder, set no properties, and call ToString()");
            connectionStringBuilder = new RelayConnectionStringBuilder();
            Assert.Throws <ArgumentNullException>(() => connectionStringBuilder.ToString());

            TestUtility.Log("Set only Endpoint, call ToString()");
            connectionStringBuilder = new RelayConnectionStringBuilder()
            {
                Endpoint = this.endpoint
            };
            connectionString = connectionStringBuilder.ToString();

            TestUtility.Log("Set OperationTimeout using connectionString");
            TimeSpan operationTimeout = TimeSpan.FromSeconds(90);

            connectionString       += $"OperationTimeout={operationTimeout.ToString(null, CultureInfo.InvariantCulture)}";
            connectionStringBuilder = new RelayConnectionStringBuilder(connectionString);
            Assert.Equal(operationTimeout, connectionStringBuilder.OperationTimeout);
            connectionString = connectionStringBuilder.ToString();

            var expectedOperationTimeOutString = $";OperationTimeout={operationTimeout.ToString(null, CultureInfo.InvariantCulture)}";

            Assert.Contains(expectedOperationTimeOutString, connectionString);
        }
 public override void Before(MethodInfo methodUnderTest)
 {
     TestUtility.Log($"Begin {methodUnderTest.DeclaringType}.{methodUnderTest.Name}({TestUtility.RuntimeFramework})");
     base.Before(methodUnderTest);
 }