Beispiel #1
0
 public void SetUp()
 {
     _simulation      = Simulation.Create();
     _testCertificate = TestCertificate.Find();
     if (_testCertificate == null)
     {
         Assert.Inconclusive("The test SSL certificate is not available. Use tools/Xim.Tests.Setup to install the certificate.");
     }
 }
Beispiel #2
0
        public async Task TestAzureBlobStorageRestApiOverSsl()
        {
            X509Certificate2 testCertificate = TestCertificate.Find();

            using ISimulation simulation = Simulation.Create();
            const string bookContents     = "title: Hello world!";
            var          sampleFileStream = new MemoryStream(Encoding.ASCII.GetBytes(bookContents));

            IApiSimulator azureBlobApi = simulation.AddApi()
                                         .SetCertificate(testCertificate)
                                         .AddHandler("HEAD /mystorage1/mycontainer1", ApiResponse.Ok())
                                         .AddHandler("GET /mystorage1/mycontainer1/books.txt", new ApiResponse(500)) // 1st call - trigger retry policy
                                         .AddHandler("GET /mystorage1/mycontainer1/books.txt", _ =>
            {
                var headers = Headers.FromString("x-ms-blob-type: BlockBlob");
                var body    = Body.FromStream(sampleFileStream);
                return(new ApiResponse(200, headers: headers, body: body));
            })
                                         .Build();

            await azureBlobApi.StartAsync();

            try
            {
                var storageConnectionString = $"DefaultEndpointsProtocol=https;AccountName=mystorage1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=https://127.0.0.1:{azureBlobApi.Port}/mystorage1;";
                var storageAccount          = CloudStorageAccount.Parse(storageConnectionString);

                CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container  = blobClient.GetContainerReference("mycontainer1");
                var containerExists           = await container.ExistsAsync();

                var            ms             = new MemoryStream();
                CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference("books.txt");
                await cloudBlockBlob.DownloadToStreamAsync(ms, null, null, null);

                var cloudBlockContents = Encoding.ASCII.GetString(ms.ToArray());
                var receivedApiCalls   = azureBlobApi.ReceivedApiCalls.ToList();

                cloudBlockContents.ShouldSatisfyAllConditions(
                    () => containerExists.ShouldBeTrue(),
                    () => cloudBlockContents.ShouldBe(bookContents),
                    () => receivedApiCalls[0].Action.ShouldStartWith("HEAD /mystorage1/mycontainer1"),
                    () => receivedApiCalls[1].Action.ShouldStartWith("GET /mystorage1/mycontainer1/books.txt"),
                    () => receivedApiCalls[1].Response.StatusCode.ShouldBe(500),
                    () => receivedApiCalls[2].Action.ShouldStartWith("GET /mystorage1/mycontainer1/books.txt"),
                    () => receivedApiCalls[2].Response.StatusCode.ShouldBe(200)
                    );
            }
            finally
            {
                await azureBlobApi.StopAsync();
            }
        }
Beispiel #3
0
        public async Task StartAsync_UsesDefaultSecurePort_WhenSecured()
        {
            using X509Certificate2 certificate = TestCertificate.Find();
            ServiceBusBuilder serviceBusBuilder = new ServiceBusBuilder(Substitute.For <ISimulation>())
                                                  .SetCertificate(certificate);
            var serviceBusSimulator = new ServiceBusSimulator(serviceBusBuilder);

            await serviceBusSimulator.StartAsync();

            try
            {
                serviceBusSimulator.Port.ShouldBe(5671);
            }
            finally
            {
                await serviceBusSimulator.StopAsync();
            }
        }
Beispiel #4
0
        public async Task StartAsync_ReportsActiveLocation_WhenSecured()
        {
            using X509Certificate2 certificate = TestCertificate.Find();
            ServiceBusBuilder serviceBusBuilder = new ServiceBusBuilder(Substitute.For <ISimulation>())
                                                  .SetCertificate(certificate);
            var serviceBusSimulator = new ServiceBusSimulator(serviceBusBuilder);

            await serviceBusSimulator.StartAsync();

            try
            {
                serviceBusSimulator.Location.ShouldStartWith($"amqps://127.0.0.1:{serviceBusSimulator.Port}");
            }
            finally
            {
                await serviceBusSimulator.StopAsync();
            }
        }
