Ejemplo n.º 1
0
        public async Task RouteAsync(RouteContext context)
        {
            var request = context.HttpContext.Request;

            Uri uri;
            PathString remaining;
            if (!request.Path.StartsWithSegments(PathString.FromUriComponent("/" + _routePrefix), out remaining))
            {
                // Fallback to other routes.
                return;
            }

            uri = new Uri(remaining.ToString(), UriKind.Relative);
            
            context.HttpContext.ODataProperties().Model = _model;
            var parser = new ODataUriParser(_model, uri);
            var path = parser.ParsePath();
            context.HttpContext.ODataProperties().NewPath = path;
            context.HttpContext.ODataProperties().Path =
                context.HttpContext.ODataPathHandler().Parse(_model, "http://service-root/", remaining.ToString());
            context.HttpContext.ODataProperties().IsValidODataRequest = true;

            await m.RouteAsync(context);
            context.IsHandled = true;
        }
Ejemplo n.º 2
0
        public void PathNavigation()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/Customers(5)/Orders"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("CustoMERS(5)/OrDErs");
        }
Ejemplo n.º 3
0
        public void PathComplex()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/Customers(-32)/HomeAddress"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("CustomErs(-32)/HOMEAddress");
        }
Ejemplo n.º 4
0
        public void PathBatch()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/$batch"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("$BATch");
        }
Ejemplo n.º 5
0
        public void PathEntity()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/Orders(2)"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("OrDERS(2)");
        }
Ejemplo n.º 6
0
        private static void KeyAfterFunction()
        {
            Console.WriteLine("TestKeyAfterFunction");
            var parser = new ODataUriParser(
                extModel.Model,
                ServiceRoot,
                new Uri("http://demo/odata.svc/People(2)/TestNS.GetFriends(2)"));

            var path=parser.ParsePath();
            Console.WriteLine(path.ToLogString());
        }
