public void FakeRequest_Controller_HasRequest()
        {
            var controller = new TestController();

            controller.FakeRequest();

            Assert.NotNull(controller.Request);
        }
        public void FakeControllerContext_ControlerAndUri_DefaultRouteMapped()
        {
            var controller = new TestController();

            var context = controller.FakeControllerContext("http://www.sample.com");

            Assert.Equal("{controller}/{id}", context.RouteData.Route.RouteTemplate);
        }
        public void FakeControllerContext_ControllerAndUri_ContextIsNotNull()
        {
            var controller = new TestController();

            var context = controller.FakeControllerContext("http://www.sample.com");

            Assert.NotNull(context);
        }
        public void ControllerName_TestController_Test()
        {
            var controller = new TestController();

            var name = controller.ControllerName();

            Assert.Equal("Test", name);
        }
        public void TestInit()
        {
            _principal = new Mock<IPrincipal>();
            _httpActionDescriptor = new Mock<HttpActionDescriptor>();

            _controller = new TestController();
            _httpConfiguration = new HttpConfiguration();
            _httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/test");
            _httpRoute = _httpConfiguration.Routes.MapHttpRoute("test", "api/{controller}/{id}");
            _httpRouteData = new HttpRouteData(_httpRoute, new HttpRouteValueDictionary { { "controller", "test" } });

            _controller.ControllerContext = new HttpControllerContext(_httpConfiguration, _httpRouteData, _httpRequestMessage);
            _controller.Request = _httpRequestMessage;
            _controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = _httpConfiguration;

            _httpActionContext = new HttpActionContext(_controller.ControllerContext, _httpActionDescriptor.Object);
        }
Ejemplo n.º 6
0
        public void Validate_DoesNotThrow_ForValidModels()
        {
            // Arrange
            HttpConfiguration configuration = new HttpConfiguration();
            TestController controller = new TestController { Configuration = configuration };
            TestEntity entity = new TestEntity { ID = 42 };

            // Act && Assert
            Assert.DoesNotThrow(() => controller.Validate(entity));
        }
Ejemplo n.º 7
0
        public void Validate_SetsModelStateErrorsUnderRightPrefix_ForInvalidModels()
        {
            // Arrange
            HttpConfiguration configuration = new HttpConfiguration();
            TestController controller = new TestController { Configuration = configuration };
            TestEntity entity = new TestEntity { ID = -1 };

            // Act
            controller.Validate(entity, keyPrefix: "prefix");

            // Assert
            Assert.False(controller.ModelState.IsValid);
            Assert.Equal("The field ID must be between 0 and 100.",
                controller.ModelState["prefix.ID"].Errors[0].ErrorMessage);
        }
Ejemplo n.º 8
0
        public void Validate_CallsValidateOnConfiguredValidator_UsingConfiguredMetadataProvider()
        {
            // Arrange
            Mock<IBodyModelValidator> validator = new Mock<IBodyModelValidator>();
            Mock<ModelMetadataProvider> metadataProvider = new Mock<ModelMetadataProvider>();

            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Services.Replace(typeof(IBodyModelValidator), validator.Object);
            configuration.Services.Replace(typeof(ModelMetadataProvider), metadataProvider.Object);

            TestController controller = new TestController { Configuration = configuration };
            TestEntity entity = new TestEntity { ID = 42 };

            // Act
            controller.Validate(entity);

            // Assert
            validator.Verify(
                v => v.Validate(entity, typeof(TestEntity), metadataProvider.Object, controller.ActionContext, String.Empty),
                Times.Once());
            Assert.True(controller.ModelState.IsValid);
        }
Ejemplo n.º 9
0
        public void Validate_DoesNothing_IfValidatorIsNull()
        {
            // Arrange
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Services.Replace(typeof(IBodyModelValidator), null);
            TestEntity entity = new TestEntity { ID = 9999999 };

            TestController controller = new TestController { Configuration = configuration };

            // Act
            controller.Validate(entity);

            // Assert
            Assert.True(controller.ModelState.IsValid);
        }
Ejemplo n.º 10
0
        public void Validate_ThrowsInvalidOperationException_IfConfigurationIsNull()
        {
            // Arrange
            TestController controller = new TestController();
            TestEntity entity = new TestEntity();

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => controller.Validate(entity),
                "ApiController.Configuration must not be null.");
        }
        public void FakeRequest_Controller_RequestPropertyRouteDataKeySet()
        {
            var controller = new TestController();

            controller.FakeRequest();

            Assert.Equal(controller.ControllerContext.RouteData, controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey]);
        }
        public void FakeRequest_Controller_RequestPropertyConfigurationKeySet()
        {
            var controller = new TestController();

            controller.FakeRequest();

            Assert.Equal(controller.ControllerContext.Configuration, controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey]);
        }
        public void Property_EntityIdHeader_IsEvaluatedLazilyAndOnlyOnce()
        {
            // Arrange
            Uri idLink = new Uri("http://id-link");
            Mock<NavigationSourceLinkBuilderAnnotation> linkBuilder = new Mock<NavigationSourceLinkBuilderAnnotation>();
            linkBuilder.CallBase = true;
            linkBuilder.Setup(b => b.BuildIdLink(It.IsAny<EntityInstanceContext>(), ODataMetadataLevel.FullMetadata))
                .Returns(idLink);

            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity)));
            model.Model.SetNavigationSourceLinkBuilder(model.Customers, linkBuilder.Object);
            ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers));
            HttpRequestMessage request = new HttpRequestMessage();
            request.ODataProperties().Model = model.Model;
            request.ODataProperties().Path = path;
            TestController controller = new TestController { Configuration = new HttpConfiguration() };
            CreatedODataResult<TestEntity> createdODataResult = new CreatedODataResult<TestEntity>(_entity, controller);

            // Act
            controller.Request = request;
            Uri entityIdHeader = createdODataResult.EntityId;

            // Assert
            Assert.Same(idLink, entityIdHeader);
            linkBuilder.Verify(
                b => b.BuildIdLink(It.IsAny<EntityInstanceContext>(), ODataMetadataLevel.FullMetadata),
                Times.Once());
        }
        public void Property_LocationHeader_IsEvaluatedOnlyOnce()
        {
            // Arrange
            Uri editLink = new Uri("http://edit-link");
            Mock<NavigationSourceLinkBuilderAnnotation> linkBuilder = new Mock<NavigationSourceLinkBuilderAnnotation>();
            linkBuilder.Setup(b => b.BuildEditLink(It.IsAny<EntityInstanceContext>(), ODataMetadataLevel.FullMetadata, null))
                .Returns(editLink);

            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity)));
            model.Model.SetNavigationSourceLinkBuilder(model.Customers, linkBuilder.Object);
            ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers));
            HttpRequestMessage request = new HttpRequestMessage();
            request.ODataProperties().Model = model.Model;
            request.ODataProperties().Path = path;
            TestController controller = new TestController { Request = request, Configuration = new HttpConfiguration() };
            CreatedODataResult<TestEntity> createdODataResult = new CreatedODataResult<TestEntity>(_entity, controller);

            // Act
            Uri locationHeader = createdODataResult.LocationHeader;

            // Assert
            linkBuilder.Verify(
                (b) => b.BuildEditLink(It.IsAny<EntityInstanceContext>(), ODataMetadataLevel.FullMetadata, null),
                Times.Once());
        }
Ejemplo n.º 15
0
 public void SetUp()
 {
     _exceptionFilter = new DPE.ExceptionFilter.ExceptionFilter();
     _testController = new TestController();
     _uri = "http://localhost/someUri/mogul";
 }