Ejemplo n.º 1
0
        /// <summary>
        /// Test method to call constraint.Match using the proper arguments for each platform.
        /// </summary>
        /// <param name="constraint">The constraint object.</param>
        /// <param name="versionRequest">The abstracted request.</param>
        /// <param name="direction">The abstracted route direction.</param>
        /// <returns>Result from constraint.Match,</returns>
        private bool ConstraintMatch(ODataVersionConstraint constraint, TestVersionRequest versionRequest, RouteDirection direction)
        {
#if NETCORE
            AspNetCore.Http.HttpContext context = new AspNetCore.Http.DefaultHttpContext();
            AspNetCore.Http.HttpRequest request = context.Request;
            foreach (KeyValuePair <string, string> kvp in versionRequest.Headers)
            {
                request.Headers.Add(kvp.Key, kvp.Value);
            }

            System.Uri requestUri = new System.Uri(versionRequest.Uri);
            request.Method = versionRequest.Method.ToString();
            request.Host   = new AspNetCore.Http.HostString(requestUri.Host, requestUri.Port);
            request.Scheme = requestUri.Scheme;

            AspNetCore.Routing.RouteDirection routeDirection = (direction == RouteDirection.UriResolution)
                ? AspNetCore.Routing.RouteDirection.IncomingRequest
                : AspNetCore.Routing.RouteDirection.UrlGeneration;

            return(constraint.Match(context, null, null, null, routeDirection));
#else
            HttpRequestMessage request = new HttpRequestMessage(versionRequest.Method, versionRequest.Uri);
            foreach (KeyValuePair <string, string> kvp in versionRequest.Headers)
            {
                request.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
            }

            System.Web.Http.Routing.HttpRouteDirection routeDirection = (direction == RouteDirection.UriResolution)
                ? System.Web.Http.Routing.HttpRouteDirection.UriResolution
                : System.Web.Http.Routing.HttpRouteDirection.UriGeneration;

            return(constraint.Match(request, null, null, null, routeDirection));
#endif
        }
Ejemplo n.º 2
0
        public void Ctor_PamaterlessDefaultsToV4()
        {
            // Act
            ODataVersionConstraint constraint = new ODataVersionConstraint();

            // Assert
            Assert.Equal(ODataVersion.V4, constraint.Version);
        }
Ejemplo n.º 3
0
        public void Ctor_SingleParameterConfiguresSingleVersion()
        {
            // Act
            ODataVersionConstraint constraint = new ODataVersionConstraint(ODataVersion.V2);

            // Assert
            Assert.Equal(constraint.MinVersion, constraint.MaxVersion);
            Assert.Equal(ODataVersion.V2, constraint.MaxVersion);
        }
Ejemplo n.º 4
0
        public void Ctor_PamaterlessDefaultsToRangeV1ToV3()
        {
            // Act
            ODataVersionConstraint constraint = new ODataVersionConstraint();

            // Assert
            Assert.Equal(ODataVersion.V1, constraint.MinVersion);
            Assert.Equal(ODataVersion.V3, constraint.MaxVersion);
        }
Ejemplo n.º 5
0
        public void Can_Create_ODataVersionConstraint()
        {
            // Arrange & Act
            ODataVersionConstraint versionConstraint = new ODataVersionConstraint(ODataVersion.V2, ODataVersion.V3);

            // Assert
            Assert.Equal(ODataVersion.V2, versionConstraint.MinVersion);
            Assert.Equal(ODataVersion.V3, versionConstraint.MaxVersion);
        }
        public void Can_Create_ODataVersionConstraint()
        {
            // Arrange & Act
            ODataVersionConstraint versionConstraint = new ODataVersionConstraint(ODataVersion.V2, ODataVersion.V3);

            // Assert
            Assert.Equal(ODataVersion.V2, versionConstraint.MinVersion);
            Assert.Equal(ODataVersion.V3, versionConstraint.MaxVersion);
        }
        public void Ctor_PamaterlessDefaultsToRangeV1ToV3()
        {
            // Act
            ODataVersionConstraint constraint = new ODataVersionConstraint();

            // Assert
            Assert.Equal(ODataVersion.V1, constraint.MinVersion);
            Assert.Equal(ODataVersion.V3, constraint.MaxVersion);
        }
        public void Ctor_SingleParameterConfiguresSingleVersion()
        {
            // Act
            ODataVersionConstraint constraint = new ODataVersionConstraint(ODataVersion.V2);

            // Assert
            Assert.Equal(constraint.MinVersion, constraint.MaxVersion);
            Assert.Equal(ODataVersion.V2, constraint.MaxVersion);
        }
        public void Ctor_PamaterlessDefaultsToV4()
        {
            // Act
            ODataVersionConstraint constraint = new ODataVersionConstraint();

            // Assert
            Assert.NotNull(constraint.Version);
            Assert.Equal(ODataVersion.V4, constraint.Version);
        }
