示例#1
0
        public async Task FormatterThrowsOnReadWhenOverridenCreateReturnsNull()
        {
            // Arrange
            TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter
            {
                ReturnNullOncreate = true,
            };

            byte[]       array        = Encoding.UTF8.GetBytes("foo");
            MemoryStream memoryStream = new MemoryStream(array);

            HttpContent content = new StringContent("foo");

            // Act & Assert
            Func <Task> action = () => formatter.ReadFromStreamAsync(typeof(SampleType), memoryStream, content, null);
            await Assert.ThrowsAsync <InvalidOperationException>(action, "The 'CreateJsonReader' method returned null. It must return a JSON reader instance.");

            Assert.True(formatter.WasCreateJsonReaderCalled);
            Assert.False(formatter.WasCreateJsonWriterCalled);
        }
示例#2
0
        public async Task FormatterThrowsOnReadWhenOverridenCreateFails()
        {
            // Arrange
            TestBsonMediaTypeFormatter formatter = new TestBsonMediaTypeFormatter
            {
                ThrowExceptionOnCreate = true,
            };

            byte[]       array        = Encoding.UTF8.GetBytes("foo");
            MemoryStream memoryStream = new MemoryStream(array);

            HttpContent content = new StringContent("foo");

            // Act & Assert
            Func <Task> action = () => formatter.ReadFromStreamAsync(typeof(SampleType), memoryStream, content, null);
            await Assert.ThrowsAsync <Exception>(action, "Throwing exception directly, since it needs to get caught by a catch all");

            Assert.True(formatter.WasCreateJsonReaderCalled);
            Assert.False(formatter.WasCreateJsonWriterCalled);
        }