コード例 #1
0
        public void ReadContentAsync_Throws_ForNoContentType()
        {
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost");

            request.Content = new StringContent("The quick, brown fox tripped and fell.");
            request.Content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatterCollection();
            var descriptor = new Mock <HttpParameterDescriptor>();

            descriptor.Setup(desc => desc.IsOptional).Returns(false);
            var binding = new FormatterParameterBinding(descriptor.Object, formatters, null);

            HttpResponseException exception = Assert.Throws <HttpResponseException>(
                () => binding.ReadContentAsync(request, typeof(string), formatters, null)
                );

            Assert.Equal(HttpStatusCode.UnsupportedMediaType, exception.Response.StatusCode);
            HttpError error;

            exception.Response.TryGetContentValue(out error);
            Assert.Equal(
                "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.",
                error.Message
                );
        }
コード例 #2
0
        public async Task ReadContentAsync_PassesCancellationToken_Further()
        {
            // Arrange
            var parameter = new Mock <HttpParameterDescriptor>();

            parameter.Setup(p => p.IsOptional).Returns(false);
            IBodyModelValidator validator = null;
            HttpRequestMessage  request   = new HttpRequestMessage();

            request.Content = new StringContent("");
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("app/test");
            IFormatterLogger        logger = null;
            CancellationTokenSource cts    = new CancellationTokenSource();

            Mock <MediaTypeFormatter> formatter = new Mock <MediaTypeFormatter>();

            formatter.Setup(f => f.CanReadType(typeof(int))).Returns(true);
            formatter.Object.SupportedMediaTypes.Add(request.Content.Headers.ContentType);
            formatter.Setup(f => f.ReadFromStreamAsync(typeof(int), It.IsAny <Stream>(), request.Content, logger, cts.Token))
            .Returns(Task.FromResult <object>(42))
            .Verifiable();

            var formatters = new[] { formatter.Object };
            FormatterParameterBinding binding = new FormatterParameterBinding(parameter.Object, formatters, validator);

            // Act
            await binding.ReadContentAsync(request, typeof(int), formatters, logger, cts.Token);

            // Assert
            formatter.Verify();
        }
コード例 #3
0
        public void ReadContentAsync_Throws_ForUnsupportedMediaType()
        {
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost");
            request.Content = new StringContent("The quick, brown fox tripped and fell.");
            var formatters = new MediaTypeFormatterCollection();
            var descriptor = new Mock<HttpParameterDescriptor>();
            descriptor.Setup(desc => desc.IsOptional).Returns(false);
            var binding = new FormatterParameterBinding(descriptor.Object, formatters, null);

            HttpResponseException exception = Assert.Throws<HttpResponseException>(
                () => binding.ReadContentAsync(request, typeof(string), formatters, null));

            Assert.Equal(HttpStatusCode.UnsupportedMediaType, exception.Response.StatusCode);
            HttpError error;
            exception.Response.TryGetContentValue(out error);
            Assert.Equal(
                "The request entity's media type 'text/plain' is not supported for this resource.",
                error.Message);
        }    
コード例 #4
0
        public void ReadContentAsync_PassesCancellationToken_Further()
        {
            // Arrange
            var parameter = new Mock<HttpParameterDescriptor>();
            parameter.Setup(p => p.IsOptional).Returns(false);
            IBodyModelValidator validator = null;
            HttpRequestMessage request = new HttpRequestMessage();
            request.Content = new StringContent("");
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("app/test");
            IFormatterLogger logger = null;
            CancellationTokenSource cts = new CancellationTokenSource();

            Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>();
            formatter.Setup(f => f.CanReadType(typeof(int))).Returns(true);
            formatter.Object.SupportedMediaTypes.Add(request.Content.Headers.ContentType);
            formatter.Setup(f => f.ReadFromStreamAsync(typeof(int), It.IsAny<Stream>(), request.Content, logger, cts.Token))
                .Returns(Task.FromResult<object>(42))
                .Verifiable();

            var formatters = new[] { formatter.Object };
            FormatterParameterBinding binding = new FormatterParameterBinding(parameter.Object, formatters, validator);

            // Act
            binding.ReadContentAsync(request, typeof(int), formatters, logger, cts.Token).Wait();

            // Assert
            formatter.Verify();
        }