Ejemplo n.º 10
0
        public void Matches_IfHeaders_Dont_Exist()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            HttpRequestMessage     request    = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            // Act
            bool result = constraint.Match(request, null, null, null, HttpRouteDirection.UriResolution);

            // Assert
            Assert.True(result);
        }
        public void Matches_IfHeaders_Dont_Exist()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint(ODataVersion.V3);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            // Act
            bool result = constraint.Match(request, null, null, null, HttpRouteDirection.UriResolution);

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 12
0
        public void Matches_IfHeaders_Dont_Exist()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            TestVersionRequest     request    = new TestVersionRequest(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            // Act
            bool result = ConstraintMatch(constraint, request, RouteDirection.UriResolution);

            // Assert
            Assert.True(result);
        }
        public void DoesNotMatch_IfNextVersionHeadersExist(string headerName)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint(ODataVersion.V2);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");
            request.Headers.TryAddWithoutValidation(headerName, "4.0");

            // Act
            bool result = constraint.Match(request, null, null, null, HttpRouteDirection.UriResolution);

            // Assert
            Assert.False(result);
        }
        public void Matches_IfRouteDirectionIsLinkGeneration()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");
            request.Headers.TryAddWithoutValidation("OData-Version", "invalid");

            // Act
            bool result = constraint.Match(request, null, null, null, HttpRouteDirection.UriGeneration);

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 15
0
        public void DoesNotMatch_IfPreviousVersionHeadersExist(string headerName)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            HttpRequestMessage     request    = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            request.Headers.TryAddWithoutValidation(headerName, "2.0");

            // Act
            bool result = constraint.Match(request, null, null, null, HttpRouteDirection.UriResolution);

            // Assert
            Assert.False(result);
        }
