public async Task Given_content_type_not_equal_to_eventstream_when_the_http_response_is_received_then_error_event_should_occur()
        {
            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));

            var config = new Configuration(_uri, handler);

            var evt      = new EventSource(config);
            var receiver = new ErrorReceiver();

            evt.Error += receiver;
            evt.Error += (_, e) => evt.Close();

            await evt.StartAsync();

            Assert.NotNull(receiver.ErrorReceived);
            Assert.Equal(ReadyState.Closed, receiver.SourceStateReceived);
            Assert.Equal(ReadyState.Shutdown, evt.ReadyState);
        }
        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 async Task When_event_source_closes_do_not_dispose_configured_http_client()
        {
            var handler = new StubMessageHandler();

            handler.QueueResponse(StubResponse.WithResponse(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("Hello")
            }));

            var client = new HttpClient(handler);
            var evt    = new EventSource(new Configuration(_uri, httpClient: client));

            evt.Close();

            await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, _uri));

            client.Dispose();
        }
        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);
            }
        }