public void ParseResourcePathTests() { AssertRequestPathIsValid(HttpParsingHelper.ParseResourcePath("a/bb/ccc"), "a", "bb", "ccc", null); AssertRequestPathIsValid(HttpParsingHelper.ParseResourcePath("a/bb/ccc/"), "a", "bb", "ccc", string.Empty); AssertRequestPathIsValid(HttpParsingHelper.ParseResourcePath("/a/bb/ccc/dddd"), "a", "bb", "ccc", "dddd"); AssertRequestPathIsValid(HttpParsingHelper.ParseResourcePath("/a/bb/ccc/dddd/"), "a", "bb", "ccc", "dddd"); AssertRequestPathIsValid(HttpParsingHelper.ParseResourcePath("/a/bb/a/dddd"), "a", "bb", "a", "dddd"); AssertRequestPathIsValid(HttpParsingHelper.ParseResourcePath("/a/bb/ccc/dddd?ee"), "a", "bb", "ccc", "dddd"); AssertRequestPathIsValid(HttpParsingHelper.ParseResourcePath("/a/bb/ccc#ee/ff"), "a", "bb", "ccc", null); AssertRequestPathIsValid(HttpParsingHelper.ParseResourcePath("/a/bb/ccc/?ee/ff"), "a", "bb", "ccc", string.Empty); }
/// <summary> /// Tries parsing given dependency telemetry item. /// </summary> /// <param name="httpDependency">Dependency item to parse. It is expected to be of HTTP type.</param> /// <returns><code>true</code> if successfully parsed dependency.</returns> internal static bool TryParse(ref DependencyTelemetry httpDependency) { string name = httpDependency.Name; string host = httpDependency.Target; string url = httpDependency.Data; if (name == null || host == null || url == null) { return(false); } if (!HttpParsingHelper.EndsWithAny(host, DocumentDbHostSuffixes)) { return(false); } //// //// DocumentDB REST API: https://docs.microsoft.com/en-us/rest/api/documentdb/ //// string verb; string nameWithoutVerb; // try to parse out the verb HttpParsingHelper.ExtractVerb(name, out verb, out nameWithoutVerb, DocumentDbSupportedVerbs); List <KeyValuePair <string, string> > resourcePath = HttpParsingHelper.ParseResourcePath(nameWithoutVerb); // populate properties foreach (var resource in resourcePath) { if (resource.Value != null) { string propertyName = GetPropertyNameForResource(resource.Key); if (propertyName != null) { httpDependency.Properties[propertyName] = resource.Value; } } } string operation = HttpParsingHelper.BuildOperationMoniker(verb, resourcePath); string operationName = GetOperationName(httpDependency, operation); httpDependency.Type = RemoteDependencyConstants.AzureDocumentDb; httpDependency.Name = string.IsNullOrEmpty(operationName) ? httpDependency.Target : operationName; return(true); }
internal static List <KeyValuePair <string, string> > ParseResourcePath(string requestPath) { if (!requestPath.Contains("(")) { return(HttpParsingHelper.ParseResourcePath(requestPath)); } // Parse OData v4 url format List <string> tokens = HttpParsingHelper.TokenizeRequestPath(requestPath); int pairCount = tokens.Count + 1; var results = new List <KeyValuePair <string, string> >(pairCount); for (int i = 0; i < tokens.Count; i++) { var current = tokens[i]; var valueStart = current.IndexOf('('); var valueEnd = current.IndexOf(')'); if (valueStart == -1) { var next = tokens.Count < (i + 1) ? tokens[++i] : null; results.Add(new KeyValuePair <string, string>(current, next)); } else { var key = current.Substring(0, valueStart); var value = current.Substring(valueStart + 2, valueEnd - valueStart - 3); results.Add(new KeyValuePair <string, string>(key, value)); } } return(results); }
private void DocumentDbHttpParserConvertsValidDependencies( string operation, string verb, string url, Dictionary <string, string> properties, string resultCode) { Uri parsedUrl = new Uri(url); // Parse with verb var d = new DependencyTelemetry( dependencyTypeName: RemoteDependencyConstants.HTTP, target: parsedUrl.Host, dependencyName: verb + " " + parsedUrl.AbsolutePath, data: parsedUrl.OriginalString) { ResultCode = resultCode ?? "200" }; bool success = DocumentDbHttpParser.TryParse(ref d); Assert.IsTrue(success, operation); Assert.AreEqual(RemoteDependencyConstants.AzureDocumentDb, d.Type, operation); Assert.AreEqual(parsedUrl.Host, d.Target, operation); Assert.AreEqual(operation, d.Name, operation); if (properties != null) { foreach (var property in properties) { string value = null; Assert.IsTrue(d.Properties.TryGetValue(property.Key, out value), operation); Assert.AreEqual(property.Value, value, operation); } } // Parse without verb d = new DependencyTelemetry( dependencyTypeName: RemoteDependencyConstants.HTTP, target: parsedUrl.Host, dependencyName: parsedUrl.AbsolutePath, data: parsedUrl.OriginalString) { ResultCode = resultCode ?? "200" }; success = DocumentDbHttpParser.TryParse(ref d); Assert.IsTrue(success, operation); Assert.AreEqual(RemoteDependencyConstants.AzureDocumentDb, d.Type, operation); Assert.AreEqual(parsedUrl.Host, d.Target, operation); string moniker = HttpParsingHelper.BuildOperationMoniker(null, HttpParsingHelper.ParseResourcePath(parsedUrl.AbsolutePath)); Assert.AreEqual(moniker, d.Name, operation); if (properties != null) { foreach (var property in properties) { string value = null; Assert.IsTrue(d.Properties.TryGetValue(property.Key, out value), operation); Assert.AreEqual(property.Value, value, operation); } } }
private static void ValidateBuildOperationMoniker(string verb, string url, string expectedMoniker) { var resourcePath = HttpParsingHelper.ParseResourcePath(url); Assert.AreEqual(expectedMoniker, HttpParsingHelper.BuildOperationMoniker(verb, resourcePath)); }