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 Uri editLink = new Uri("http://id-link"); Mock <NavigationSourceLinkBuilderAnnotation> linkBuilder = new Mock <NavigationSourceLinkBuilderAnnotation>(); linkBuilder.CallBase = true; linkBuilder.Setup( b => b.BuildEditLink(It.IsAny <ResourceContext>(), 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 EntitySetSegment(model.Customers)); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Path = path; request.EnableHttpDependencyInjectionSupport(model.Model); CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request); // Act var locationHeader = createdODataResult.GenerateLocationHeader(); // Assert Assert.Same(editLink, locationHeader); }
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 <ResourceContext>(), 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 EntitySetSegment(model.Customers)); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Path = path; request.EnableHttpDependencyInjectionSupport(model.Model); TestController controller = new TestController { Configuration = request.GetConfiguration() }; 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 <ResourceContext>(), ODataMetadataLevel.FullMetadata), Times.Once()); }
public void GenerateLocationHeader_ForContainment() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.OrderLine, new ClrTypeAnnotation(typeof(OrderLine))); var path = new DefaultODataPathHandler().Parse( model.Model, "http://localhost/", "MyOrders(1)/OrderLines"); var request = GetODataRequest(model.Model); request.ODataProperties().Path = path; var orderLine = new OrderLine { ID = 2 }; var createdODataResult = new CreatedODataResult <OrderLine>( orderLine, _contentNegotiator, request, _formatters, _locationHeader); // Act var locationHeader = createdODataResult.GenerateLocationHeader(); // Assert Assert.Equal("http://localhost/MyOrders(1)/OrderLines(2)", locationHeader.ToString()); }
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()); }
public void GenerateLocationHeader_ThrowsODataPathMissing_IfRequestDoesNotHaveODataPath() { HttpRequestMessage request = new HttpRequestMessage(); request.EnableHttpDependencyInjectionSupport(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."); }
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.MapODataServiceRoute 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 or singleton cannot be found from the OData path. The related entity set or singleton 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.OData.Results.CreatedODataResultTest+TestEntity' in the model."); }
public void GenerateLocationHeader_ThrowsTypeMustBeEntity_IfMappingTypeIsNotEntity() { CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPath path = new ODataPath(new EntitySetSegment(model.Customers)); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Path = path; request.EnableHttpDependencyInjectionSupport(model.Model); 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=minimal"); 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 <NavigationSourceLinkBuilderAnnotation> linkBuilder = new Mock <NavigationSourceLinkBuilderAnnotation>(); 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 EntitySetSegment(model.Customers)); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Path = path; request.EnableHttpDependencyInjectionSupport(model.Model); 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); }
public void GetInnerActionResult_ReturnsNegotiatedContentResult_IfRequestAsksForContent() { // Arrange HttpRequestMessage request = new HttpRequestMessage(); request.Headers.TryAddWithoutValidation("Prefer", "return=representation"); 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); }