Exemplo n.º 1
0
        public void SendAsync_ReturnsOriginalResponseIfObjectContentHasValue()
        {
            // Arrange
            Mock <MediaTypeFormatter> formatter = new Mock <MediaTypeFormatter>();

            formatter.Setup(f => f.CanWriteType(It.IsAny <Type>())).Returns(true);

            HttpResponseMessage originalResponse = new HttpResponseMessage(HttpStatusCode.OK);

            originalResponse.Content = new ObjectContent(typeof(string), "value", formatter.Object);

            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
            {
                InnerHandler = new TestMessageHandler(originalResponse)
            };

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/any");

            request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment("EntitySet"));

            // Act
            HttpResponseMessage response = handler.SendAsync(request).Result;

            // Assert
            Assert.Same(originalResponse, response);
        }
Exemplo n.º 2
0
        private Task <HttpResponse> SendToHandler(
            ODataNullValueMessageHandler handler,
            AspNetCore.Http.HttpRequest request)
        {
            var pageContext = new PageContext(new ActionContext(
                                                  request.HttpContext,
                                                  new RouteData(),
                                                  new PageActionDescriptor(),
                                                  new ModelStateDictionary()));

            var model = new Mock <PageModel>();

            var modelAsFilter = model.As <IAsyncResultFilter>();

            modelAsFilter
            .Setup(f => f.OnResultExecutionAsync(It.IsAny <ResultExecutingContext>(), It.IsAny <ResultExecutionDelegate>()))
            .Returns(Task.CompletedTask);

            var resultExecutingContext = new ResultExecutingContext(
                pageContext,
                Array.Empty <IFilterMetadata>(),
                new AspNetCore.Mvc.RazorPages.PageResult(),
                model.Object);

            handler.OnResultExecuting(resultExecutingContext);

            return(Task.FromResult(request.HttpContext.Response));
        }
Exemplo n.º 3
0
        public async Task SendAsync_ReturnsNotFoundForNullEntityResponse()
        {
            // Arrange
            Mock <MediaTypeFormatter> formatter = new Mock <MediaTypeFormatter>();

            formatter.Setup(f => f.CanWriteType(It.IsAny <Type>())).Returns(true);

            HttpResponseMessage originalResponse = new HttpResponseMessage(HttpStatusCode.OK);

            originalResponse.Content = new ObjectContent(typeof(string), null, formatter.Object);

            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
            {
                InnerHandler = new TestMessageHandler(originalResponse)
            };

            var                configuration = RoutingConfigurationFactory.Create();
            ODataPath          path          = new DefaultODataPathHandler().Parse(BuildModel(), "http://localhost/any", "Customers(3)");
            HttpRequestMessage request       = RequestFactory.Create(HttpMethod.Get, "http://localhost/any", configuration);

            request.ODataContext().Path = path;

            // Act
            HttpResponseMessage response = await handler.SendAsync(request);

            // Assert
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
Exemplo n.º 4
0
        public async Task SendAsync_ReturnsOriginalResponseIfRequestIsNotGet(string method)
        {
            // Arrange
            Mock <MediaTypeFormatter> formatter = new Mock <MediaTypeFormatter>();

            formatter.Setup(f => f.CanWriteType(It.IsAny <Type>())).Returns(true);

            HttpResponseMessage originalResponse = new HttpResponseMessage(HttpStatusCode.OK);

            originalResponse.Content = new ObjectContent(typeof(string), null, formatter.Object);

            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
            {
                InnerHandler = new TestMessageHandler(originalResponse)
            };

            HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), "http://localhost/any");

            request.ODataProperties().Path = new ODataPath(new EntitySetSegment(_entitySet));

            // Act
            HttpResponseMessage response = await handler.SendAsync(request);

            // Assert
            Assert.Same(originalResponse, response);
        }
Exemplo n.º 5
0
        public void SendAsync_ThrowsIfRequestIsNull()
        {
            // Arrange
            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler();

            // Act & Assert
            Assert.ThrowsArgumentNull(() => { HttpResponseMessage result = handler.SendAsync(null).Result; }, "request");
        }
Exemplo n.º 6
0
        private ODataNullValueMessageHandler CreateHandler(HttpResponseMessage originalResponse)
        {
            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler();

            handler.InnerHandler = new TestMessageHandler(originalResponse);

            return(handler);
        }
Exemplo n.º 7
0
        public void SendAsync_ThrowsIfContextIsNull()
        {
            // Arrange
            ODataNullValueMessageHandler handler = CreateHandler();

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => handler.OnResultExecuting(null), "context");
        }
Exemplo n.º 8
0
        public async Task SendAsync_ThrowsIfRequestIsNull()
        {
            // Arrange
            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler();

            // Act & Assert
            await ExceptionAssert.ThrowsArgumentNullAsync(() => handler.SendAsync(null), "request");
        }
        public void SendAsync_ThrowsIfRequestIsNull()
        {
            // Arrange
            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler();

            // Act & Assert
            Assert.ThrowsArgumentNull(() => { HttpResponseMessage result = handler.SendAsync(null).Result; }, "request");
        }
Exemplo n.º 10
0
        public async Task SendAsync_ReturnsNullIfResponseIsNull()
        {
            // Arrange
            ODataNullValueMessageHandler handler = CreateHandler(null);
            var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/any");

            request.ODataContext().Path = new ODataPath(new EntitySetSegment(_entitySet));

            // Act
            var response = await SendToHandler(handler, request);

            // Assert
            Assert.Null(response);
        }
