예제 #1
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);
            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();
                }
            };
            es.EventReceived += (o, e) =>
            {
                receivedMessages.Add(e.Message);
                eventReceived.Set();
            };

            return(es);
        }
예제 #2
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"]);
        }
예제 #3
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);
        }
예제 #4
0
 public WebRequesterMock(ServiceResponseMock response)
 {
     this.Response = response;
 }
예제 #5
0
 public WebRequesterFactoryMock(ServiceResponseMock response)
 {
     this.WebRequesterMock = new WebRequesterMock(response);
 }
예제 #6
0
 public GetIsCalledEventArgs(ServiceResponseMock response)
 {
     ServerResponse = response;
 }