Exemplo n.º 1
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.Equal(receivedMessages.Count, 3);
        }
Exemplo n.º 2
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.Equal(receivedMessages.Count, 1);
            Assert.Equal(receivedMessages[0].EventType, "test");
        }
Exemplo n.º 3
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.Equal(receivedMessages.Count, 1);
            Assert.Equal(receivedMessages[0].EventType, "test");
            Assert.Equal(receivedMessages[0].Data, "simple\n");
        }
Exemplo n.º 4
0
        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);
            ILogger logger = new LoggerFactory().CreateLogger("test");

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

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

            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.Equal(1, factory.WebRequesterMock.Response.Headers.Count);
            Assert.Equal("headerValue", factory.WebRequesterMock.Response.Headers["x-key"]);
        }
Exemplo n.º 5
0
        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);
            ILogger logger = new LoggerFactory().CreateLogger("test");

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

            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.True(states.Count == 2);
            Assert.Equal(states[0], EventSourceState.CONNECTING);
            Assert.Equal(states[1], EventSourceState.OPEN);
        }