public void ServerReplyShouldContainsAccessControlAllowOrigin()
        {
            var owinHostingContext = CreateTestOwinHostingContext(new CorsOwinHostingModule(), new FakeOwinHostingModule());
            var owinHostingService = new OwinHostingService(owinHostingContext);

            owinHostingService.Start();

            try
            {
                // Given

                var requestUri = new Uri($"{owinHostingContext.Configuration.Scheme}://{owinHostingContext.Configuration.Name}:{owinHostingContext.Configuration.Port}/some/resource");
                var request    = WebRequest.Create(requestUri);
                request.Method = "GET";
                request.Headers.Add("Origin", "null");

                // When

                var response = request.GetResponse();
                var headers  = response.Headers;

                // Then
                Assert.IsTrue(headers.AllKeys.Contains("Access-Control-Allow-Origin"));
                Assert.AreEqual("true", headers["Access-Control-Allow-Credentials"]);
                Assert.IsTrue(headers.AllKeys.Contains("Access-Control-Allow-Credentials"));
                Assert.AreEqual("null", headers["Access-Control-Allow-Origin"]);
            }
            finally
            {
                owinHostingService.Stop();
            }
        }
Ejemplo n.º 2
0
        public void CheckCommunications()
        {
            var owinHostingContext = CreateTestOwinHostingContext(new SignalROwinHostingModule());
            var owinHostingService = new OwinHostingService(owinHostingContext);

            owinHostingService.Start();

            try
            {
                // Направленное оповещение от клиента клиенту

                {
                    // Given

                    var receiveEvent1 = new CountdownEvent(1);
                    var receiveEvent2 = new CountdownEvent(1);

                    var client1 = new WebClientNotification(owinHostingContext.Configuration, receiveEvent1, "someEvent1");
                    var client2 = new WebClientNotification(owinHostingContext.Configuration, receiveEvent2, "someEvent2");

                    // When

                    client1.Notify("someEvent2", "1to2");
                    client2.Notify("someEvent1", "2to1");

                    receiveEvent1.Wait(5000);
                    receiveEvent2.Wait(5000);

                    // Then

                    CollectionAssert.AreEquivalent(new[] { "1to2" }, client2.ReceiveMessages);
                    CollectionAssert.AreEquivalent(new[] { "2to1" }, client1.ReceiveMessages);
                }

                // Оповещение от сервера всем клиентам

                {
                    // Given

                    var receiveEvent = new CountdownEvent(3);

                    var server = new ClientNotificationService();

                    var client1 = new WebClientNotification(owinHostingContext.Configuration, receiveEvent, "someEvent1");
                    var client2 = new WebClientNotification(owinHostingContext.Configuration, receiveEvent, "someEvent1");
                    var client3 = new WebClientNotification(owinHostingContext.Configuration, receiveEvent, "someEvent1");

                    // When

                    server.Notify("someEvent1", "eventBody1");

                    receiveEvent.Wait(5000);

                    // Then

                    CollectionAssert.AreEquivalent(new[] { "eventBody1" }, client1.ReceiveMessages);
                    CollectionAssert.AreEquivalent(new[] { "eventBody1" }, client2.ReceiveMessages);
                    CollectionAssert.AreEquivalent(new[] { "eventBody1" }, client3.ReceiveMessages);
                }

                // Оповещения от сервера определенным клиентам

                {
                    // Given

                    var receiveEvent1 = new CountdownEvent(1);
                    var receiveEvent2 = new CountdownEvent(1);
                    var receiveEvent3 = new CountdownEvent(1);

                    var server = new ClientNotificationService();

                    var client1 = new WebClientNotification(owinHostingContext.Configuration, receiveEvent1, "someEvent1");
                    var client2 = new WebClientNotification(owinHostingContext.Configuration, receiveEvent2, "someEvent2");
                    var client3 = new WebClientNotification(owinHostingContext.Configuration, receiveEvent3, "someEvent3");

                    // When

                    server.Notify("someEvent1", "eventBody1");
                    server.Notify("someEvent2", "eventBody2");
                    server.Notify("someEvent3", "eventBody3");

                    receiveEvent1.Wait(5000);
                    receiveEvent2.Wait(5000);
                    receiveEvent3.Wait(5000);

                    // Then

                    CollectionAssert.AreEquivalent(new[] { "eventBody1" }, client1.ReceiveMessages);
                    CollectionAssert.AreEquivalent(new[] { "eventBody2" }, client2.ReceiveMessages);
                    CollectionAssert.AreEquivalent(new[] { "eventBody3" }, client3.ReceiveMessages);
                }
            }
            finally
            {
                owinHostingService.Stop();
            }
        }