Пример #1
0
        public Task ReadFromStreamAsync_WhenContentLengthIsNull_ReadsDataButDoesNotCloseStream()
        {
            // Arrange
            TFormatter         formatter      = CreateFormatter();
            MemoryStream       memStream      = new MemoryStream(ExpectedSampleTypeByteRepresentation);
            HttpContent        content        = new StringContent(String.Empty);
            HttpContentHeaders contentHeaders = content.Headers;

            contentHeaders.ContentLength = null;
            contentHeaders.ContentType   = ExpectedSupportedMediaTypes.First();

            // Act
            return(formatter.ReadFromStreamAsync(typeof(SampleType), memStream, content, null).ContinueWith(
                       readTask =>
            {
                // Assert
                Assert.Equal(TaskStatus.RanToCompletion, readTask.Status);
                Assert.True(memStream.CanRead);

                var value = Assert.IsType <SampleType>(readTask.Result);
                Assert.Equal(42, value.Number);
            }));
        }
Пример #2
0
        public Task WriteToStreamAsync_WritesDataButDoesNotCloseStream()
        {
            // Arrange
            TFormatter formatter  = CreateFormatter();
            SampleType sampleType = new SampleType {
                Number = 42
            };
            MemoryStream memStream = new MemoryStream();
            HttpContent  content   = new StringContent(String.Empty);

            content.Headers.ContentType = ExpectedSupportedMediaTypes.First();

            // Act
            return(formatter.WriteToStreamAsync(typeof(SampleType), sampleType, memStream, content, null).ContinueWith(
                       writeTask =>
            {
                // Assert
                Assert.Equal(TaskStatus.RanToCompletion, writeTask.Status);
                Assert.True(memStream.CanRead);

                byte[] actualSampleTypeByteRepresentation = memStream.ToArray();
                Assert.NotEmpty(actualSampleTypeByteRepresentation);
            }));
        }
 protected virtual MediaTypeHeaderValue CreateSupportedMediaType()
 {
     return(ExpectedSupportedMediaTypes.First());
 }