public void ThrowArgumentNullExceptionForEmptyOrNullTemplateValueInAdd() { // Arrange var table = new UriTemplateMatcher(); // Act and Assert Assert.Throws <ArgumentNullException>(() => table.Add("set", "")); Assert.Throws <ArgumentNullException>(() => table.Add("set", null)); }
public void ThrowArgumentNullExceptionForEmptyOrNullKeyValueInAdd() { // Arrange var table = new UriTemplateMatcher(); // Act and Assert Assert.Throws <ArgumentNullException>(() => table.Add("", "/settings/{id}")); Assert.Throws <ArgumentNullException>(() => table.Add(null, "/settings/{id}")); }
public void ThrowArgumentNullExceptionForNullUriValueInMatch() { // Arrange var table = new UriTemplateMatcher(); table.Add("goo", "/{goo}/{bar}/blob"); table.Add("set", "/settings/{id}"); table.Add("org", "/organization/{id}/settings/iteminsights"); // Act and Assert Assert.Throws <ArgumentNullException>(() => table.Match(null)); }
/// <summary> /// Populates the template table with the request urls and the scopes table with the permission scopes. /// </summary> private void SeedPermissionsTables() { _urlTemplateMatcher = new UriTemplateMatcher(); _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 _urlTemplateMatcher.Add(count.ToString(), requestUrl); // Add the permission scopes _scopesListTable.Add(count, property.Value); } } _permissionsRefreshed = true; } } }
/// <summary> /// Populates the _uriTemplateTable with the Graph url paths and the _openApiOperationsTable /// with the respective OpenApiOperations for these urls paths. /// </summary> /// <param name="source">The OpenAPI document.</param> /// <returns>A task.</returns> 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(), urlPath.ToLower()); OpenApiOperation[] operations = path.Value.Operations.Values.ToArray(); _openApiOperationsTable.Add(count, operations); } } }
public void MatchPathToUriTemplates(string uri, string key) { // Arrange var table = new UriTemplateMatcher(); table.Add("root", "/"); table.Add("foo", "/foo/{bar}"); table.Add("kit", "/baz/kit"); table.Add("fooxy3", "/foo?x={x}&y={y}"); table.Add("baz", "/baz/{bar}"); table.Add("blob", "/baz/{bar}/blob"); table.Add("goo", "/{goo}/{bar}/blob"); table.Add("set", "/settings/{id}"); table.Add("state", "/games/{gametitle}/{gameid}/State/{stateid}"); table.Add("org", "/organization/{id}/settings/iteminsights"); table.Add("gamessetup", "/games/{gametitle}/Setup/{gamesid}"); // Act var result = table.Match(new Uri(uri, UriKind.RelativeOrAbsolute)); // Assert if (string.IsNullOrEmpty(key)) { Assert.Null(result); } else { Assert.Equal(key, result?.Key); } Assert.NotNull(table["goo"]); Assert.Null(table["goo1"]); }