Beispiel #1
0
        public void ReconnectAfterHttpError()
        {
            HttpStatusCode error1 = HttpStatusCode.BadRequest, error2 = HttpStatusCode.InternalServerError;
            var            message = new MessageEvent("put", "hello", _uri);

            var handler = Handlers.Sequential(
                Handlers.Status((int)error1),
                Handlers.Status((int)error2),
                StartStream().Then(WriteEvent(message)).Then(LeaveStreamOpen())
                );

            WithServerAndEventSource(handler, c => c.InitialRetryDelay(TimeSpan.FromMilliseconds(20)), (server, es) =>
            {
                var eventSink = new EventSink(es, _testLogging);
                _             = Task.Run(es.StartAsync);

                var action1 = eventSink.ExpectAction();
                var ex1     = Assert.IsType <EventSourceServiceUnsuccessfulResponseException>(action1.Exception);
                Assert.Equal((int)error1, ex1.StatusCode);

                eventSink.ExpectActions(EventSink.ClosedAction());

                var action2 = eventSink.ExpectAction();
                var ex2     = Assert.IsType <EventSourceServiceUnsuccessfulResponseException>(action2.Exception);
                Assert.Equal((int)error2, ex2.StatusCode);

                eventSink.ExpectActions(
                    EventSink.ClosedAction(),
                    EventSink.OpenedAction(),
                    EventSink.MessageReceivedAction(message)
                    );
            });
        }
        public void ReconnectAfterHttpError()
        {
            HttpStatusCode error1 = HttpStatusCode.BadRequest, error2 = HttpStatusCode.InternalServerError;
            var            message = new MessageEvent("put", "hello", _uri);

            var handler = new StubMessageHandler();

            handler.QueueResponse(StubResponse.WithStatus(error1));
            handler.QueueResponse(StubResponse.WithStatus(error2));
            handler.QueueResponse(StubResponse.StartStream(StreamAction.Write(message)));

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

                var action1 = eventSink.ExpectAction();
                var ex1     = Assert.IsType <EventSourceServiceUnsuccessfulResponseException>(action1.Exception);
                Assert.Equal((int)error1, ex1.StatusCode);

                eventSink.ExpectActions(EventSink.ClosedAction());

                var action2 = eventSink.ExpectAction();
                var ex2     = Assert.IsType <EventSourceServiceUnsuccessfulResponseException>(action2.Exception);
                Assert.Equal((int)error2, ex2.StatusCode);

                eventSink.ExpectActions(
                    EventSink.ClosedAction(),
                    EventSink.OpenedAction(),
                    EventSink.MessageReceivedAction(message)
                    );
            }
        }
        public void ErrorForInvalidHttpStatus(HttpStatusCode statusCode)
        {
            using (var server = HttpServer.Start(Handlers.Status(statusCode)))
            {
                using (var es = MakeEventSource(server.Uri))
                {
                    var eventSink = new EventSink(es, _testLogging);
                    _ = Task.Run(es.StartAsync);

                    var errorAction = eventSink.ExpectAction();
                    var ex          = Assert.IsType <EventSourceServiceUnsuccessfulResponseException>(errorAction.Exception);
                    Assert.Equal((int)statusCode, ex.StatusCode);
                }
            }
        }
        public void ErrorForInvalidHttpStatus(HttpStatusCode statusCode)
        {
            var handler  = new StubMessageHandler();
            var response = new HttpResponseMessage(statusCode);

            handler.QueueResponse(StubResponse.WithResponse(response));

            using (var es = MakeEventSource(handler))
            {
                var eventSink = new EventSink(es, _testLogging);
                _ = Task.Run(es.StartAsync);

                var errorAction = eventSink.ExpectAction();
                var ex          = Assert.IsType <EventSourceServiceUnsuccessfulResponseException>(errorAction.Exception);
                Assert.Equal((int)statusCode, ex.StatusCode);
            }
        }
        public void ErrorForIncorrectContentType()
        {
            var response = Handlers.Status(200).Then(Handlers.BodyString("text/html", "testing"));

            using (var server = HttpServer.Start(response))
            {
                using (var es = MakeEventSource(server.Uri))
                {
                    var eventSink = new EventSink(es, _testLogging);
                    _ = Task.Run(es.StartAsync);

                    var errorAction = eventSink.ExpectAction();
                    var ex          = Assert.IsType <EventSourceServiceCancelledException>(errorAction.Exception);
                    Assert.Matches(".*content type.*text/html", ex.Message);
                }
            }
        }
        public void ErrorForIncorrectContentType()
        {
            var handler = new StubMessageHandler();

            var response =
                new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("testing", System.Text.Encoding.UTF8)
            };

            response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");

            handler.QueueResponse(StubResponse.WithResponse(response));

            using (var es = MakeEventSource(handler))
            {
                var eventSink = new EventSink(es, _testLogging);
                _ = Task.Run(es.StartAsync);

                var errorAction = eventSink.ExpectAction();
                var ex          = Assert.IsType <EventSourceServiceCancelledException>(errorAction.Exception);
                Assert.Matches(".*content type.*text/html", ex.Message);
            }
        }