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); }
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"]); }
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); }
public WebRequesterMock(ServiceResponseMock response) { this.Response = response; }
public WebRequesterFactoryMock(ServiceResponseMock response) { this.WebRequesterMock = new WebRequesterMock(response); }
public GetIsCalledEventArgs(ServiceResponseMock response) { ServerResponse = response; }