Exemplo n.º 1
0
        public async Task When_a_comment_SSE_is_received_then_a_comment_event_is_raised()
        {
            // Arrange
            var commentSent = ":";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(commentSent);

            var evt = new EventSource(new Configuration(_uri, handler, readTimeout: _defaultReadTimeout));

            string commentReceived       = string.Empty;
            var    wasCommentEventRaised = false;

            evt.CommentReceived += (_, e) =>
            {
                commentReceived       = e.Comment;
                wasCommentEventRaised = true;

                evt.Close();
            };

            //// Act
            await evt.StartAsync();

            //// Assert
            Assert.Equal(commentSent, commentReceived);
            Assert.True(wasCommentEventRaised);
        }
Exemplo n.º 2
0
        public async Task When_a_data_only_message_SSE_is_received_then_a_message_event_is_raised()
        {
            // Arrange
            var sse     = "data: this is a test message\n\n";
            var sseData = "this is a test message";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(sse);

            var evt = new EventSource(new Configuration(_uri, handler, readTimeout: _defaultReadTimeout));

            MessageEvent message = null;
            var          wasMessageReceivedEventRaised = false;

            evt.MessageReceived += (_, e) =>
            {
                message = e.Message;
                wasMessageReceivedEventRaised = true;

                evt.Close();
            };

            //// Act
            await evt.StartAsync();

            //// Assert
            Assert.Equal(sseData, message?.Data);
            Assert.True(wasMessageReceivedEventRaised);
        }
Exemplo n.º 3
0
        public async Task When_response_exceeds_read_timeout_then_read_timeout_exception_occurs()
        {
            var commentSent = ":";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(commentSent);

            TimeSpan readTimeout = TimeSpan.FromSeconds(4);
            TimeSpan timeout     = readTimeout.Add(TimeSpan.FromSeconds(1));

            var evt = new StubEventSource(new Configuration(_uri, handler, readTimeout: readTimeout), (int)timeout.TotalMilliseconds);

            var exceptionMessage = string.Empty;

            try
            {
                evt.Error += (_, e) =>
                {
                    exceptionMessage = e.Exception.Message;
                    evt.Close();
                };

                await evt.StartAsync();
            }
            catch (TaskCanceledException tce) {}

            Assert.Contains(exceptionMessage, Resources.EventSourceService_Read_Timeout);
            Assert.True(evt.ReadyState == ReadyState.Shutdown);
        }
Exemplo n.º 4
0
        public async Task When_response_does_not_exceed_read_timeout_then_expected_message_event_occurs()
        {
            var sse = "event: put\ndata: this is a test message\n\n";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(sse);
            handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.NoContent));

            TimeSpan readTimeout = TimeSpan.FromSeconds(4);
            TimeSpan timeout     = readTimeout.Subtract(TimeSpan.FromSeconds(1));

            var evt = new StubEventSource(new Configuration(_uri, handler, readTimeout: readTimeout), (int)timeout.TotalMilliseconds);

            var wasMessageReceivedEventRaised = false;
            var eventName = "message";

            evt.MessageReceived += (_, e) =>
            {
                eventName = e.EventName;
                wasMessageReceivedEventRaised = true;

                evt.Close();
            };

            await evt.StartAsync();

            Assert.Equal("put", eventName);
            Assert.True(wasMessageReceivedEventRaised);
        }
Exemplo n.º 5
0
        public async Task When_LastEventId_is_configured_then_the_outgoing_request_contains_Last_Event_Id_header()
        {
            // Arrange
            var sse         = ":";
            var lastEventId = "10";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(sse);

            var config = new Configuration(
                uri: _uri,
                messageHandler: handler,
                readTimeout: _defaultReadTimeout,
                lastEventId: lastEventId);

            var evt = new EventSource(config);

            evt.CommentReceived += (_, e) =>
            {
                evt.Close();
            };

            //// Act
            await evt.StartAsync();

            var request = handler.GetRequests().First();

            IEnumerable <string> headerValues;
            var lastEventHeaderExists = request.Headers.TryGetValues(Constants.LastEventIdHttpHeader, out headerValues);

            //// Assert
            Assert.True(lastEventHeaderExists);
            Assert.Equal(lastEventId, headerValues.First());
        }
Exemplo n.º 6
0
        public async Task When_Configuration_Request_headers_are_set_then_the_outgoing_request_contains_those_same_headers()
        {
            // Arrange
            var sse = ":";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(sse);

            var headers = new Dictionary <string, string> {
                { "User-Agent", "mozilla" }, { "Authorization", "testing" }
            };

            var config = new Configuration(_uri, handler, requestHeaders: headers, readTimeout: _defaultReadTimeout);

            var evt = new EventSource(config);

            evt.CommentReceived += (_, e) =>
            {
                evt.Close();
            };

            //// Act
            await evt.StartAsync();

            var request = handler.GetRequests().First();

            //// Assert
            Assert.True(headers.All(
                            item =>
                            request.Headers.Contains(item.Key) &&
                            request.Headers.GetValues(item.Key).Contains(item.Value)
                            ));
        }