Ejemplo n.º 7
0
        private static void KeyContainingSpecialChar()
        {
            Console.WriteLine("KeyContainingSpecialChar");
            var parser = new ODataUriParser(
                extModel.Model,
                ServiceRoot,
                new Uri("http://demo/odata.svc/Resources('w%23j')"));

            var path = parser.ParsePath();
            Console.WriteLine(path.ToLogString());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Parses the specified URI and wraps the results in a QueryContext.
        /// </summary>
        /// <param name="requestUri">The URI to parse.</param>
        /// <param name="model">The data store model.</param>
        /// <returns>The parsed URI wrapped in a QueryContext.</returns>
        public static QueryContext ParseUri(Uri requestUri, IEdmModel model)
        {
            var requestUriParts = requestUri.OriginalString.Split('?');
            var queryPath = requestUriParts.First();
            if (requestUriParts.Count() > 1)
            {
                throw new NotSupportedException("Query option is not supported by the service yet.");
            }

            ODataUriParser uriParser = new ODataUriParser(model, ServiceConstants.ServiceBaseUri, new Uri(queryPath, UriKind.Absolute));
            return new QueryContext { QueryPath = uriParser.ParsePath() };
        }
Ejemplo n.º 9
0
        public void ParseKeyTemplateWithTemplateParserWithWithWhitespaceOutsideofTempateExpressionTest()
        {
            var uriParser = new ODataUriParser(HardCodedTestModel.TestModel, new Uri("People(  {1} )", UriKind.Relative))
            {
                EnableUriTemplateParsing = true
            };

            var path = uriParser.ParsePath();
            var keySegment = path.LastSegment.As<KeySegment>();
            KeyValuePair<string, object> keypair = keySegment.Keys.Single();
            keypair.Key.Should().Be("ID");
            keypair.Value.As<UriTemplateExpression>().ShouldBeEquivalentTo(new UriTemplateExpression { LiteralText = "{1}", ExpectedType = keySegment.EdmType.As<IEdmEntityType>().DeclaredKey.Single().Type });
        }
Ejemplo n.º 10
0
 public void PathBaseSingletonWithKeyShouldFail()
 {
     ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/SpecialOrder(1)"));
     try
     {
         parser.ParsePath();
     }
     catch (ODataException e)
     {
         var expected = ODataExpectedExceptions.ODataException("RequestUriProcessor_SyntaxError");
         expected.ExpectedMessage.Verifier.VerifyMatch("RequestUriProcessor_SyntaxError", e.Message);
     }
 }
Ejemplo n.º 11
0
        public void ParseKeyTemplateAsSegmentWithTemplateParser()
        {
            var uriParser = new ODataUriParser(HardCodedTestModel.TestModel, new Uri("http://host"), new Uri("http://host/People/{1}"))
            {
                EnableUriTemplateParsing = true,
                UrlConventions = ODataUrlConventions.KeyAsSegment
            };

            var path = uriParser.ParsePath();
            var keySegment = path.LastSegment.As<KeySegment>();
            KeyValuePair<string, object> keypair = keySegment.Keys.Single();
            keypair.Key.Should().Be("ID");
            keypair.Value.As<UriTemplateExpression>().ShouldBeEquivalentTo(new UriTemplateExpression { LiteralText = "{1}", ExpectedType = keySegment.EdmType.As<IEdmEntityType>().DeclaredKey.Single().Type });
        }
Ejemplo n.º 12
0
 public void NoneQueryOptionShouldWork()
 {
     var uriParser = new ODataUriParser(HardCodedTestModel.TestModel, ServiceRoot, FullUri);
     var path = uriParser.ParsePath();
     path.Should().HaveCount(1);
     path.LastSegment.ShouldBeEntitySetSegment(HardCodedTestModel.GetPeopleSet());
     uriParser.ParseFilter().Should().BeNull();
     uriParser.ParseSelectAndExpand().Should().BeNull();
     uriParser.ParseOrderBy().Should().BeNull();
     uriParser.ParseTop().Should().Be(null);
     uriParser.ParseSkip().Should().Be(null);
     uriParser.ParseCount().Should().Be(null);
     uriParser.ParseSearch().Should().BeNull();
     uriParser.ParseSkipToken().Should().BeNull();
     uriParser.ParseDeltaToken().Should().BeNull();
 }
Ejemplo n.º 13
0
        private static void TestCaseInsensitive()
        {
            Console.WriteLine("TestCaseInsensitive");
            var parser = new ODataUriParser(
                extModel.Model,
                ServiceRoot,
                new Uri("http://demo/odata.svc/People(1)/Pets/TestNS.Fish?$orderby=Color"));

            var path = parser.ParsePath();
            var clause = parser.ParseOrderBy();
            Console.WriteLine(path.ToLogString());
            Console.WriteLine(clause.Expression.ToLogString());

            var parser2 = new ODataUriParser(
                extModel.Model,
                ServiceRoot,
                new Uri("http://demo/odata.svc/people(1)/pets/testns.fish?$ORDERBY=color"))
            {
                Resolver = new ODataUriResolver { EnableCaseInsensitive = true }
            };

            // Identical to path and clause
            var path2 = parser2.ParsePath();
            var clause2 = parser2.ParseOrderBy();
            Console.WriteLine(path2.ToLogString());
            Console.WriteLine(clause2.Expression.ToLogString());

            // Query option parser also supports custom resolver
            var parser3 = new ODataQueryOptionParser(
                extModel.Model,
                extModel.Fish,
                extModel.PetSet,
                new Dictionary<string, string>
                {
                    {"$orderby", "color"}
                })
            {
                Resolver = new ODataUriResolver { EnableCaseInsensitive = true }
            };
            var clause3 = parser3.ParseOrderBy();
            Console.WriteLine(clause3.Expression.ToLogString());
        }
Ejemplo n.º 14
0
 public void EmptyValueQueryOptionShouldWork()
 {
     var uriParser = new ODataUriParser(HardCodedTestModel.TestModel, ServiceRoot, new Uri(FullUri, "?$filter=&$select=&$expand=&$orderby=&$top=&$skip=&$count=&$search=&$unknow=&$unknowvalue&$skiptoken=&$deltatoken="));
     var path = uriParser.ParsePath();
     path.Should().HaveCount(1);
     path.LastSegment.ShouldBeEntitySetSegment(HardCodedTestModel.GetPeopleSet());
     uriParser.ParseFilter().Should().BeNull();
     var results = uriParser.ParseSelectAndExpand();
     results.AllSelected.Should().BeTrue();
     results.SelectedItems.Should().HaveCount(0);
     uriParser.ParseOrderBy().Should().BeNull();
     Action action = () => uriParser.ParseTop();
     action.ShouldThrow<ODataException>().WithMessage(Strings.SyntacticTree_InvalidTopQueryOptionValue(""));
     action = () => uriParser.ParseSkip();
     action.ShouldThrow<ODataException>().WithMessage(Strings.SyntacticTree_InvalidSkipQueryOptionValue(""));
     action = () => uriParser.ParseCount();
     action.ShouldThrow<ODataException>().WithMessage(Strings.ODataUriParser_InvalidCount(""));
     action = () => uriParser.ParseSearch();
     action.ShouldThrow<ODataException>().WithMessage(Strings.UriQueryExpressionParser_ExpressionExpected(0, ""));
     uriParser.ParseSkipToken().Should().BeEmpty();
     uriParser.ParseDeltaToken().Should().BeEmpty();
 }
Ejemplo n.º 15
0
        private static void TestUnqualified()
        {
            Console.WriteLine("TestUnqualified");
            var parser = new ODataUriParser(
                extModel.Model,
                ServiceRoot,
                new Uri("http://demo/odata.svc/People(1)/Addr/TestNS.GetZip"));

            var path = parser.ParsePath();
            Console.WriteLine(path.ToLogString());

            var parser2 = new ODataUriParser(
                extModel.Model,
                ServiceRoot,
                new Uri("http://demo/odata.svc/People(1)/addr/getzip"))
            {
                Resolver = new UnqualifiedODataUriResolver { EnableCaseInsensitive = true }
            };

            // Identical to path
            var path2 = parser2.ParsePath();
            Console.WriteLine(path2.ToLogString());
        }
Ejemplo n.º 16
0
        public void PathTypeSegmentToNavigation()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/People(1)/Microsoft.Test.Taupo.OData.WCFService.Customer/Orders"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("people(1)/microsoft.test.taupo.ODATA.WCFService.Customer/OrderS");
            //, Unqualified type not supported yet.
            //"People(1)/Customer/Orders",
            //"People(1)/customer/Orders");
        }
Ejemplo n.º 17
0
        public void ParsePathExceptionDataTestWithAlias()
        {
            var parser = new ODataUriParser(HardCodedTestModel.TestModel, new Uri("GetCoolestPersonWithStyle(styleID=@p1)/UndeclaredProperty?@p1=32", UriKind.Relative));
            Action action = () => parser.ParsePath();
            ODataUnrecognizedPathException ex = action.ShouldThrow<ODataUnrecognizedPathException>().And;
            ex.ParsedSegments.As<IEnumerable<ODataPathSegment>>().Count().Should().Be(1);
            ex.CurrentSegment.Should().Be("UndeclaredProperty");
            ex.UnparsedSegments.As<IEnumerable<string>>().Count().Should().Be(0);

            var aliasNode = parser.ParameterAliasNodes;
            aliasNode.Count.Should().Be(1);
            aliasNode["@p1"].ShouldBeConstantQueryNode(32);
        }
Ejemplo n.º 18
0
 public void ParsePathExceptionDataTestInvalidNavigationProperty()
 {
     var parser = new ODataUriParser(HardCodedTestModel.TestModel, new Uri("People(1)/MyDog/Color1", UriKind.Relative));
     Action action = () => parser.ParsePath();
     ODataUnrecognizedPathException ex = action.ShouldThrow<ODataUnrecognizedPathException>().And;
     ex.ParsedSegments.As<IEnumerable<ODataPathSegment>>().Count().Should().Be(3);
     ex.CurrentSegment.Should().Be("Color1");
     ex.UnparsedSegments.As<IEnumerable<string>>().Count().Should().Be(0);
     parser.ParameterAliasNodes.Count.Should().Be(0);
 }
Ejemplo n.º 19
0
        public void PathFunctionAliasedParameterWithResolver()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/Customers/Microsoft.Test.Taupo.OData.WCFService.InCity(City=@a)?@a='Redmond'"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("customers/Microsoft.Test.Taupo.OData.wcfservice.InCity(city=@a)?@a='Redmond'",
                "Customers/InCity(City=@a)?@a='Redmond'",
                "customers/incity(CITY=@a)?@a='Redmond'");
        }
Ejemplo n.º 20
0
 public void PathLimitIsRespectedForPath()
 {
     ODataUriParser parser = new ODataUriParser(HardCodedTestModel.TestModel, new Uri("http://gobbldygook/", UriKind.Absolute), new Uri("http://gobbldygook/path/to/something", UriKind.Absolute)) { Settings = { PathLimit = 0 } };
     Action parseWithLimit = () => parser.ParsePath();
     parseWithLimit.ShouldThrow<ODataException>().WithMessage(ODataErrorStrings.UriQueryPathParser_TooManySegments);
 }
Ejemplo n.º 21
0
        protected virtual ODataMessageWriterSettings GetWriterSettings()
        {
            ODataMessageWriterSettings settings = new ODataMessageWriterSettings
            {
                AutoComputePayloadMetadataInJson = true,
                PayloadBaseUri = this.ServiceRootUri,
                ODataUri = new ODataUri()
                {
                    RequestUri = this.RequestUri,
                    ServiceRoot = this.ServiceRootUri,
                    Path = this.QueryContext.QueryPath,
                    SelectAndExpand = this.QueryContext.QuerySelectExpandClause,
                }
            };

            // TODO: howang why here?
            if (this.QueryContext != null)
            {
                if (this.QueryContext.CanonicalUri == null)
                {
                    settings.ODataUri.RequestUri = this.QueryContext.QueryUri;
                    settings.ODataUri.Path = this.QueryContext.QueryPath;
                }
                else
                {
                    settings.ODataUri.RequestUri = this.QueryContext.CanonicalUri;
                    ODataUriParser uriParser = new ODataUriParser(this.DataSource.Model, ServiceConstants.ServiceBaseUri, this.QueryContext.CanonicalUri);
                    settings.ODataUri.Path = uriParser.ParsePath();
                }
            }

            // TODO: howang read the encoding from request.
            settings.SetContentType(string.IsNullOrEmpty(this.QueryContext.FormatOption) ? this.RequestAcceptHeader : this.QueryContext.FormatOption, Encoding.UTF8.WebName);

            return settings;
        }
Ejemplo n.º 22
0
        public void PathPrimitive()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/Customers(0)/FirstName"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("CustomERS(0)/FiRStName");
        }