Ejemplo n.º 16
0
        public void Matches_Version(string header, string versionString)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            TestVersionRequest     request    = new TestVersionRequest(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            request.Headers.Add(header, versionString);

            // Act
            bool result = ConstraintMatch(constraint, request, RouteDirection.UriResolution);

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 17
0
        public void DoesNotMatch_IfPreviousVersionHeadersExist(string headerName)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            TestVersionRequest     request    = new TestVersionRequest(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            request.Headers.Add(headerName, "2.0");

            // Act
            bool result = ConstraintMatch(constraint, request, RouteDirection.UriResolution);

            // Assert
            Assert.False(result);
        }
Ejemplo n.º 18
0
        public void Matches_IfRouteDirectionIsLinkGeneration()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            HttpRequestMessage     request    = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            request.Headers.TryAddWithoutValidation("OData-Version", "invalid");

            // Act
            bool result = constraint.Match(request, null, null, null, HttpRouteDirection.UriGeneration);

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 19
0
        public void Matches_IfRouteDirectionIsLinkGeneration()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            TestVersionRequest     request    = new TestVersionRequest(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            request.Headers.Add("OData-Version", "invalid");

            // Act
            bool result = ConstraintMatch(constraint, request, RouteDirection.UriGeneration);

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 20
0
        public void Matches_WithoutVersionHeadersExistWithEnabledRelaxFlag()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint
            {
                IsRelaxedMatch = true
            };
            TestVersionRequest request = new TestVersionRequest(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            // Act
            bool result = ConstraintMatch(constraint, request, RouteDirection.UriResolution);

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 21
0
        public void Matches_WithoutVersionHeadersExistWithEnabledRelaxFlag()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint
            {
                IsRelaxedMatch = true
            };
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            // Act
            bool result = constraint.Match(request, route: null, parameterName: null, values: null,
                                           routeDirection: HttpRouteDirection.UriResolution);

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 22
0
        public void Matches_MinMaxVersion(string odataVersion, string minVersion, string maxVersion)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            TestVersionRequest     request    = new TestVersionRequest(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            request.Headers.Add("OData-Version", odataVersion);
            request.Headers.Add("OData-MinVersion", minVersion);
            request.Headers.Add("OData-MaxVersion", maxVersion);

            // Act
            bool result = ConstraintMatch(constraint, request, RouteDirection.UriResolution);

            // Assert
            Assert.True(result);
        }
        public void DoesNotMatch_IfOnlyPreviousVersionHeadersExistWithEnabledRelaxFlag(string headerName)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint
            {
                IsRelaxedMatch = true
            };
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");
            request.Headers.TryAddWithoutValidation(headerName, "3.0");

            // Act
            bool result = constraint.Match(request, route: null, parameterName: null, values: null,
                routeDirection: HttpRouteDirection.UriResolution);

            // Assert
            Assert.False(result);
        }
Ejemplo n.º 24
0
        public void DoesNotMatch_IfOnlyPreviousVersionHeadersExistWithEnabledRelaxFlag(string headerName)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint
            {
                IsRelaxedMatch = true
            };
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            request.Headers.TryAddWithoutValidation(headerName, "3.0");

            // Act
            bool result = constraint.Match(request, route: null, parameterName: null, values: null,
                                           routeDirection: HttpRouteDirection.UriResolution);

            // Assert
            Assert.False(result);
        }
Ejemplo n.º 25
0
        public void DoesNotMatch_IfBothMaxVersionAndPreviousVersionHeadersExistWithEnabledRelaxFlag()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint
            {
                IsRelaxedMatch = true
            };
            TestVersionRequest request = new TestVersionRequest(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            request.Headers.Add("MaxDataServiceVersion", "3.0");
            request.Headers.Add("DataServiceVersion", "3.0");
            request.Headers.Add("OData-MaxVersion", "4.0");

            // Act
            bool result = ConstraintMatch(constraint, request, RouteDirection.UriResolution);

            // Assert
            Assert.False(result);
        }
        public void Matches_Only_WhenHeadersAreValidAndVersionIsValid(string dataServiceVersion,
            string maxDataServiceVersion, bool expectedResult)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");
            if (dataServiceVersion != null)
            {
                string versionHeaderName = HttpRequestMessageProperties.ODataServiceVersionHeader;
                request.Headers.TryAddWithoutValidation(versionHeaderName, dataServiceVersion);
            }
            if (maxDataServiceVersion != null)
            {
                string maxVersionHeaderName = HttpRequestMessageProperties.ODataMaxServiceVersionHeader;
                request.Headers.TryAddWithoutValidation(maxVersionHeaderName, maxDataServiceVersion);
            }

            // Act
            bool result = constraint.Match(request, null, null, null, HttpRouteDirection.UriResolution);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Ejemplo n.º 27
0
        public void Matches_Only_WhenHeadersAreValidAndVersionIsValid(string dataServiceVersion,
                                                                      string maxDataServiceVersion, bool expectedResult)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            HttpRequestMessage     request    = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            if (dataServiceVersion != null)
            {
                string versionHeaderName = HttpRequestMessageProperties.ODataServiceVersionHeader;
                request.Headers.TryAddWithoutValidation(versionHeaderName, dataServiceVersion);
            }
            if (maxDataServiceVersion != null)
            {
                string maxVersionHeaderName = HttpRequestMessageProperties.ODataMaxServiceVersionHeader;
                request.Headers.TryAddWithoutValidation(maxVersionHeaderName, maxDataServiceVersion);
            }

            // Act
            bool result = constraint.Match(request, null, null, null, HttpRouteDirection.UriResolution);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Ejemplo n.º 28
0
        public void Matches_Only_WhenHeadersAreValidAndVersionIsValid(string dataServiceVersion,
                                                                      string maxDataServiceVersion, bool expectedResult)
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint();
            var request = new TestVersionRequest(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            if (dataServiceVersion != null)
            {
                string versionHeaderName = ODataVersionConstraint.ODataServiceVersionHeader;
                request.Headers.Add(versionHeaderName, dataServiceVersion);
            }
            if (maxDataServiceVersion != null)
            {
                string maxVersionHeaderName = ODataVersionConstraint.ODataMaxServiceVersionHeader;
                request.Headers.Add(maxVersionHeaderName, maxDataServiceVersion);
            }

            // Act
            bool result = ConstraintMatch(constraint, request, RouteDirection.UriResolution);

            // Assert
            Assert.Equal(expectedResult, result);
        }
        public void Matches_WithoutVersionHeadersExistWithEnabledRelaxFlag()
        {
            // Arrange
            ODataVersionConstraint constraint = new ODataVersionConstraint
            {
                IsRelaxedMatch = true
            };
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345/itdoesnotmatter");

            // Act
            bool result = constraint.Match(request, route: null, parameterName: null, values: null,
                routeDirection: HttpRouteDirection.UriResolution);

            // Assert
            Assert.True(result);
        }