ReadFromStreamAsync() приватный Метод

private ReadFromStreamAsync ( Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger ) : Task
type Type
readStream System.IO.Stream
content System.Net.Http.HttpContent
formatterLogger IFormatterLogger
Результат Task
 public override Task <object> ReadFromStreamAsync(
     Type type,
     Stream readStream,
     HttpContent content,
     IFormatterLogger formatterLogger
     )
 {
     return(_innerTracer.ReadFromStreamAsync(type, readStream, content, formatterLogger));
 }
        public void OnReadFromStreamAsync_Traces_And_Throws_When_Inner_Throws()
        {
            // Arrange
            InvalidOperationException exception = new InvalidOperationException("test");
            Mock<MediaTypeFormatter> mockFormatter = new Mock<MediaTypeFormatter>() { CallBase = true };
            mockFormatter.Setup(
                f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), It.IsAny<HttpContentHeaders>(), It.IsAny<IFormatterLogger>())).Throws(exception);

            TestTraceWriter traceWriter = new TestTraceWriter();
            HttpRequestMessage request = new HttpRequestMessage();
            request.Content = new StringContent("");
            MediaTypeFormatterTracer tracer = new MediaTypeFormatterTracer(mockFormatter.Object, traceWriter, request);
            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ReadFromStreamAsync" },
                new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "ReadFromStreamAsync" }
            };

            // Act
            Exception thrown = Assert.Throws<InvalidOperationException>(() => tracer.ReadFromStreamAsync(typeof(string), new MemoryStream(), request.Content.Headers, null));

            // Assert
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
        public void OnReadFromStreamAsync_Traces()
        {
            // Arrange
            Mock<MediaTypeFormatter> mockFormatter = new Mock<MediaTypeFormatter>() { CallBase = true };
            mockFormatter.Setup(
                f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), It.IsAny<HttpContentHeaders>(), It.IsAny<IFormatterLogger>())).
                Returns(TaskHelpers.FromResult<object>("sampleValue"));
            TestTraceWriter traceWriter = new TestTraceWriter();
            HttpRequestMessage request = new HttpRequestMessage();
            request.Content = new StringContent("");
            MediaTypeFormatterTracer tracer = new MediaTypeFormatterTracer(mockFormatter.Object, traceWriter, request);
            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ReadFromStreamAsync" },
                new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "ReadFromStreamAsync" }
            };

            // Act
            Task<object> task = tracer.ReadFromStreamAsync(typeof(string), new MemoryStream(), request.Content.Headers, null);
            string result = task.Result as string;

            // Assert
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Equal("sampleValue", result);
        }
        public async Task ReadFromStreamAsync_LogErrorFromJsonRequestBody(IList <TraceRecord> expectedTraces,
                                                                          HttpRequestMessage request,
                                                                          string requestBody)
        {
            // Arrange
            var formatter = new JsonMediaTypeFormatter();

            formatter.UseDataContractJsonSerializer = false;
            HttpContent content = new StringContent(requestBody);

            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            var loggerMock = new Mock <IFormatterLogger>();

            loggerMock.Setup(l => l.LogError(It.IsAny <string>(), It.IsAny <Exception>()));
            TestTraceWriter traceWriter = new TestTraceWriter();
            var             tracer      = new MediaTypeFormatterTracer(formatter, traceWriter, request);

            // Act
            await tracer.ReadFromStreamAsync(typeof(SampleType),
                                             await content.ReadAsStreamAsync(),
                                             content, loggerMock.Object
                                             );

            // Assert
            // Error must always be marked as handled at ReadFromStream in BaseJsonMediaTypeFormatters,
            // so it would not propagate to here.
            // Note that regarding the exception's comparison in the record we only compare its message,
            // because we cannot get the exact exception and message would be enough for logging.
            Assert.Equal <TraceRecord>(expectedTraces,
                                       traceWriter.Traces,
                                       new TraceRecordComparer()
            {
                IgnoreExceptionReference = true
            });
        }
        public void OnReadFromStreamAsync_Traces_And_Faults_When_Inner_Faults()
        {
            // Arrange
            InvalidOperationException exception     = new InvalidOperationException("test");
            Mock <MediaTypeFormatter> mockFormatter = new Mock <MediaTypeFormatter>()
            {
                CallBase = true
            };
            TaskCompletionSource <object> tcs = new TaskCompletionSource <object>();

            tcs.TrySetException(exception);

            mockFormatter.Setup(
                f => f.ReadFromStreamAsync(It.IsAny <Type>(), It.IsAny <Stream>(), It.IsAny <HttpContentHeaders>(), It.IsAny <IFormatterLogger>())).
            Returns(tcs.Task);
            TestTraceWriter    traceWriter = new TestTraceWriter();
            HttpRequestMessage request     = new HttpRequestMessage();

            request.Content = new StringContent("");
            MediaTypeFormatterTracer tracer = new MediaTypeFormatterTracer(mockFormatter.Object, traceWriter, request);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin, Operation = "ReadFromStreamAsync"
                },
                new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Error)
                {
                    Kind = TraceKind.End, Operation = "ReadFromStreamAsync"
                }
            };

            // Act
            Task <object> task = tracer.ReadFromStreamAsync(typeof(string), new MemoryStream(), request.Content.Headers, null);

            // Assert
            Exception thrown = Assert.Throws <InvalidOperationException>(() => task.Wait());

            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
        public void OnReadFromStreamAsync_Traces()
        {
            // Arrange
            Mock <MediaTypeFormatter> mockFormatter = new Mock <MediaTypeFormatter>()
            {
                CallBase = true
            };

            mockFormatter.Setup(
                f => f.ReadFromStreamAsync(It.IsAny <Type>(), It.IsAny <Stream>(), It.IsAny <HttpContentHeaders>(), It.IsAny <IFormatterLogger>())).
            Returns(TaskHelpers.FromResult <object>("sampleValue"));
            TestTraceWriter    traceWriter = new TestTraceWriter();
            HttpRequestMessage request     = new HttpRequestMessage();

            request.Content = new StringContent("");
            MediaTypeFormatterTracer tracer = new MediaTypeFormatterTracer(mockFormatter.Object, traceWriter, request);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin, Operation = "ReadFromStreamAsync"
                },
                new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.End, Operation = "ReadFromStreamAsync"
                }
            };

            // Act
            Task <object> task   = tracer.ReadFromStreamAsync(typeof(string), new MemoryStream(), request.Content.Headers, null);
            string        result = task.Result as string;

            // Assert
            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Equal("sampleValue", result);
        }