Exemplo n.º 11
0
        private static void Configuration(IAppBuilder builder)
        {
            HttpConfiguration configuration = new HttpConfiguration();

            // ODataNullValueMessageHandler handle returning a 404 response instead of a 200 with a null value when the raw value
            // of a property is null.
            var nullHandler = new ODataNullValueMessageHandler
            {
                InnerHandler = new HttpControllerDispatcher(configuration)
            };

            configuration.MapODataServiceRoute("odata", "odata", ShoppingEdmModel.GetModel(), nullHandler);
            builder.UseWebApi(configuration);
        }
        public void SendAsync_ReturnsNullIfResponseIsNull()
        {
            // Arrange
            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
                {
                    InnerHandler = new TestMessageHandler(null)
                };
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/any");
            request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment("EntitySet"));

            // Act 
            HttpResponseMessage response = handler.SendAsync(request).Result;

            // Assert
            Assert.Null(response);
        }
Exemplo n.º 13
0
        public async Task SendAsync_ReturnsOriginalResponseIfNoObjectContent()
        {
            // Arrange
            var originalResponse = ResponseFactory.Create(HttpStatusCode.OK, "test");
            ODataNullValueMessageHandler handler = CreateHandler(originalResponse);

            var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/any");

            request.ODataContext().Path = new ODataPath(new EntitySetSegment(_entitySet));

            // Act
            var response = await SendToHandler(handler, request);

            // Assert
            Assert.Same(originalResponse, response);
        }
Exemplo n.º 14
0
        public void SendAsync_ReturnsNullIfResponseIsNull()
        {
            // Arrange
            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
            {
                InnerHandler = new TestMessageHandler(null)
            };
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/any");

            request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment("EntitySet"));

            // Act
            HttpResponseMessage response = handler.SendAsync(request).Result;

            // Assert
            Assert.Null(response);
        }
Exemplo n.º 15
0
        public void GetResponseStatusCode_ReturnsNoContentForProperties_AndNotFoundForEntities(string odataPath,
                                                                                               HttpStatusCode?expected)
        {
            // Arrange
            IEdmModel         model       = BuildModel();
            IODataPathHandler pathHandler = new DefaultODataPathHandler();
            ODataPath         path        = pathHandler.Parse(model, "http://localhost/any", odataPath);

            // Guard
            Assert.NotNull(path);

            // Act
            HttpStatusCode?statusCode = ODataNullValueMessageHandler.GetUpdatedResponseStatusCodeOrNull(path);

            // Assert
            Assert.Equal(expected, statusCode);
        }
        public void SendAsync_ReturnsOriginalResponseIfContentIsNull()
        {
            // Arrange
            HttpResponseMessage originalResponse = new HttpResponseMessage(HttpStatusCode.OK);

            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
            {
                InnerHandler = new TestMessageHandler(originalResponse)
            };

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/any");
            request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment("EntitySet"));

            // Act 
            HttpResponseMessage response = handler.SendAsync(request).Result;

            // Assert
            Assert.Same(originalResponse, response);
        }
Exemplo n.º 17
0
        public void SendAsync_ReturnsOriginalResponseIfContentIsNull()
        {
            // Arrange
            HttpResponseMessage originalResponse = new HttpResponseMessage(HttpStatusCode.OK);

            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
            {
                InnerHandler = new TestMessageHandler(originalResponse)
            };

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/any");

            request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment("EntitySet"));

            // Act
            HttpResponseMessage response = handler.SendAsync(request).Result;

            // Assert
            Assert.Same(originalResponse, response);
        }
Exemplo n.º 18
0
 private Task <HttpResponseMessage> SendToHandler(
     ODataNullValueMessageHandler handler,
     HttpRequestMessage request)
 {
     return(handler.SendAsync(request));
 }
        public void SendAsync_ReturnsOriginalResponseIfObjectContentHasValue()
        {
            // Arrange
            Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>();
            formatter.Setup(f => f.CanWriteType(It.IsAny<Type>())).Returns(true);

            HttpResponseMessage originalResponse = new HttpResponseMessage(HttpStatusCode.OK);
            originalResponse.Content = new ObjectContent(typeof(string), "value", formatter.Object);

            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
            {
                InnerHandler = new TestMessageHandler(originalResponse)
            };

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/any");
            request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment("EntitySet"));

            // Act 
            HttpResponseMessage response = handler.SendAsync(request).Result;

            // Assert
            Assert.Same(originalResponse, response);
        }
        public void SendAsync_ReturnsNotFoundForNullEntityResponse()
        {
            // Arrange
            Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>();
            formatter.Setup(f => f.CanWriteType(It.IsAny<Type>())).Returns(true);

            HttpResponseMessage originalResponse = new HttpResponseMessage(HttpStatusCode.OK);
            originalResponse.Content = new ObjectContent(typeof(string), null, formatter.Object);

            ODataNullValueMessageHandler handler = new ODataNullValueMessageHandler
            {
                InnerHandler = new TestMessageHandler(originalResponse)
            };

            ODataPath path = new DefaultODataPathHandler().Parse(BuildModel(), "http://localhost/any", "Customers(3)");
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/any");
            request.SetConfiguration(new HttpConfiguration());
            request.ODataProperties().Path = path;

            // Act 
            HttpResponseMessage response = handler.SendAsync(request).Result;

            // Assert
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }