public void RetryDelayDurationsShouldIncrease()
        {
            var handler = new StubMessageHandler();

            var nAttempts = 3;

            for (var i = 0; i < nAttempts; i++)
            {
                handler.QueueResponse(StubResponse.StartStream(
                                          StreamAction.Write(":hi\n"),
                                          StreamAction.CloseStream()));
            }
            handler.QueueResponse(StubResponse.StartStream());

            var backoffs = new List <TimeSpan>();

            using (var es = MakeEventSource(handler, builder => builder.InitialRetryDelay(TimeSpan.FromMilliseconds(100))))
            {
                _          = new EventSink(es, _testLogging);
                es.Closed += (_, state) =>
                {
                    backoffs.Add(es.BackOffDelay);
                };

                _ = Task.Run(es.StartAsync);

                for (int i = 0; i <= nAttempts; i++)
                {
                    _ = handler.AwaitRequest();
                }
            }

            AssertBackoffsAlwaysIncrease(backoffs, nAttempts);
        }
        public void LastEventIdHeaderIsNotSetByDefault()
        {
            var handler = new StubMessageHandler(StubResponse.StartStream());

            using (var es = MakeEventSource(handler))
            {
                _ = Task.Run(es.StartAsync);

                var req = handler.AwaitRequest();
                Assert.False(req.Headers.Contains(Constants.LastEventIdHttpHeader));
            }
        }
        public void CustomMethod()
        {
            var handler = new StubMessageHandler(StubResponse.StartStream());

            using (var es = MakeEventSource(handler, builder => builder.Method(HttpMethod.Post)))
            {
                _ = Task.Run(es.StartAsync);

                var req = handler.AwaitRequest();
                Assert.Equal(HttpMethod.Post, req.Method);
            }
        }
        public void DefaultMethod()
        {
            var handler = new StubMessageHandler(StubResponse.StartStream());

            using (var es = MakeEventSource(handler))
            {
                _ = Task.Run(es.StartAsync);

                var req = handler.AwaitRequest();
                Assert.Equal(HttpMethod.Get, req.Method);
            }
        }
        public void LastEventIdHeaderIsSetIfConfigured()
        {
            var handler     = new StubMessageHandler(StubResponse.StartStream());
            var lastEventId = "abc123";

            using (var es = MakeEventSource(handler, builder => builder.LastEventId(lastEventId)))
            {
                _ = Task.Run(es.StartAsync);

                var req = handler.AwaitRequest();
                Assert.True(req.Headers.Contains(Constants.LastEventIdHttpHeader));
                Assert.Contains(lastEventId, req.Headers.GetValues(Constants.LastEventIdHttpHeader));
            }
        }
        public void SendMostRecentEventIdOnReconnect()
        {
            var initialEventId = "abc123";
            var eventId1       = "xyz456";
            var message1       = new MessageEvent("put", "this is a test message", eventId1, _uri);

            var handler = new StubMessageHandler();

            handler.QueueResponse(StubResponse.StartStream(StreamAction.Write(message1), StreamAction.CloseStream()));
            handler.QueueResponse(StubResponse.StartStream());

            using (var es = MakeEventSource(handler, builder => builder.InitialRetryDelay(TimeSpan.FromMilliseconds(20))
                                            .LastEventId(initialEventId)))
            {
                var eventSink = new EventSink(es, _testLogging);
                _ = Task.Run(es.StartAsync);

                var req1 = handler.AwaitRequest();
                Assert.Contains(initialEventId, req1.Headers.GetValues("Last-Event-Id"));

                var req2 = handler.AwaitRequest();
                Assert.Contains(eventId1, req2.Headers.GetValues("Last-Event-Id"));
            }
        }
        public void CustomMethodWithRequestBody()
        {
            var handler = new StubMessageHandler(StubResponse.StartStream());

            HttpContent        content   = new StringContent("{}");
            Func <HttpContent> contentFn = () => content;

            using (var es = MakeEventSource(handler, builder =>
                                            builder.Method(HttpMethod.Post).RequestBodyFactory(contentFn)))
            {
                _ = Task.Run(es.StartAsync);

                var req = handler.AwaitRequest();
                Assert.Equal(content, req.Content);
            }
        }
        public void AcceptHeaderIsAlwaysPresent()
        {
            var handler = new StubMessageHandler(StubResponse.StartStream());

            HttpContent        content   = new StringContent("{}");
            Func <HttpContent> contentFn = () => content;

            using (var es = MakeEventSource(handler, builder =>
                                            builder.Method(HttpMethod.Post).RequestBodyFactory(contentFn)))
            {
                _ = Task.Run(es.StartAsync);

                var req = handler.AwaitRequest();
                Assert.True(req.Headers.Contains(Constants.AcceptHttpHeader));
                Assert.Contains(Constants.EventStreamContentType, req.Headers.GetValues(Constants.AcceptHttpHeader));
            }
        }
        public void CustomRequestHeaders()
        {
            var handler = new StubMessageHandler();

            handler.QueueResponse(StubResponse.StartStream());

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

            using (var es = MakeEventSource(handler, builder => builder.RequestHeaders(headers)))
            {
                _ = Task.Run(es.StartAsync);

                var req = handler.AwaitRequest();
                Assert.True(headers.All(
                                item =>
                                req.Headers.Contains(item.Key) &&
                                req.Headers.GetValues(item.Key).Contains(item.Value)
                                ));
            }
        }