Ejemplo n.º 23
0
        public void PathFunctionWithComplexParameters()
        {
            string inputstring = "http://www.potato.com/Customers/Microsoft.Test.Taupo.OData.WCFService.Within(Address={ Street:'1 Microsoft Way', City:'Redmond', PostalCode:'98052' }, " +
                "Distance=@a, Location=geography'Point(10 30 15 6)', ArbitraryInt=@b, DateTimeOffset=null, Byte=@c, LineString=@d)/" +
                "Microsoft.Test.Taupo.OData.WCFService.Within(Address=null, " +
                "Distance=5.04, ArbitraryInt=24555, DateTimeOffset=null, Byte=@f, Location=@e, LineString=@potato)";
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri(inputstring));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions(
                "customers/Microsoft.Test.Taupo.OData.WCFService.Within(Address={ Street:'1 Microsoft Way', City:'Redmond', PostalCode:'98052' }, " +
                "Distance=@a, LocAtion=geography'Point(10 30 15 6)', ArbitraryInt=@b, DateTimeOffset=null, Byte=@c, LineString=@d)/" +
                "Microsoft.Test.Taupo.OData.WCFService.Within(Address=null, " +
                "Distance=5.04, ArbitraryInt=24555, DateTimeOffset=null, Byte=@f, Location=@e, LineString=@potato)",
                "Customers/Within(Address={ Street:'1 Microsoft Way', City:'Redmond', PostalCode:'98052' }, " +
                "Distance=@a, Location=geography'Point(10 30 15 6)', ArbitraryInt=@b, DateTimeOffset=null, Byte=@c, LineString=@d)/" +
                "Microsoft.Test.Taupo.OData.WCFService.Within(Address=null, " +
                "Distance=5.04, ArbitraryInt=24555, DateTimeOffset=null, Byte=@f, Location=@e, LineString=@potato)",
                "customers/within(Address={ Street:'1 Microsoft Way', City:'Redmond', PostalCode:'98052' }, " +
                "Distance=@a, LocAtion=geography'Point(10 30 15 6)', ArbitraryInt=@b, DateTimeOffset=null, Byte=@c, LineString=@d)/" +
                "Within(Address=null, " +
                "Distance=5.04, ArbitraryInt=24555, DATETimeOffset=null, Byte=@f, Location=@e, LineString=@potato)");
        }
Ejemplo n.º 24
0
 public void PathFunctionWithParens()
 {
     ODataUriParser parserWithoutparens = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/Orders/Microsoft.Test.Taupo.OData.WCFService.OrdersWithMoreThanTwoItems"));
     ODataUriParser parserWithparens = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/Orders/Microsoft.Test.Taupo.OData.WCFService.OrdersWithMoreThanTwoItems()"));
     Assert.AreEqual(QueryNodeToStringVisitor.ToString(parserWithoutparens.ParsePath()), QueryNodeToStringVisitor.ToString(parserWithparens.ParsePath()));
 }
Ejemplo n.º 25
0
        public void PathAction()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/People(1)/Microsoft.Test.Taupo.OData.WCFService.ChangeAddress"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("PEOPLE(1)/Microsoft.Test.Taupo.ODATA.WCFService.ChangeAddress",
                "People(1)/ChangeAddress",
                "people(1)/changeaddress");
        }
Ejemplo n.º 26
0
 public void PathEntityWithDurationKeyUsingKeyAsSegmments()
 {
     ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/DurationInKeys/P1DT2H3M4.5678901S"));
     parser.UrlConventions = ODataUrlConventions.KeyAsSegment;
     var result = parser.ParsePath();
     ApprovalVerify(QueryNodeToStringVisitor.ToString(result));
 }
Ejemplo n.º 27
0
        public void PathEntityWithDurationKey()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/DurationInKeys(duration'P1DT2H3M4.5678901S')"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("durationinkeys(dUration'P1DT2H3M4.5678901S')");
        }
Ejemplo n.º 28
0
 public void PathActionWithParens()
 {
     ODataUriParser parserWithoutparens = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/People(1)/Microsoft.Test.Taupo.OData.WCFService.ChangeAddress"));
     ODataUriParser parserWithparens = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/People(1)/Microsoft.Test.Taupo.OData.WCFService.ChangeAddress()"));
     Assert.AreEqual(QueryNodeToStringVisitor.ToString(parserWithoutparens.ParsePath()), QueryNodeToStringVisitor.ToString(parserWithparens.ParsePath()));
 }
Ejemplo n.º 29
0
        public void PathCollection()
        {
            ODataUriParser parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/People(1)/Numbers"));
            var result = parser.ParsePath();
            ApprovalVerify(QueryNodeToStringVisitor.ToString(result));

            this.TestExtensions("people(1)/numbers");
        }
Ejemplo n.º 30
0
 public void PathKeysAsSegments()
 {
     var parser = new ODataUriParser(model, new Uri("http://www.potato.com/"), new Uri("http://www.potato.com/Customers/5")) { UrlConventions = ODataUrlConventions.KeyAsSegment };
     var result = parser.ParsePath();
     ApprovalVerify(QueryNodeToStringVisitor.ToString(result));
 }