public void ExecuteResultSetsContentType()
        {
            var mockHttpResponse = new Mock<HttpResponseBase>();
            var mockHttpContext = new Mock<HttpContextBase>();
            mockHttpContext.Setup(c => c.Response).Returns(mockHttpResponse.Object);

            var mockController = new Mock<Controller>();
            var controllerContext = new ControllerContext(new RequestContext(mockHttpContext.Object, new RouteData()), mockController.Object);
            var result = new ContentResult { Content = "blah blah" };
            var filterContext = new ResultExecutingContext(controllerContext, result);

            var filter = new ContentTypeAttribute("text/xml");
            filter.OnResultExecuting(filterContext);

            mockHttpResponse.VerifySet(r => r.ContentType = "text/xml");
        }
Пример #2
0
        public static string GetContentType(this HttpClient httpClient, Type type)
        {
            string result = string.Concat("application/", TestConfiguration.TestData.RestAPI.ContentType);

            if (type != null)
            {
                object[] attributes = type.GetCustomAttributes(false);
                foreach (object attribute in attributes)
                {
                    if (attribute is ContentTypeAttribute)
                    {
                        ContentTypeAttribute contentType = (ContentTypeAttribute)attribute;
                        result = string.Concat(contentType.ContentType, "+", TestConfiguration.TestData.RestAPI.ContentType);
                        break;
                    }
                }
            }
            return(result);
        }
        public void GetApiDescription_IncludesResponseFormats_FilteredByType()
        {
            // Arrange
            var action = CreateActionDescriptor(nameof(ReturnsObject));
            var filter = new ContentTypeAttribute("text/*")
            {
                Type = typeof(Order)
            };

            action.FilterDescriptors = new List<FilterDescriptor>();
            action.FilterDescriptors.Add(new FilterDescriptor(filter, FilterScope.Action));

            var formatters = CreateFormatters();

            // This will just format Order
            formatters[0].SupportedTypes.Add(typeof(Order));

            // This will just format Product
            formatters[1].SupportedTypes.Add(typeof(Product));

            // Act
            var descriptions = GetApiDescriptions(action, formatters);

            // Assert
            var description = Assert.Single(descriptions);
            Assert.Equal(1, description.SupportedResponseFormats.Count);
            Assert.Equal(typeof(Order), description.ResponseType);
            Assert.NotNull(description.ResponseModelMetadata);

            var formats = description.SupportedResponseFormats;
            Assert.Single(formats, f => f.MediaType.ToString() == "text/json");
            Assert.Same(formatters[0], formats[0].Formatter);
        }
        public void GetApiDescription_PopulatesResponseInformation_WhenSetByFilter(string methodName)
        {
            // Arrange
            var action = CreateActionDescriptor(methodName);
            var filter = new ContentTypeAttribute("text/*")
            {
                Type = typeof(Order)
            };

            action.FilterDescriptors = new List<FilterDescriptor>();
            action.FilterDescriptors.Add(new FilterDescriptor(filter, FilterScope.Action));

            // Act
            var descriptions = GetApiDescriptions(action);

            // Assert
            var description = Assert.Single(descriptions);
            Assert.Equal(typeof(Order), description.ResponseType);
            Assert.NotNull(description.ResponseModelMetadata);
        }
 public void ContentTypeSetInCtor()
 {
     var attr = new ContentTypeAttribute("text/html");
     Assert.Equal("text/html", attr.ContentType);
 }