Exemplo n.º 7
0
        public async Task When_an_message_SSE_contains_id_is_received_then_last_event_id_is_set()
        {
            // Arrange
            var sse = "id:200\nevent: put\ndata: this is a test message\n\n";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(sse);

            var evt = new EventSource(new Configuration(_uri, handler, readTimeout: _defaultReadTimeout));

            MessageEvent message = null;
            var          wasMessageReceivedEventRaised = false;

            evt.MessageReceived += (_, e) =>
            {
                message = e.Message;
                wasMessageReceivedEventRaised = true;

                evt.Close();
            };

            //// Act
            await evt.StartAsync();

            //// Assert
            Assert.Equal("200", message.LastEventId);
            Assert.True(wasMessageReceivedEventRaised);
        }
Exemplo n.º 8
0
        public async Task When_the_HttpRequest_is_sent_then_the_outgoing_request_contains_accept_header()
        {
            // Arrange
            var sse = ":";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(sse);

            var evt = new EventSource(new Configuration(_uri, handler, readTimeout: _defaultReadTimeout));

            evt.CommentReceived += (_, e) =>
            {
                evt.Close();
            };

            //// Act
            await evt.StartAsync();

            var request = handler.GetRequests().First();

            IEnumerable <string> headerValues;
            var acceptHeaderExists = request.Headers.TryGetValues(Constants.AcceptHttpHeader, out headerValues);

            //// Assert
            Assert.True(acceptHeaderExists);
            Assert.True(headerValues.Contains(Constants.EventStreamContentType));
        }
Exemplo n.º 9
0
        public async Task When_an_event_message_SSE_is_received_then_a_message_event_is_raised()
        {
            // Arrange
            var sse = "event: put\ndata: this is a test message\n\n";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(sse);

            var evt = new EventSource(new Configuration(_uri, handler, readTimeout: _defaultReadTimeout));

            var wasMessageReceivedEventRaised = false;
            var eventName = "message";

            evt.MessageReceived += (_, e) =>
            {
                eventName = e.EventName;
                wasMessageReceivedEventRaised = true;

                evt.Close();
            };

            //// Act
            await evt.StartAsync();

            //// Assert
            Assert.Equal("put", eventName);
            Assert.True(wasMessageReceivedEventRaised);
        }
Exemplo n.º 10
0
        public async Task When_server_returns_HTTP_error_a_reconnect_attempt_is_made()
        {
            var messageData = "hello";

            var handler = new StubMessageHandler();

            handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.Unauthorized));
            handler.QueueStringResponse("event: put\ndata: " + messageData + "\n\n");

            var evt = new EventSource(new Configuration(_uri, handler));

            var receiver = new ErrorReceiver(evt);

            string messageReceived = null;

            evt.MessageReceived += (_, e) =>
            {
                messageReceived = e.Message.Data;
                evt.Close();
            };

            await evt.StartAsync();

            Assert.Equal(2, handler.GetRequests().Count());
            Assert.NotNull(receiver.ErrorReceived);
            var ex = Assert.IsType <EventSourceServiceUnsuccessfulResponseException>(receiver.ErrorReceived);

            Assert.Equal((int)HttpStatusCode.Unauthorized, ex.StatusCode);
            Assert.Equal(messageData, messageReceived);
        }
Exemplo n.º 11
0
        public async Task Default_HTTP_method_is_get()
        {
            var handler = new StubMessageHandler();

            handler.QueueStringResponse(":");

            var evt = new EventSource(new Configuration(_uri, handler, readTimeout: _defaultReadTimeout));

            evt.CommentReceived += (_, e) => { evt.Close(); };
            await evt.StartAsync();

            var request = handler.GetRequests().First();

            Assert.Equal(HttpMethod.Get, request.Method);
        }
Exemplo n.º 12
0
        public async Task HTTP_request_body_can_be_specified()
        {
            var handler = new StubMessageHandler();

            handler.QueueStringResponse(":");

            HttpContent content = new StringContent("{}");

            Configuration.HttpContentFactory contentFn = () =>
            {
                return(content);
            };

            var evt = new EventSource(new Configuration(_uri, handler, readTimeout: _defaultReadTimeout,
                                                        method: HttpMethod.Post, requestBodyFactory: contentFn));

            evt.CommentReceived += (_, e) => { evt.Close(); };
            await evt.StartAsync();

            var request = handler.GetRequests().First();

            Assert.Equal(content, request.Content);
        }
Exemplo n.º 13
0
        public async Task When_response_exceeds_read_timeout_then_read_timeout_exception_occurs()
        {
            var commentSent = ":";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(commentSent);

            TimeSpan readTimeout = TimeSpan.FromSeconds(4);
            TimeSpan timeout     = readTimeout.Add(TimeSpan.FromSeconds(1));

            var evt = new StubEventSource(new Configuration(_uri, handler, readTimeout: readTimeout), (int)timeout.TotalMilliseconds);

            var receiver = new ErrorReceiver(evt);

            receiver.CloseEventSourceOnError = true;

            await evt.StartAsync();

            Assert.NotNull(receiver.ErrorReceived);
            Assert.Contains(receiver.ErrorReceived.Message, Resources.EventSourceService_Read_Timeout);
            Assert.Equal(ReadyState.Closed, receiver.SourceStateReceived);
            Assert.Equal(ReadyState.Shutdown, evt.ReadyState);
        }