public void TestSuccesfulConnection()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts = new CancellationTokenSource();
            List<EventSourceState> states = new List<EventSourceState>();
            ServiceResponseMock response = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory = new WebRequesterFactoryMock(response);
            ManualResetEvent stateIsOpen = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);
            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsOpen.Set();
                    cts.Cancel();
                }
            };


            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();

            // assert
            Assert.IsTrue(states.Count == 2);
            Assert.AreEqual(states[0], EventSourceState.CONNECTING);
            Assert.AreEqual(states[1], EventSourceState.OPEN);
        }
Exemple #2
0
        public void TestMultipleEvents()
        {
            // setup
            TestableEventSource es = SetupAndConnect();

            // act
            es.Start(cts.Token);
            stateIsOpen.WaitOrThrow();

            //entire event in one line
            response.WriteTestTextToStream("id: 1" + "\n" + "event: newevent" + "\n" + "data: Hello" + "\n\n");
            eventReceived.WaitOrThrow();
            eventReceived.Reset();

            //This event is split up over multiple res.write lines
            response.WriteTestTextToStream("id:2 " + "\n");
            response.WriteTestTextToStream("event: event 3" + "\n");
            response.WriteTestTextToStream("data: Hello again");
            response.WriteTestTextToStream("\n\n");
            eventReceived.WaitOrThrow();
            eventReceived.Reset();

            //Again an event that is split up over multiple res.write statements
            response.WriteTestTextToStream("id: 3" + "\n");
            response.WriteTestTextToStream("event: event3" + "\n" + "data: Goodbye" + "\n\n");
            eventReceived.WaitOrThrow();
            eventReceived.Reset();

            // assert
            Assert.AreEqual(receivedMessages.Count, 3);
        }
        private TestableEventSource SetupAndConnect()
        {
            // setup
            cts = new CancellationTokenSource();
            states = new List<EventSourceState>();
            response = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            factory = new WebRequesterFactoryMock(response);
            stateIsOpen = new ManualResetEvent(false);
            receivedMessages = new List<ServerSentEvent>();
            eventReceived = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);
            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                    stateIsOpen.Set();
            };
            es.EventReceived += (o, e) =>
            {
                receivedMessages.Add(e.Message);
                eventReceived.Set();
            };

            return es;
        }
Exemple #4
0
        private TestableEventSource SetupAndConnect()
        {
            // setup
            cts              = new CancellationTokenSource();
            states           = new List <EventSourceState>();
            response         = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            factory          = new WebRequesterFactoryMock(response);
            stateIsOpen      = new ManualResetEvent(false);
            receivedMessages = new List <ServerSentEvent>();
            eventReceived    = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);

            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsOpen.Set();
                }
            };
            es.EventReceived += (o, e) =>
            {
                receivedMessages.Add(e.Message);
                eventReceived.Set();
            };

            return(es);
        }
        public void TestSuccesfulConnection()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts         = new CancellationTokenSource();
            List <EventSourceState> states      = new List <EventSourceState>();
            ServiceResponseMock     response    = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory     = new WebRequesterFactoryMock(response);
            ManualResetEvent        stateIsOpen = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);

            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsOpen.Set();
                    cts.Cancel();
                }
            };


            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();

            // assert
            Assert.IsTrue(states.Count == 2);
            Assert.AreEqual(states[0], EventSourceState.CONNECTING);
            Assert.AreEqual(states[1], EventSourceState.OPEN);
        }
Exemple #6
0
        public void TestDoubleCarriageReturnLineFeedPair()
        {
            // setup
            TestableEventSource es = SetupAndConnect();

            // act
            es.Start(cts.Token);
            stateIsOpen.WaitOrThrow();
            response.WriteTestTextToStream("event: test\r\n\r\n");
            eventReceived.WaitOrThrow();

            // assert
            Assert.AreEqual(receivedMessages.Count, 1);
            Assert.AreEqual(receivedMessages[0].EventType, "test");
        }
Exemple #7
0
        public void TestMixedSeparators()
        {
            // setup
            TestableEventSource es = SetupAndConnect();

            // act
            es.Start(cts.Token);
            stateIsOpen.WaitOrThrow();
            response.WriteTestTextToStream("event: test\rdata: simple\n\n");
            eventReceived.WaitOrThrow();

            // assert
            Assert.AreEqual(receivedMessages.Count, 1);
            Assert.AreEqual(receivedMessages[0].EventType, "test");
            Assert.AreEqual(receivedMessages[0].Data, "simple\n");
        }
Exemple #8
0
        public void TestReConnectionAfterConnectionLost()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts    = new CancellationTokenSource();
            List <EventSourceState> states = new List <EventSourceState>();
            ServiceResponseMock     serviceResponseMock = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory             = new WebRequesterFactoryMock(serviceResponseMock);
            ManualResetEvent        stateIsOpen         = new ManualResetEvent(false);
            ManualResetEvent        stateIsClosed       = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);

            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsClosed.Reset();
                    stateIsOpen.Set();
                }
                else if (e.State == EventSourceState.CLOSED)
                {
                    stateIsOpen.Reset();
                    stateIsClosed.Set();
                }
            };


            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();
            states.Clear();

            serviceResponseMock.DistantConnectionClose();

            stateIsClosed.WaitOrThrow();
            stateIsOpen.WaitOrThrow();

            // assert
            Assert.AreEqual(states[0], EventSourceState.CLOSED);
            Assert.AreEqual(states[1], EventSourceState.CONNECTING);
            Assert.AreEqual(states[2], EventSourceState.OPEN);
        }
        public void TestReConnectionAfterConnectionLost()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts = new CancellationTokenSource();
            List<EventSourceState> states = new List<EventSourceState>();
            ServiceResponseMock serviceResponseMock = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory = new WebRequesterFactoryMock(serviceResponseMock);
            ManualResetEvent stateIsOpen = new ManualResetEvent(false);
            ManualResetEvent stateIsClosed = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);
            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsClosed.Reset();
                    stateIsOpen.Set();
                }
                else if (e.State == EventSourceState.CLOSED)
                {
                    stateIsOpen.Reset();
                    stateIsClosed.Set();
                }
            };

            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();
            states.Clear();

            serviceResponseMock.DistantConnectionClose();

            stateIsClosed.WaitOrThrow();
            stateIsOpen.WaitOrThrow();

            // assert
            Assert.AreEqual(states[0], EventSourceState.CLOSED);
            Assert.AreEqual(states[1], EventSourceState.CONNECTING);
            Assert.AreEqual(states[2], EventSourceState.OPEN);
        }
        public void TestSuccesfulConnectionWithHeaders()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts         = new CancellationTokenSource();
            List <EventSourceState> states      = new List <EventSourceState>();
            ServiceResponseMock     response    = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory     = new WebRequesterFactoryMock(response);
            ManualResetEvent        stateIsOpen = new ManualResetEvent(false);

            var headers = new Dictionary <string, string>
            {
                { "x-key", "headerValue" }
            };

            TestableEventSource es = new TestableEventSource(url, factory, headers);

            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsOpen.Set();
                    cts.Cancel();
                }
            };


            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();

            // assert
            Assert.AreEqual(1, factory.WebRequesterMock.Response.Headers.Count);
            Assert.AreEqual("headerValue", factory.WebRequesterMock.Response.Headers["x-key"]);
        }