Beispiel #5
0
        public async Task StartAsync_UsesCertificate_WhenSpecified()
        {
            using X509Certificate2 certificate = TestCertificate.Find();
            ApiBuilder apiBuilder   = new ApiBuilder(Substitute.For <ISimulation>()).SetCertificate(certificate);
            var        apiSimulator = new ApiSimulator(apiBuilder);

            await apiSimulator.StartAsync();

            try
            {
                apiSimulator.Location.ShouldStartWith("https://");
            }
            finally
            {
                await apiSimulator.StopAsync();
            }

            certificate.Reset();
        }
        public async Task TestAzureServiceBusQueueSend()
        {
            X509Certificate2 testCertificate = TestCertificate.Find();

            if (testCertificate == null)
            {
                Assert.Inconclusive("The test SSL certificate is not available.");
            }

            using ISimulation simulation = Simulation.Create();
            const string queueName   = "my-queue-3278";
            var          messageBody = Encoding.ASCII.GetBytes("DTESTa");

            IServiceBusSimulator bus = simulation.AddServiceBus()
                                       .SetCertificate(testCertificate)
                                       .AddQueue(queueName)
                                       .Build();

            await bus.StartAsync();

            var queueClient = new QueueClient(bus.ConnectionString, queueName);

            var message = new Message(messageBody)
            {
                CorrelationId = "3278"
            };

            message.UserProperties["BoldManTrue"] = 32;
            await queueClient.SendAsync(message);

            await queueClient.CloseAsync();

            await bus.StopAsync();

            IReadOnlyList <IDelivery> deliveries = bus.Queues[queueName].Deliveries;

            bus.ShouldSatisfyAllConditions(
                () => deliveries.Count.ShouldBe(1),
                () => deliveries[0].Message.Body.ShouldBe(messageBody),
                () => deliveries[0].Message.ApplicationProperties["BoldManTrue"].ShouldBe(32)
                );
        }
        public async Task TestAzureServiceBusQueueReceive()
        {
            X509Certificate2 testCertificate = TestCertificate.Find();

            using ISimulation simulation = Simulation.Create();
            const string queueName    = "sb-queue-x";
            Exception    busException = null;

            IDelivery[] deliveries = null;

            IServiceBusSimulator bus = simulation.AddServiceBus()
                                       .SetCertificate(testCertificate)
                                       .AddQueue(queueName)
                                       .Build();

            await bus.StartAsync();

            IQueue busQueue = bus.Queues[queueName];

            var receiver = new MessageReceiver(bus.ConnectionString, queueName);

            try
            {
                receiver.ServiceBusConnection.OperationTimeout = TimeSpan.FromHours(1);
                receiver.RegisterMessageHandler(async(message, cancellationToken) =>
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if (message.MessageId == "2481")
                    {
                        await receiver.DeadLetterAsync(message.SystemProperties.LockToken);
                    }
                    else if (message.MessageId == "51847")
                    {
                        await receiver.AbandonAsync(message.SystemProperties.LockToken);
                    }
                    else
                    {
                        await receiver.CompleteAsync(message.SystemProperties.LockToken);
                    }
                }, new MessageHandlerOptions(e =>
                {
                    if (!(e.Exception is OperationCanceledException))
                    {
                        busException = e.Exception;
                    }
                    return(Task.CompletedTask);
                })
                {
                    AutoComplete = false
                });

                busQueue.Post(new Amqp.Message
                {
                    Properties = new Amqp.Framing.Properties {
                        MessageId = "2481"
                    }
                });
                busQueue.Post(new Amqp.Message
                {
                    Properties = new Amqp.Framing.Properties {
                        MessageId = "51847"
                    },
                });
                busQueue.Post(new Amqp.Message
                {
                    Properties = new Amqp.Framing.Properties {
                        MessageId = "33782"
                    },
                });
                busQueue.Post(new Amqp.Message());
                deliveries = busQueue.Deliveries.ToArray();

                IEnumerable <Task <bool> > deliveryTasks = deliveries.Select(d => d.WaitAsync(TimeSpan.FromSeconds(5)));
                await Task.WhenAll(deliveryTasks);
            }
            finally
            {
                await receiver.CloseAsync();

                await bus.StopAsync();
            }

            bus.ShouldSatisfyAllConditions(
                () => deliveries.Length.ShouldBe(4),
                () => deliveries[0].Result.ShouldBe(DeliveryResult.DeadLettered),
                () => deliveries[1].Result.ShouldBe(DeliveryResult.Abandoned),
                () => deliveries[2].Result.ShouldBe(DeliveryResult.Completed),
                () => deliveries[3].Result.ShouldBe(DeliveryResult.Completed)
                );
        }