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; }
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"); }
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"); }
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"); }
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)"); }
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()); }
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()); }
/// <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() }; }
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 }); }
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); } }
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 }); }
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(); }
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()); }
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(); }
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()); }
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"); }
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); }
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); }
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'"); }
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); }
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; }
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"); }
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)"); }
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())); }
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"); }
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)); }
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')"); }
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())); }
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"); }
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)); }