public void Property_LocationHeader_IsEvaluatedOnlyOnce()
        {
            // Arrange
            Uri editLink = new Uri("http://edit-link");
            Mock <EntitySetLinkBuilderAnnotation> linkBuilder = new Mock <EntitySetLinkBuilderAnnotation>();

            linkBuilder.Setup(b => b.BuildEditLink(It.IsAny <EntityInstanceContext>(), ODataMetadataLevel.Default, null))
            .Returns(editLink);

            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity)));
            model.Model.SetEntitySetLinkBuilder(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.Default, null),
                Times.Once());
        }
        public void Property_ContentNegotiator_DirectDependency()
        {
            CreatedODataResult <TestEntity> result = new CreatedODataResult <TestEntity>(
                _entity, _contentNegotiator, _request, _formatters, _locationHeader);

            Assert.Same(_contentNegotiator, result.ContentNegotiator);
        }
        public void GenerateLocationHeader_UsesEntitySetLinkBuilder_ToGenerateLocationHeader()
        {
            // Arrange
            string idLink   = "http://id-link";
            Uri    editLink = new Uri(idLink);
            Mock <EntitySetLinkBuilderAnnotation> linkBuilder = new Mock <EntitySetLinkBuilderAnnotation>();

            linkBuilder.Setup(b => b.BuildIdLink(It.IsAny <EntityInstanceContext>(), ODataMetadataLevel.Default))
            .Returns(idLink);
            linkBuilder.Setup(b => b.BuildEditLink(It.IsAny <EntityInstanceContext>(), ODataMetadataLevel.Default, idLink))
            .Returns(editLink);

            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity)));
            model.Model.SetEntitySetLinkBuilder(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;
            CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request);

            // Act
            var locationHeader = createdODataResult.GenerateLocationHeader();

            // Assert
            Assert.Same(editLink, locationHeader);
        }
        public void GenerateLocationHeader_ThrowsODataPathMissing_IfRequestDoesNotHaveODataPath()
        {
            HttpRequestMessage request = new HttpRequestMessage();

            request.ODataProperties().Model = EdmCoreModel.Instance;
            CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
                                                      "The operation cannot be completed because no ODataPath is available for the request.");
        }
Ejemplo n.º 5
0
        public void GenerateLocationHeader_ThrowsRequestMustHaveModel_IfRequestDoesNotHaveModel()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();
            CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
                                                      "The request must have an associated EDM model. Consider using the extension method " +
                                                      "HttpConfiguration.Routes.MapODataRoute to register a route that parses the OData URI and attaches the model information.");
        }
        public void GenerateLocationHeader_ThrowsEntitySetMissingDuringSerialization_IfODataPathEntitySetIsNull()
        {
            ODataPath          path    = new ODataPath();
            HttpRequestMessage request = new HttpRequestMessage();

            request.ODataProperties().Model = EdmCoreModel.Instance;
            request.ODataProperties().Path  = path;
            CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
                                                      "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.");
        }
        public void GenerateLocationHeader_ThrowsEntityTypeNotInModel_IfContentTypeIsNotThereInModel()
        {
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            ODataPath          path             = new ODataPath(new EntitySetPathSegment(model.Customers));
            HttpRequestMessage request          = new HttpRequestMessage();

            request.ODataProperties().Model = model.Model;
            request.ODataProperties().Path  = path;
            CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
                                                      "Cannot find the entity type 'System.Web.Http.OData.Results.CreatedODataResultTest+TestEntity' in the model.");
        }
        public void GenerateLocationHeader_ThrowsTypeMustBeEntity_IfMappingTypeIsNotEntity()
        {
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            ODataPath          path             = new ODataPath(new EntitySetPathSegment(model.Customers));
            HttpRequestMessage request          = new HttpRequestMessage();

            request.ODataProperties().Model = model.Model;
            request.ODataProperties().Path  = path;
            model.Model.SetAnnotationValue(model.Address, new ClrTypeAnnotation(typeof(TestEntity)));
            CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
                                                      "NS.Address is not an entity type. Only entity types are supported.");
        }
        public void GetInnerActionResult_ReturnsNoContentStatusCodeResult_IfRequestAsksForNoContent()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();

            request.Headers.TryAddWithoutValidation("Prefer", "return-no-content");
            CreatedODataResult <TestEntity> createdODataResult =
                new CreatedODataResult <TestEntity>(_entity, _contentNegotiator, request, _formatters, _locationHeader);

            // Act
            IHttpActionResult result = createdODataResult.GetInnerActionResult();

            // Assert
            StatusCodeResult statusCodeResult = Assert.IsType <StatusCodeResult>(result);

            Assert.Equal(HttpStatusCode.NoContent, statusCodeResult.StatusCode);
            Assert.Same(request, statusCodeResult.Request);
        }
        public void GenerateLocationHeader_ThrowsEditLinkNullForLocationHeader_IfEntitySetLinkBuilderReturnsNull()
        {
            // Arrange
            Mock <EntitySetLinkBuilderAnnotation> linkBuilder = new Mock <EntitySetLinkBuilderAnnotation>();
            CustomersModelWithInheritance         model       = new CustomersModelWithInheritance();

            model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity)));
            model.Model.SetEntitySetLinkBuilder(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;
            CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request);

            // Act
            Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
                                                      "The edit link builder for the entity set 'Customers' returned null. An edit link is required for the location header.");
        }
        public void GetInnerActionResult_ReturnsNegotiatedContentResult_IfRequestHasNoPreferenceHeader()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();
            CreatedODataResult <TestEntity> createdODataResult =
                new CreatedODataResult <TestEntity>(_entity, _contentNegotiator, request, _formatters, _locationHeader);

            // Act
            IHttpActionResult result = createdODataResult.GetInnerActionResult();

            // Assert
            NegotiatedContentResult <TestEntity> negotiatedResult = Assert.IsType <NegotiatedContentResult <TestEntity> >(result);

            Assert.Equal(HttpStatusCode.Created, negotiatedResult.StatusCode);
            Assert.Same(request, negotiatedResult.Request);
            Assert.Same(_contentNegotiator, negotiatedResult.ContentNegotiator);
            Assert.Same(_entity, negotiatedResult.Content);
            Assert.Same(_formatters, negotiatedResult.Formatters);
        }