Esempio n. 1
0
        /// <summary>
        /// Find a node from the supplied key.  The action is used to determine what
        /// to do if the node does not exist.
        /// </summary>
        private Node FindNode(TKey key, NotFoundAction action)
        {
            Node currentNode = _root;

            foreach (TSubKey subKey in key)
            {
                Node found = currentNode.GetChildForKey(subKey, _subKeyComparer);

                if (found == null)
                {
                    if (action == NotFoundAction.CreateNew)
                    {
                        found = Node.Create(subKey);
                        currentNode.AddNode(found);
                    }
                    else if (action == NotFoundAction.ReturnNull)
                    {
                        return(null);
                    }
                    else
                    {
                        throw new InvalidOperationException("Inexhuastive branches");
                    }
                }

                currentNode = found;
            }

            return(currentNode);
        }
Esempio n. 2
0
        public void Constructor_SetsStatusCode()
        {
            // Act
            NotFoundAction action = new NotFoundAction();

            // Assert
            Assert.AreEqual(HttpStatusCode.NotFound, action.StatusCode);
        }
Esempio n. 3
0
        public void Execute_WithNullContext_Throws()
        {
            // Arrange
            NotFoundAction  action  = new NotFoundAction();
            IRewriteContext context = null;

            // Act/Assert
            ExceptionAssert.Throws <ArgumentNullException>(() => action.Execute(context));
        }
Esempio n. 4
0
        /// <summary>
        /// Returns the part of the source string before the first occurrence of the specified character.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="ch"></param>
        /// <returns></returns>
        static public String LeftOfFirst(this String source, Char ch, NotFoundAction notFoundAction = NotFoundAction.returnNull)
        {
            int index = source.IndexOf(ch);

            if (index == -1)
            {
                return(source.HandleNotFoundAction(notFoundAction));
            }
            string subString = source.Substring(0, index);

            return(subString);
        }
Esempio n. 5
0
        /// <summary>
        /// Returns the part of the source string after the last occurrence of the specified character.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="ch"></param>
        /// <returns></returns>
        static public String RightOfLast(this String source, Char ch, NotFoundAction notFoundAction = NotFoundAction.returnNull)
        {
            int index = source.LastIndexOf(ch);

            if (index == -1)
            {
                return(source.HandleNotFoundAction(notFoundAction));
            }
            string subString = source.Substring(index + 1);

            return(subString);
        }
Esempio n. 6
0
        /// <summary>
        /// Returns the part of the source string before any occurrence of the specified characters.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="match"></param>
        /// <param name="notFoundAction"></param>
        /// <param name="stringComparison"></param>
        /// <returns></returns>
        public static string LeftOfFirst(this string source,
                                         string match,
                                         NotFoundAction notFoundAction     = NotFoundAction.returnNull,
                                         StringComparison stringComparison = StringComparison.CurrentCulture)
        {
            int index = source.IndexOf(match, stringComparison);

            if (index == -1)
            {
                return(source.HandleNotFoundAction(notFoundAction));
            }
            return(source.Substring(0, index));
        }
Esempio n. 7
0
        public void Execute_SetsStatusCode_ReturnsStopProcessing()
        {
            // Arrange
            NotFoundAction  action  = new NotFoundAction();
            IRewriteContext context = new MockRewriteContext();

            // Act
            RewriteProcessing result = action.Execute(context);

            // Assert
            Assert.AreEqual(HttpStatusCode.NotFound, context.StatusCode);
            Assert.AreEqual(RewriteProcessing.StopProcessing, result);
        }
Esempio n. 8
0
        /// <summary>
        /// Returns the part of the source string before the first occurrence of the specified character.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="ch"></param>
        /// <returns></returns>
        static public String LeftOfLast(this String source,
                                        string match,
                                        NotFoundAction notFoundAction     = NotFoundAction.returnNull,
                                        StringComparison stringComparison = StringComparison.CurrentCulture)
        {
            int index = source.LastIndexOf(match, stringComparison);

            if (index == -1)
            {
                return(source.HandleNotFoundAction(notFoundAction));
            }
            string subString = source.Substring(0, index);

            return(subString);
        }
Esempio n. 9
0
        private static string HandleNotFoundAction(this string source, NotFoundAction notFoundAction)
        {
            switch (notFoundAction)
            {
            case NotFoundAction.returnNull: return(null);

            case NotFoundAction.returnEmpty: return(String.Empty);

            case NotFoundAction.returnAll: return(source);

            case NotFoundAction.throwException: throw new ArgumentException("Match string not present in the source string");

            default: throw new NotImplementedException($"{notFoundAction} is not handled");
            }
        }
Esempio n. 10
0
        private async Task <string> GetAsync(string url, NotFoundAction action = NotFoundAction.ThrowException)
        {
            if (string.IsNullOrEmpty(serverUrl))
            {
                return(null);
            }

            using (var client = await this.CreateHttpClientAsync().ConfigureAwait(false))
            {
                var downloadUrl = IJenkinsConnectionInfoExtensions.GetApiUrl(this.serverUrl) + '/' + url.TrimStart('/');
                this.logger?.LogDebug($"Downloading string from {downloadUrl}...");
                using (var response = await client.GetAsync(downloadUrl, this.cancellationToken).ConfigureAwait(false))
                {
                    if (response.StatusCode == HttpStatusCode.NotFound && action == NotFoundAction.ReturnNull)
                    {
                        return(null);
                    }

                    response.EnsureSuccessStatusCode();
                    return(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                }
            }
        }
Esempio n. 11
0
        public void LeftOfLast(string source, string match, NotFoundAction notFound, System.StringComparison stringComparison, string expected)
        {
            string result = source.LeftOfLast(match, notFound, stringComparison);

            Assert.AreEqual(expected, result);
        }