public void FindPathTemplates(string url, string key) { var table = new UriTemplateTable(); // Shorter paths and literal path segments should be added to the table first. table.Add("root", new UriTemplate("/")); table.Add("foo", new UriTemplate("/foo/{bar}")); table.Add("kit", new UriTemplate("/baz/kit")); table.Add("baz", new UriTemplate("/baz/{bar}")); table.Add("blob", new UriTemplate("/baz/{bar}/blob")); table.Add("goo", new UriTemplate("/{goo}/{bar}/blob")); table.Add("set", new UriTemplate("/settings/{id}")); table.Add("org", new UriTemplate("/organization/{id}/settings/iteminsights")); var uri = new Uri(url, UriKind.RelativeOrAbsolute); var result = table.Match(new Uri(url, UriKind.RelativeOrAbsolute)); if (string.IsNullOrEmpty(key)) { Assert.Null(result); } else { Assert.Equal(key, result.Key); } Assert.NotNull(table["goo"]); Assert.Null(table["goo1"]); }
public void FindTemplatesWithArrayQueryParameters() { var table = new UriTemplateTable(); // More restrictive templates should have priority over less restrictive ones table.Add("fooxy3", new UriTemplate("/foo?x={x}&y={y}")); table.Add("fooxy2", new UriTemplate("/foo?x={x}{&y}")); table.Add("fooxy4", new UriTemplate("/foo?x={x}{&z}")); table.Add("fooxy", new UriTemplate("/foo{?x,y}")); table.Add("foo", new UriTemplate("/foo")); var result = table.Match(new Uri("/foo?x=a,b,c,d", UriKind.RelativeOrAbsolute)); Assert.Equal("fooxy2", result.Key); }
/// <summary> /// Populates the template table with the request urls and the scopes table with the permission scopes. /// </summary> private void SeedPermissionsTables() { HashSet <string> uniqueRequestUrlsTable = new HashSet <string>(); int count = 0; foreach (string permissionFilePath in _permissionsFilePaths) { string jsonString = _fileUtility.ReadFromFile(permissionFilePath).GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(jsonString)) { JObject permissionsObject = JObject.Parse(jsonString); JToken apiPermissions = permissionsObject.First.First; foreach (JProperty property in apiPermissions) { // Remove any '(...)' from the request url and set to lowercase for uniformity string requestUrl = Regex.Replace(property.Name.ToLower(), @"\(.*?\)", string.Empty); if (uniqueRequestUrlsTable.Add(requestUrl)) { count++; // Add the request url _urlTemplateTable.Add(count.ToString(), new UriTemplate(requestUrl)); // Add the permission scopes _scopesListTable.Add(count, property.Value); } } } } }
static ThemeService() { #if NETCOREAPP _templateTable = new UriTemplateTable(); _templateTable.Add("theme", new UriTemplate(_baseUrl + _themeUrl)); _templateTable.Add("accent", new UriTemplate(_baseUrl + _accentUrl)); _templateTable.Add("uwptheme", new UriTemplate(_baseUrl + _uwpThemeUrl)); _templateTable.Add("uwpaccent", new UriTemplate(_baseUrl + _uwpAccentUrl)); _templateTable.Add("style", new UriTemplate(_baseUrl + _styleUrl)); #else _templateTable = new UriTemplateTable(_templateBaseUri); _templateTable.KeyValuePairs.Add(new KeyValuePair <UriTemplate, Object>(_themeTemplate, "theme")); _templateTable.KeyValuePairs.Add(new KeyValuePair <UriTemplate, Object>(_accentTemplate, "accent")); _templateTable.KeyValuePairs.Add(new KeyValuePair <UriTemplate, Object>(_uwpThemeTemplate, "uwptheme")); _templateTable.KeyValuePairs.Add(new KeyValuePair <UriTemplate, Object>(_uwpAccentTemplate, "uwpaccent")); _templateTable.KeyValuePairs.Add(new KeyValuePair <UriTemplate, Object>(_styleTemplate, "style")); _templateTable.MakeReadOnly(false); #endif }
public void MatchTemplateWithDifferentOrderOfQueryStringParameters() { var table = new UriTemplateTable(); // More restrictive templates should have priority over less restrictive ones table.Add("fooxy3", new UriTemplate("/foo?x={x}&y={y}")); var result = table.Match(new Uri("/foo?y=a&x=b", UriKind.RelativeOrAbsolute), QueryStringParameterOrder.Any); Assert.Equal("fooxy3", result.Key); }
public void FindTemplatesWithQueryStrings(string url, string key) { var table = new UriTemplateTable(); // More restrictive templates should have priority over less restrictive ones table.Add("fooxy3", new UriTemplate("/foo?x={x}&y={y}")); table.Add("fooxy2", new UriTemplate("/foo?x={x}{&y}")); table.Add("fooxy4", new UriTemplate("/foo?x={x}{&z}")); table.Add("fooxy", new UriTemplate("/foo{?x,y}")); table.Add("foo", new UriTemplate("/foo")); var result = table.Match(new Uri(url, UriKind.RelativeOrAbsolute)); if (string.IsNullOrEmpty(key)) { Assert.Null(result); } else { Assert.Equal(key, result.Key); } }
public void FindTemplatesInGamesApi(string url, string key) { var table = new UriTemplateTable(); table.Add("games", new UriTemplate("/games")); table.Add("gamessetup", new UriTemplate("/games/{gametitle}/Setup/{gamesid}")); table.Add("resource", new UriTemplate("/games/{gametitle}/Resources/{resourcetype}/{resourceid}")); table.Add("chat", new UriTemplate("/games/{gametitle}/{gameid}/Chat/{chatid}")); table.Add("state", new UriTemplate("/games/{gametitle}/{gameid}/State/{stateid}")); var result = table.Match(new Uri(url, UriKind.RelativeOrAbsolute)); if (string.IsNullOrEmpty(key)) { Assert.Null(result); } else { Assert.Equal(key, result.Key); } }
/// <summary> /// Populates the template table with the request urls and the scopes table with the permission scopes. /// </summary> private void SeedPermissionsTables() { _urlTemplateTable = new UriTemplateTable(); _scopesListTable = new Dictionary <int, object>(); HashSet <string> uniqueRequestUrlsTable = new HashSet <string>(); int count = 0; foreach (string permissionFilePath in _permissionsBlobNames) { string relativePermissionPath = FileServiceHelper.GetLocalizedFilePathSource(_permissionsContainerName, permissionFilePath); string jsonString = _fileUtility.ReadFromFile(relativePermissionPath).GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(jsonString)) { JObject permissionsObject = JObject.Parse(jsonString); if (permissionsObject.Count < 1) { throw new InvalidOperationException($"The permissions data sources cannot be empty." + $"Check the source file or check whether the file path is properly set. File path: " + $"{relativePermissionPath}"); } JToken apiPermissions = permissionsObject.First.First; foreach (JProperty property in apiPermissions) { // Remove any '(...)' from the request url and set to lowercase for uniformity string requestUrl = Regex.Replace(property.Name.ToLower(), @"\(.*?\)", string.Empty); if (uniqueRequestUrlsTable.Add(requestUrl)) { count++; // Add the request url _urlTemplateTable.Add(count.ToString(), new UriTemplate(requestUrl)); // Add the permission scopes _scopesListTable.Add(count, property.Value); } } _permissionsRefreshed = true; } } }
public ContractRouter(Stream contractStream) { // By default, assume it is an OpenAPI contract // We can add another ctor that has a media type parameter to support // other contracts. var openApiDoc = new OpenApiDocument(); JsonStreamingParser.ParseStream(contractStream, openApiDoc, OpenApiVocab.Create()); _UriTemplateTable = new UriTemplateTable(); foreach (var path in openApiDoc.Paths) { if (!string.IsNullOrWhiteSpace(path.Value.XController)) { _UriTemplateTable.Add(path.Value.XController.ToLowerInvariant(), new UriTemplate(path.Key)); } } }
/// <summary> /// Populates the _uriTemplateTable with the Graph url paths and the _openApiOperationsTable /// with the respective OpenApiOperations for these urls paths. /// </summary> /// <param name="graphUri">The uri of the Microsoft Graph metadata doc.</param> /// <param name="forceRefresh">Don't read from in-memory cache.</param> private static async Task PopulateReferenceTablesAync(OpenApiDocument source) { HashSet <string> uniqueUrlsTable = new HashSet <string>(); // to ensure unique url path entries in the UriTemplate table int count = 0; foreach (var path in source.Paths) { if (uniqueUrlsTable.Add(path.Key)) { count++; string urlPath = path.Key.Replace('-', '_'); _uriTemplateTable.Add(count.ToString(), new UriTemplate(urlPath.ToLower())); OpenApiOperation[] operations = path.Value.Operations.Values.ToArray(); _openApiOperationsTable.Add(count, operations); } } }